Repository navigation
Expand file tree
/
Copy pathesbuild.config.js
More file actions
76 lines (65 loc) · 2.49 KB
/
Copy pathesbuild.config.js
File metadata and controls
76 lines (65 loc) · 2.49 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
// Builds the esbuild bundles into app/assets/builds/. Usage:
// node esbuild.config.js <core-case>... [--watch[=forever]]
// See docs/js_pipeline.md for how these bundles relate to the importmap.
const esbuild = require('esbuild');
const SHARED = {
bundle: true,
sourcemap: true,
format: 'iife',
platform: 'browser',
publicPath: '/assets',
};
const JS = 'app/javascript';
const BUNDLES = {
// Core case page (Stimulus controllers + runtime modules).
'core-case': {
entryPoints: [`${JS}/core_stimulus.js`],
outfile: 'app/assets/builds/core_case.js',
// Share the browser's importmap editor and Bootstrap/Vega globals with Rails pages.
format: 'esm',
external: ['bootstrap_globals', 'vega_globals'],
plugins: [{
name: 'shared-editor-importmap',
setup(build) {
// The modules alias is expanded before resolution; retain the browser's bare specifier.
build.onResolve({ filter: /(^|\/)modules\/editor(?:\.js)?$/ }, () => ({
path: 'modules/editor', external: true
}));
}
}],
// Bare specifiers used by core code; the importmap resolves these for Rails pages.
alias: {
controllers: `./${JS}/controllers`,
utils: `./${JS}/utils`,
stores: `./${JS}/stores`,
api: `./${JS}/api`,
modules: `./${JS}/modules`,
core_runtime: `./${JS}/core_runtime.js`,
quepid_search: `./${JS}/quepid_search.js`,
quepid_store: `./${JS}/quepid_store.js`
}
}
};
async function main() {
const args = process.argv.slice(2);
const watch = args.some((arg) => arg === '--watch' || arg.startsWith('--watch='));
const names = args.filter((arg) => !arg.startsWith('--'));
const unknown = names.filter((name) => !BUNDLES[name]);
if (names.length === 0 || unknown.length > 0) {
console.error(`Usage: node esbuild.config.js <${Object.keys(BUNDLES).join('|')}>... [--watch]`);
process.exit(1);
}
const contexts = await Promise.all(
names.map((name) => esbuild.context({ ...SHARED, ...BUNDLES[name], logLevel: 'info' }))
);
if (watch) {
await Promise.all(contexts.map((ctx) => ctx.watch()));
return; // watchers keep the process alive
}
// allSettled lets every bundle finish before disposing; esbuild has already logged errors.
const results = await Promise.allSettled(contexts.map((ctx) => ctx.rebuild()));
if (results.some((result) => result.status === 'rejected')) process.exitCode = 1;
await Promise.all(contexts.map((ctx) => ctx.dispose()));
}
main();