|
16 | 16 | * under the License. |
17 | 17 | */ |
18 | 18 |
|
19 | | -import { readFileSync } from 'fs'; |
| 19 | +import {readFileSync} from 'fs'; |
| 20 | +import {createRequire} from 'module'; |
20 | 21 | import * as esbuild from 'esbuild'; |
21 | | -import { createRequire } from 'module'; |
22 | 22 | import inlineWorkerPlugin from 'esbuild-plugin-inline-worker'; |
23 | 23 |
|
24 | 24 | const require = createRequire(import.meta.url); |
25 | 25 | const pkg = JSON.parse(readFileSync('./package.json', 'utf8')); |
26 | 26 |
|
27 | 27 | // Get dependencies excluding crypto-related ones that need to be bundled |
28 | | -const externalDeps = [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})] |
29 | | - .filter(dep => !['crypto-browserify', 'randombytes', 'buffer'].includes(dep)); |
| 28 | +const externalDeps = [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})].filter( |
| 29 | + dep => !['crypto-browserify', 'randombytes', 'buffer'].includes(dep), |
| 30 | +); |
30 | 31 |
|
31 | 32 | // Plugin to alias crypto and buffer modules |
32 | 33 | const polyfillPlugin = { |
33 | 34 | name: 'polyfill-plugin', |
34 | 35 | setup(build) { |
35 | 36 | // Crypto polyfill |
36 | | - build.onResolve({ filter: /^crypto$/ }, () => ({ |
37 | | - path: require.resolve('crypto-browserify') |
| 37 | + build.onResolve({filter: /^crypto$/}, () => ({ |
| 38 | + path: require.resolve('crypto-browserify'), |
38 | 39 | })); |
39 | 40 |
|
40 | 41 | // Buffer polyfill |
41 | | - build.onResolve({ filter: /^buffer$/ }, () => ({ |
42 | | - path: require.resolve('buffer/') |
| 42 | + build.onResolve({filter: /^buffer$/}, () => ({ |
| 43 | + path: require.resolve('buffer/'), |
43 | 44 | })); |
44 | | - } |
| 45 | + }, |
45 | 46 | }; |
46 | 47 |
|
47 | 48 | const commonOptions = { |
48 | | - bundle: true, |
49 | | - entryPoints: ['src/index.ts'], |
50 | | - external: externalDeps, |
51 | | - platform: 'browser', |
52 | | - target: ['es2020'], |
53 | | - define: { |
54 | | - global: 'globalThis', // Required by crypto-browserify |
55 | | - 'process.env.NODE_DEBUG': 'false', |
56 | | - 'process.version': '"16.0.0"', |
57 | | - 'process.browser': 'true' |
58 | | - }, |
59 | 49 | banner: { |
60 | 50 | js: ` |
61 | 51 | import { Buffer } from 'buffer/'; |
62 | 52 | if (typeof window !== 'undefined' && !window.Buffer) { |
63 | 53 | window.Buffer = Buffer; |
64 | 54 | } |
65 | | - ` |
| 55 | + `, |
| 56 | + }, |
| 57 | + bundle: true, |
| 58 | + define: { |
| 59 | + global: 'globalThis', // Required by crypto-browserify |
| 60 | + 'process.browser': 'true', |
| 61 | + 'process.env.NODE_DEBUG': 'false', |
| 62 | + 'process.version': '"16.0.0"', |
66 | 63 | }, |
| 64 | + entryPoints: ['src/index.ts'], |
| 65 | + external: externalDeps, |
67 | 66 | footer: { |
68 | 67 | js: ` |
69 | 68 | if (typeof window !== 'undefined' && !window.Buffer) { |
70 | 69 | window.Buffer = require('buffer/').Buffer; |
71 | 70 | } |
72 | | - ` |
| 71 | + `, |
73 | 72 | }, |
| 73 | + platform: 'browser', |
74 | 74 | plugins: [ |
75 | 75 | polyfillPlugin, |
76 | 76 | inlineWorkerPlugin({ |
77 | | - format: 'iife', |
78 | | - target: 'es2020', |
79 | | - platform: 'browser', |
80 | 77 | define: { |
81 | | - 'global': 'self', |
82 | | - 'globalThis': 'self', |
| 78 | + global: 'self', |
| 79 | + globalThis: 'self', |
| 80 | + 'process.browser': 'true', |
83 | 81 | 'process.env.NODE_DEBUG': 'false', |
84 | 82 | 'process.version': '"16.0.0"', |
85 | | - 'process.browser': 'true' |
86 | | - } |
87 | | - }) |
88 | | - ] |
| 83 | + }, |
| 84 | + format: 'iife', |
| 85 | + platform: 'browser', |
| 86 | + target: 'es2020', |
| 87 | + }), |
| 88 | + ], |
| 89 | + target: ['es2020'], |
89 | 90 | }; |
90 | 91 |
|
91 | 92 | await esbuild.build({ |
92 | 93 | ...commonOptions, |
93 | 94 | format: 'esm', |
94 | 95 | outfile: 'dist/index.js', |
95 | | - sourcemap: true |
| 96 | + sourcemap: true, |
96 | 97 | }); |
97 | 98 |
|
98 | 99 | await esbuild.build({ |
99 | 100 | ...commonOptions, |
100 | 101 | format: 'cjs', |
101 | 102 | outfile: 'dist/cjs/index.js', |
102 | | - sourcemap: true |
| 103 | + sourcemap: true, |
103 | 104 | }); |
0 commit comments