forked from muxinc/media-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
58 lines (49 loc) · 1.5 KB
/
build.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
const _ = require('lodash');
const chokidar = require('chokidar');
const esbuild = require('esbuild');
const glob = require('tiny-glob');
const serveHandler = require('serve-handler');
const http = require('http');
const args = process.argv;
const PORT = 3001;
async function build(opts = {}) {
const start = Date.now();
/*
index.js is the entire library bundled up + exported in one file.
*/
await esbuild.build({
entryPoints: ['./index.js'],
bundle: true,
outfile: './dist/index.js',
...opts,
});
/*
For all the other files, we'll just export them to dist so folks can import the modules individually if they want.
*/
let entryPoints = await glob('./src/js/**/*.js');
await esbuild.build({
entryPoints,
outdir: 'dist',
...opts,
});
console.log(`👏 Built ${entryPoints.length} files in ${Date.now() - start}ms`);
};
if (args.includes('--dev')) {
const server = http.createServer((request, response) => {
// You pass two more arguments for config and middleware
// More details here: https://github.com/vercel/serve-handler#options
return serveHandler(request, response);
})
server.listen(PORT, () => {
console.log(`💫 Preview server running at http://localhost:${PORT}`);
});
const watcher = chokidar.watch('./src', {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true
});
watcher.on('all', _.throttle(async (event, path) => {
await build();
}, 250, { trailing: false }));
} else {
build({ minify: true });
}