|
1 | 1 | import MagicString from 'magic-string';
|
2 | 2 |
|
3 |
| -const CRYPTO_IMPORT_ESM_SRC = `const nodejsRandomBytes = await (async () => { |
| 3 | +const CRYPTO_IMPORT_ESM_SRC = `import { randomBytes as nodejsRandomBytes } from 'crypto';`; |
| 4 | +const BROWSER_ESM_SRC = `const nodejsRandomBytes = nodejsMathRandomBytes;`; |
| 5 | +const CODE_TO_REPLACE = `const nodejsRandomBytes = (() => { |
4 | 6 | try {
|
5 |
| - return (await import('crypto')).randomBytes;`; |
6 |
| - |
7 |
| -export class RequireRewriter { |
8 |
| - /** |
9 |
| - * Take the compiled source code input; types are expected to already have been removed |
10 |
| - * Look for the function that depends on crypto, replace it with a top-level await |
11 |
| - * and dynamic import for the crypto module. |
12 |
| - * |
13 |
| - * @param {string} code - source code of the module being transformed |
14 |
| - * @param {string} id - module id (usually the source file name) |
15 |
| - * @returns {{ code: string; map: import('magic-string').SourceMap }} |
16 |
| - */ |
17 |
| - transform(code, id) { |
18 |
| - if (!id.includes('node_byte_utils')) { |
19 |
| - return; |
| 7 | + return require('crypto').randomBytes; |
20 | 8 | }
|
21 |
| - if (!code.includes('const nodejsRandomBytes')) { |
22 |
| - throw new Error(`Unexpected! 'const nodejsRandomBytes' is missing from ${id}`); |
| 9 | + catch { |
| 10 | + return nodejsMathRandomBytes; |
23 | 11 | }
|
| 12 | +})();`; |
24 | 13 |
|
25 |
| - const start = code.indexOf('const nodejsRandomBytes'); |
26 |
| - const endString = `return require('crypto').randomBytes;`; |
27 |
| - const end = code.indexOf(endString) + endString.length; |
| 14 | +export function requireRewriter({ isBrowser = false } = {}) { |
| 15 | + return { |
| 16 | + /** |
| 17 | + * Take the compiled source code input; types are expected to already have been removed |
| 18 | + * Look for the function that depends on crypto, replace it with a top-level await |
| 19 | + * and dynamic import for the crypto module. |
| 20 | + * |
| 21 | + * @param {string} code - source code of the module being transformed |
| 22 | + * @param {string} id - module id (usually the source file name) |
| 23 | + * @returns {{ code: string; map: import('magic-string').SourceMap }} |
| 24 | + */ |
| 25 | + transform(code, id) { |
| 26 | + if (!id.includes('node_byte_utils')) { |
| 27 | + return; |
| 28 | + } |
| 29 | + const start = code.indexOf(CODE_TO_REPLACE); |
| 30 | + if (start === -1) { |
| 31 | + throw new Error(`Unexpected! Code meant to be replaced is missing from ${id}`); |
| 32 | + } |
28 | 33 |
|
29 |
| - if (start < 0 || end < 0) { |
30 |
| - throw new Error( |
31 |
| - `Unexpected! 'const nodejsRandomBytes' or 'return require('crypto').randomBytes;' not found` |
32 |
| - ); |
33 |
| - } |
| 34 | + const end = start + CODE_TO_REPLACE.length; |
34 | 35 |
|
35 |
| - // MagicString lets us edit the source code and still generate an accurate source map |
36 |
| - const magicString = new MagicString(code); |
37 |
| - magicString.overwrite(start, end, CRYPTO_IMPORT_ESM_SRC); |
| 36 | + // MagicString lets us edit the source code and still generate an accurate source map |
| 37 | + const magicString = new MagicString(code); |
| 38 | + magicString.overwrite(start, end, isBrowser ? BROWSER_ESM_SRC : CRYPTO_IMPORT_ESM_SRC); |
38 | 39 |
|
39 |
| - return { |
40 |
| - code: magicString.toString(), |
41 |
| - map: magicString.generateMap({ hires: true }) |
42 |
| - }; |
43 |
| - } |
| 40 | + return { |
| 41 | + code: magicString.toString(), |
| 42 | + map: magicString.generateMap({ hires: true }) |
| 43 | + }; |
| 44 | + } |
| 45 | + }; |
44 | 46 | }
|
0 commit comments