-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
66 lines (56 loc) · 1.9 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const gulp = require('gulp');
const entry = './src/server/**/*.js';
const babel = require('gulp-babel');
const watch = require('gulp-watch');
const cleanEntry = ['./src/server/config/index.js'];
const rollup = require('gulp-rollup'); // 做流清洗 也就是tree shaking
const replace = require('rollup-plugin-replace'); // 用提供的结果去替换打包后的变量名
function builddev() {
return watch(entry, { ignoreInitial: false }, function () {
gulp.src(entry)
.pipe(babel({
babelrc: false, // 需要自己独立的编译 很重要 忽略外面的.babelrc文件
"plugins": ["@babel/plugin-transform-modules-commonjs"] // 需要安装@babel/plugin-transform-modules-commonjs
})).pipe(gulp.dest('dist'));
})
}
// 上线环境
function buildprod() {
return gulp.src(entry)
.pipe(babel({
babelrc: false,
ignore: cleanEntry, // 忽略掉此文件
"plugins": ["@babel/plugin-transform-modules-commonjs"]
})).pipe(gulp.dest('dist'));
}
// 上线配置 tree shaking
function buildconfig() {
return gulp.src(entry)
// transform the files here.
.pipe(rollup({
output: {
format: "cjs" // common js 规范
},
// any option supported by Rollup can be set here.
input: cleanEntry,
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify('production')
})
]
}))
.pipe(gulp.dest('./dist'));
}
// 校验
function lint() {}
// Gulp 4抛弃了依赖参数(dependency parameter),用执行函数来代替:
// gulp.series 用于串行(顺序)执行
// gulp.parallel 用于并行执行
let build = gulp.series(builddev);
if (process.env.NODE_ENV == "production") {
build = gulp.series(buildprod, buildconfig);
}
if (process.env.NODE_ENV == "lint") {
build = gulp.series('lint');
}
gulp.task('default', build);