-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.js
62 lines (55 loc) · 1.86 KB
/
builder.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
const { start } = require('live-server')
const { build, analyzeMetafile } = require('esbuild')
const ScssModulesPlugin = require('esbuild-scss-modules-plugin').ScssModulesPlugin;
const fs = require('fs-extra')
const JsonRpcServer = require('./src/mock/jsonrpc').JsonRpcServer;
const isDev = process.env.NODE_ENV !== 'production'
const output = process.env.BUILDER_OUTPUT || 'dist'
const metafile = process.env.BUILDER_METAFILE === 'true'
const verboseMetafile = process.env.BUILDER_METAFILE_VERBOSE === 'true'
const analyzeJson = process.env.BUILDER_METAFILE_JSON === 'true'
const serverParams = { // https://www.npmjs.com/package/live-server#usage-from-node
port: 8181,
root: 'dist',
open: true,
middleware: [JsonRpcServer],
};
const buildParams = {
color: true,
entryPoints: [process.env.BUILDER_ENTRYPOINT || 'src/index.jsx'],
loader: { '.js': 'jsx' },
outdir: output,
minify: !isDev,
format: 'cjs',
platform: 'browser',
bundle: true,
sourcemap: true,
logLevel: 'info',
incremental: true,
watch: isDev,
legalComments: process.env.BUILDER_ENTRYPOINT_LEGAL_COMMENT,
metafile,
plugins: [
ScssModulesPlugin({
inject: true,
minify: true,
}),
]
};
!(async () => {
fs.removeSync(output);
if (output === 'dist') {
fs.copySync('public', 'dist');
}
const result = await build(buildParams);
if (analyzeJson) { // BUILDER_METAFILE_JSON=true BUILDER_METAFILE=true NODE_ENV=production node builder.js > /tmp/esbuild.json
console.log(JSON.stringify(result.metafile));
} else if (metafile) { // BUILDER_METAFILE=true NODE_ENV=production node builder.js
console.log(await analyzeMetafile(result.metafile, { verbose: verboseMetafile }));
}
if (isDev) {
start(serverParams);
} else {
process.exit(0);
}
})();