forked from bullet-train-co/bullet_train
-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild.config.js
74 lines (69 loc) · 2.07 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
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 };
});
},
});
require("esbuild").build({
entryPoints: [
path.join(process.cwd(), "app/javascript/application.js"),
path.join(process.cwd(), "app/javascript/intl-tel-input-utils.js")
],
define: {
global: "window"
},
bundle: true,
sourcemap: true,
outdir: path.join(process.cwd(), "app/assets/builds"),
// absWorkingDir: path.join(process.cwd(), "app/javascript"),
loader: {
".png": "file",
".jpg": "file",
".svg": "file",
".woff": "file",
".ttf": "file",
".eot": "file",
},
watch: process.argv.includes("--watch"),
plugins: [
ImportGlobPlugin()
],
}).catch(() => process.exit(1));