forked from bullet-train-co/bullet_train
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.config.js
117 lines (105 loc) · 3.26 KB
/
esbuild.config.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const path = require('path');
const { execSync } = require("child_process");
const glob = require('glob').sync
// Glob plugin derived from:
// https://github.com/thomaschaaf/esbuild-plugin-import-glob
// https://github.com/xiaohui-zhangxh/jsbundling-rails/commit/b15025dcc20f664b2b0eb238915991afdbc7cb58
const ImportGlobPlugin = () => ({
name: 'require-context',
setup: (build) => {
build.onResolve({ filter: /\*/ }, async (args) => {
console.log('resolveDir', args.resolveDir)
if (args.resolveDir === '') {
return; // Ignore unresolvable paths
}
console.log('path', args.path)
return {
path: args.path,
namespace: 'import-glob',
pluginData: {
resolveDir: args.resolveDir,
},
};
});
build.onLoad({ filter: /.*/, namespace: 'import-glob' }, async (args) => {
const files = (
glob(args.path, {
cwd: args.pluginData.resolveDir,
})
).sort();
let importerCode = `
${files
.map((module, index) => `import * as module${index} from './${module}'`)
.join(';')}
export default [${files
.map((module, index) => `module${index}.default`)
.join(',')}];
export const context = {
${files.map((module, index) => `'${module}': module${index}.default`).join(',')}
}
`;
return { contents: importerCode, resolveDir: args.pluginData.resolveDir };
});
},
});
let themeFile = ""
if (process.env.THEME) {
themeFile = execSync(`bundle exec bin/theme javascript ${process.env.THEME}`).toString().trim()
}
// Could also swap to packs?
const otherEntrypoints = {}
glob("app/javascript/entrypoints/**/*.*")
.forEach((file) => {
// strips app/javascript/entrypoints from the key.
const key = path.join(path.dirname(file), path.parse(file).name).split(path.sep + "entrypoints" + path.sep)[1]
const value = "." + path.sep + path.join(path.dirname(file), path.basename(file))
otherEntrypoints[key] = value
});
const themeEntrypoints = {}
if (process.env.THEME) {
themeEntrypoints[`application.${process.env.THEME}`] = themeFile
}
let build_details = {
entryPoints: {
...otherEntrypoints,
"application": path.join(process.cwd(), "app/javascript/application.js"),
"intl-tel-input-utils": path.join(process.cwd(), "app/javascript/intl-tel-input-utils.js"),
...themeEntrypoints,
},
define: {
global: "window"
},
bundle: true,
// ESM + Splitting will only work if the script is type="module"
// splitting: true,
// format: "esm",
format: "iife",
sourcemap: true,
outdir: path.join(process.cwd(), "app/assets/builds"),
loader: {
".png": "file",
".jpg": "file",
".svg": "file",
".woff": "file",
".woff2": "file",
".ttf": "file",
".eot": "file",
},
plugins: [
ImportGlobPlugin()
],
// TODO: Silencing warnings until the charset warning is fixed.
logLevel: 'error'
}
async function serve_with_esbuild() {
let ctx = await require("esbuild").context(build_details)
await ctx.watch()
let { host, port } = await ctx.serve({
servedir: path.join(process.cwd(), "app/assets/builds")
})
}
if(process.argv.includes("--watch")) {
serve_with_esbuild()
} else {
require("esbuild").build(build_details)
}