-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesbuild.browser.mjs
More file actions
49 lines (44 loc) · 1.46 KB
/
esbuild.browser.mjs
File metadata and controls
49 lines (44 loc) · 1.46 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
import { build } from 'esbuild';
import path from 'path';
// Plugin to stub Node builtins that aren't needed in browser
const emptyNodeBuiltins = {
name: 'empty-node-builtins',
setup(build) {
const builtins = ['buffer', 'crypto', 'stream', 'os', 'fs', 'http', 'https',
'util', 'zlib', 'vm', 'assert', 'net', 'tls', 'child_process'];
const filter = new RegExp(`^(${builtins.join('|')})$`);
build.onResolve({ filter }, (args) => ({
path: args.path,
namespace: 'empty-node-builtin',
}));
build.onLoad({ filter: /.*/, namespace: 'empty-node-builtin' }, () => ({
contents: 'export default {};',
loader: 'js',
}));
},
};
const result = await build({
entryPoints: ['src/browser.ts'],
bundle: true,
minify: true,
sourcemap: true,
format: 'iife',
globalName: 'NostrNsecSeedphrase',
outfile: 'dist/browser/nostr-nsec-seedphrase.min.js',
target: ['es2020'],
platform: 'browser',
alias: {
// Force CJS build of nostr-crypto-utils (its ESM output lacks .mjs extensions)
'nostr-crypto-utils': path.resolve('node_modules/nostr-crypto-utils/dist/cjs/index.js'),
},
define: {
'process.env.NODE_ENV': '"production"',
'global': 'globalThis',
},
plugins: [emptyNodeBuiltins],
metafile: true,
});
const output = Object.entries(result.metafile.outputs)
.filter(([k]) => k.endsWith('.js'))
.map(([k, v]) => `${k}: ${(v.bytes / 1024).toFixed(1)}KB`);
console.log('Browser bundle built:', output.join(', '));