-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
58 lines (47 loc) · 1.36 KB
/
Copy pathgulpfile.js
File metadata and controls
58 lines (47 loc) · 1.36 KB
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
const gulp = require('gulp');
const fs = require('fs');
const path = require('path');
const del = require('del');
const rollup = require('rollup');
const rollupBabel = require('rollup-plugin-babel');
// tasks
gulp.task('clean-dist', cleanDist);
gulp.task('build-js', buildJs);
gulp.task('watch', watchJs);
gulp.task('build-all', gulp.series('clean-dist', 'build-js'));
gulp.task('default', gulp.series('build-all', 'watch'));
// functions
async function cleanDist() {
await del.sync('dist/**/*');
}
async function rollupBuild(folderName) {
const inputFolder = `./src/${folderName}`;
const inputOptions = {
input: `${inputFolder}/index.js`,
plugins: [
rollupBabel({
exclude: 'node_modules/**',
})
]
};
const outputOptions = {
file: `./dist/${folderName}.user.js`,
format: 'iife',
banner: () => fs.readFileSync(`${inputFolder}/header-comment.js`),
};
// create a bundle
const bundle = await rollup.rollup(inputOptions);
await bundle.write(outputOptions);
}
async function buildJs() {
const srcDir = 'src';
const srcFolders = fs.readdirSync(srcDir)
.filter(file => fs.statSync(path.join(srcDir, file)).isDirectory());
for (const folder of srcFolders) {
await rollupBuild(folder);
}
}
function watchJs() {
gulp.watch('src/**/*.js', buildJs)
.on('change', file => console.log('File changed:', file));
}