-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
50 lines (45 loc) · 1.26 KB
/
build.js
File metadata and controls
50 lines (45 loc) · 1.26 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
const esbuild = require('esbuild');
const fs = require('fs');
const isProd = process.argv.includes('--prod');
// Inlines CSS as a JS snippet that appends a <style> tag to <head>.
// Replaces style-loader with zero runtime overhead.
const cssInjectPlugin = {
name: 'css-inject',
setup(build) {
build.onLoad({ filter: /\.css$/ }, (args) => {
// args.path is resolved by esbuild from source imports — not user input. nosec
const css = fs.readFileSync(args.path, 'utf8');
return {
contents: `
const __style = document.createElement('style');
__style.textContent = ${JSON.stringify(css)};
document.head.appendChild(__style);
`,
loader: 'js',
};
});
},
};
const options = {
entryPoints: ['src/index.js'],
bundle: true,
minify: isProd,
sourcemap: !isProd,
format: 'iife',
outfile: isProd
? 'dist/libretexts-support-widget.min.js'
: 'dist/libretexts-support-widget.dev.js',
target: ['es2018'],
plugins: [cssInjectPlugin],
logLevel: 'info',
};
async function main() {
if (process.argv.includes('--watch')) {
const ctx = await esbuild.context(options);
await ctx.watch();
console.log('Watching for changes...');
} else {
await esbuild.build(options).catch(() => process.exit(1));
}
}
main();