-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
146 lines (116 loc) · 3.79 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const path = require('path');
const gulp = require('gulp');
const rollup = require('gulp-better-rollup');
const commonjs = require('rollup-plugin-commonjs');
const resolve = require('rollup-plugin-node-resolve');
const babel = require('rollup-plugin-babel');
const plumber = require('gulp-plumber');
const connect = require('connect');
const serveStatic = require('serve-static');
const rename = require('gulp-rename');
const terser = require('gulp-minify');
const through = require("through2");
const {exec} = require('child_process');
const noop= ()=> through.obj();
const ENTRY_FILE = 'src/is-equal-objects.js';
const BASE_NAME= path.basename(ENTRY_FILE, '.js');
const EXPORT_NAME= 'isEqualObjects';
const DIST_DIR = __dirname + '/dist';
const PORT= 3000;
const toCamelCase= (str)=> str.replace(/-([a-z])/g, (w)=>w[1].toUpperCase());
let isDevMode= false;
function createBuildTask(entryFile, buildOptions) {
const {name, base, ext} = path.parse(entryFile),
{
exportName = name,
destPath = DIST_DIR,
format = 'umd',
toES5 = false,
outFile = `${name}.${format}${ext}`,
taskTargetName = outFile,
include = 'node_modules/**',
exclude,
minify
} = buildOptions || {};
const taskName = `build:${taskTargetName}`;
const rollupPlugins = [
resolve({mainFields: ['module', 'main']}),
commonjs({
include: include !== false ? include : undefined,
exclude
})
];
if (toES5) {
rollupPlugins.push(babel())
}
gulp.task(taskName, () => {
return gulp.src(entryFile)
.pipe(isDevMode ? plumber() : noop())
.pipe(rollup({
plugins: rollupPlugins,
}, {
name: exportName,
format,
preferConst: true
}))
.pipe(outFile !== base ? rename(outFile) : noop())
.pipe(gulp.dest(destPath))
.pipe(minify ? terser({
ext: {
min: '.min.js'
},
noSource: true
}) : noop())
.pipe(minify ? gulp.dest(destPath) : noop());
});
return taskName;
}
const webserver= ()=>{
connect()
.use(serveStatic('./'))
.use(serveStatic('./public'))
.use(serveStatic('./dist'))
.use(serveStatic('./src'))
.use(serveStatic('./test'))
.listen(PORT);
console.log(`Server listening on http://localhost:${PORT}`);
};
const buildTask = createBuildTask(ENTRY_FILE, {exportName: EXPORT_NAME || toCamelCase(BASE_NAME), toES5: true, minify: true});
const buildTaskES = createBuildTask(ENTRY_FILE, {format: 'esm', minify: true});
const buildTaskCJS = createBuildTask(ENTRY_FILE, {format: 'cjs', minify: false});
const runTests= (done)=>{
exec('npm run tests:run', (err, stdout) => {
console.log(`test:\n ${stdout}`);
if(isDevMode){
done();
}else{
done(err);
}
});
};
const build= gulp.parallel(buildTask, buildTaskES, buildTaskCJS);
const setDevEnv= (done)=> {
isDevMode= true;
done();
};
const watchSources= (done)=>{
console.log('Sources watcher started');
gulp.watch('./src/**/*.js', gulp.series(build, runTests), function (file) {
console.log(`File [${file.path}] has been changed`);
});
done();
};
const watchTests= (done)=>{
console.log('Tests watcher started');
gulp.watch('./test/*.js', runTests, function (file) {
console.log(`File [${file.path}] has been changed`);
});
done();
};
const watch= gulp.series(watchSources, watchTests);
const dev= gulp.series(setDevEnv, build, gulp.parallel(runTests, webserver, watch));
module.exports= {
default: dev,
build,
dev
};