-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Durran Jordan <durran@gmail.com>
- Loading branch information
Showing
21 changed files
with
5,632 additions
and
8,312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
etc/rollup/rollup-plugin-require-rewriter/require_rewriter.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import MagicString from 'magic-string'; | ||
|
||
const CRYPTO_IMPORT_ESM_SRC = `const nodejsRandomBytes = await (async () => { | ||
try { | ||
return (await import('node:crypto')).randomBytes;`; | ||
|
||
export class RequireRewriter { | ||
/** | ||
* Take the compiled source code input; types are expected to already have been removed | ||
* Look for the function that depends on crypto, replace it with a top-level await | ||
* and dynamic import for the node:crypto module. | ||
* | ||
* @param {string} code - source code of the module being transformed | ||
* @param {string} id - module id (usually the source file name) | ||
* @returns {{ code: string; map: import('magic-string').SourceMap }} | ||
*/ | ||
transform(code, id) { | ||
if (!id.includes('node_byte_utils')) { | ||
return; | ||
} | ||
if (!code.includes('const nodejsRandomBytes')) { | ||
throw new Error(`Unexpected! 'const nodejsRandomBytes' is missing from ${id}`); | ||
} | ||
|
||
const start = code.indexOf('const nodejsRandomBytes'); | ||
const endString = `return require('node:crypto').randomBytes;`; | ||
const end = code.indexOf(endString) + endString.length; | ||
|
||
if (start < 0 || end < 0) { | ||
throw new Error( | ||
`Unexpected! 'const nodejsRandomBytes' or 'return require('node:crypto').randomBytes;' not found` | ||
); | ||
} | ||
|
||
// MagicString lets us edit the source code and still generate an accurate source map | ||
const magicString = new MagicString(code); | ||
magicString.overwrite(start, end, CRYPTO_IMPORT_ESM_SRC); | ||
|
||
return { | ||
code: magicString.toString(), | ||
map: magicString.generateMap({ hires: true }) | ||
}; | ||
} | ||
} |
Oops, something went wrong.