-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
42 lines (37 loc) · 1.13 KB
/
build.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
import { build } from 'esbuild';
import { readdir } from 'fs/promises';
await build({
entryPoints: ['src/index.ts'],
outfile: 'dist/index.js',
plugins: [importDir('commands'), importDir('modules')],
bundle: true,
treeShaking: true,
sourcemap: 'linked',
packages: 'external',
format: 'esm',
target: 'esnext',
platform: 'node',
logLevel: 'error'
});
/**
* @param {string} namespace
* @returns {import('esbuild').Plugin}
*/
function importDir(namespace) {
return {
name: `import-dir:${namespace}`,
setup(build) {
const filter = new RegExp(`^~${namespace}$`);
const dir = `./src/${namespace}`;
build.onResolve({ filter }, args => ({ path: args.path, namespace }));
build.onLoad({ filter, namespace }, async () => {
const files = await readdir(dir);
const contents = files.map(file => {
file = file.replace(/\.ts$/, '.js');
return `import './${file}';`
}).join('\n');
return { contents, resolveDir: dir };
});
},
};
}