-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
90 lines (81 loc) · 2.27 KB
/
build.js
File metadata and controls
90 lines (81 loc) · 2.27 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import esbuild from 'esbuild';
import alias from 'esbuild-plugin-alias';
import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { createRequire } from 'module';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url);
const projectRoot = __dirname;
const srcDir = path.resolve(projectRoot, 'src');
const distDir = path.resolve(projectRoot, 'dist');
const typesDir = path.resolve(distDir, 'types');
const sharedConfig = {
entryPoints: [path.resolve(srcDir, 'index.ts')],
bundle: true,
sourcemap: true,
minify: true,
target: ['es2020'],
};
const nodeAlias = alias({
signers: path.resolve(srcDir, 'signers/common.ts'),
});
const browserAlias = alias({
signers: path.resolve(srcDir, 'signers/browser.ts'),
});
const nodeCjsConfig = {
...sharedConfig,
platform: 'node',
format: 'cjs',
outfile: path.resolve(distDir, 'index.cjs'),
define: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
},
external: ['process', 'stream/promises', '@dha-team/arbundles', 'http-message-signatures'],
plugins: [nodeAlias],
};
const nodeEsmConfig = {
...nodeCjsConfig,
format: 'esm',
outfile: path.resolve(distDir, 'index.js'),
};
const browserConfig = {
...sharedConfig,
platform: 'browser',
format: 'esm',
plugins: [
browserAlias,
nodeModulesPolyfillPlugin({
modules: {
crypto: true,
constants: true,
events: true,
stream: true,
},
}),
alias({
crypto: require.resolve('crypto-browserify'),
constants: require.resolve('constants-browserify'),
stream: require.resolve('stream-browserify'),
process: require.resolve('process/browser'),
}),
],
bundle: true,
minify: true,
outfile: path.resolve(distDir, 'index.esm.js'),
external: ['fs', 'os', 'path', 'http', 'https', 'zlib'],
};
(async () => {
try {
const configs = [nodeCjsConfig, nodeEsmConfig, browserConfig];
for (const cfg of configs) {
console.log(`Building ${path.relative(projectRoot, cfg.outfile)} (${cfg.platform}/${cfg.format})...`);
await esbuild.build(cfg);
}
console.log('Build complete!');
} catch (err) {
console.error('Build failed:', err);
process.exit(1);
}
})();