-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
74 lines (65 loc) · 1.68 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
import childProcess from 'child_process';
import gulp from 'gulp';
import log from 'fancy-log';
import { rollup, watch } from 'rollup';
import { deleteAsync } from 'del';
const DIST = 'dist';
async function loadConfig() {
const { default: rollupConfig } = await import('./rollup.config.js');
return rollupConfig;
}
export function clean() {
return deleteAsync([DIST]);
}
function copy() {
return gulp.src('src/public/**').pipe(gulp.dest(DIST));
}
async function buildJs() {
const rollupConfig = await loadConfig();
return Promise.all(
rollupConfig.map(async (config) => {
const bundle = await rollup(config);
await bundle.write(config.output);
})
);
}
async function watchJs() {
const rollupConfig = await loadConfig();
const watcher = watch(rollupConfig);
return new Promise((resolve, reject) => {
watcher.on('event', (e) => {
if (e.code === 'ERROR') {
console.error();
console.error(`${e.error}`);
console.error();
reject(e);
} else if (e.code === 'BUNDLE_END') {
log(`Compilation success after ${e.duration}ms`);
resolve();
}
});
});
}
let loaded = false;
function serveDemo() {
if (loaded) return;
loaded = true;
childProcess.spawn(
'node',
['.', 'serve', '--cwd', 'demo', '-p', process.env.PORT || 4000],
{
stdio: 'inherit',
}
);
}
function wrapError(handle) {
const wrapped = () =>
handle().catch((err) => {
log(err.toString());
});
wrapped.displayName = handle.name;
return wrapped;
}
export const build = gulp.series([copy, buildJs]);
export const dev = gulp.series([copy, watchJs]);
export const demo = gulp.series([dev, serveDemo]);