-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.config.mjs
88 lines (81 loc) · 2.06 KB
/
esbuild.config.mjs
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
import esbuild from 'esbuild';
import fs from 'fs';
import path from 'path';
import { NodeResolvePlugin } from '@esbuild-plugins/node-resolve';
const wasmPlugin = {
name: 'wasm',
setup(build) {
build.onResolve({ filter: /\.wasm$/ }, args => {
return { path: path.resolve('./node_modules', args.path), namespace: 'wasm' };
});
build.onLoad({ filter: /.*/, namespace: 'wasm' }, async (args) => {
const buffer = await fs.promises.readFile(args.path);
const base64 = buffer.toString('base64');
return {
contents: `export default Buffer.from("${base64}", "base64")`,
loader: 'js',
};
});
},
};
const plugins = [
NodeResolvePlugin({
extensions: ['.ts', '.js', '.wasm'],
onResolved: (resolved) => {
if (resolved.includes('node_modules')) {
return {
external: true,
}
}
return resolved
},
}),
]
const generic = {
chunkNames: "[name]",
assetNames: "[name]",
entryPoints: ['src/index.ts'],
sourcemap: false,
bundle: true,
platform: 'neutral',
splitting: false,
resolveExtensions: ['.ts', '.js', '.wasm'],
inject: [],
mainFields: ['module', 'main'],
banner: {
js: "\nif (typeof Buffer === 'undefined') {\n global.Buffer = require('buffer').Buffer;\n}",
},
define: {
'global.Buffer': 'Buffer',
},
external: ['buffer']
}
// Build ES module
esbuild.build({
...generic,
outfile:"build/esm/index.mjs",
target: ['esnext'],
format: 'esm',
plugins: [
wasmPlugin,
...plugins
],
}).then(() => {
esbuild.build({
...generic,
outfile:"build/cjs/index.cjs",
target: ['es6'],
format: 'cjs',
plugins: [
wasmPlugin,
...plugins
],
}).catch((err) => {
console.log(err)
process.exit(1)
});
})
.catch((err) => {
console.log(err)
process.exit(1)
});