Skip to content

Commit 28ce4d5

Browse files
nbbeekendurran
andauthored
feat(NODE-4713)!: modernize bundling (#534)
Co-authored-by: Durran Jordan <[email protected]>
1 parent 95d5edf commit 28ce4d5

21 files changed

+5632
-8312
lines changed

.evergreen/install-dependencies.sh

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,8 @@ nvm use --lts
4343

4444
set -o xtrace
4545

46-
47-
48-
# setup npm cache in a local directory
49-
cat <<EOT > .npmrc
50-
devdir=${NPM_CACHE_DIR}/.node-gyp
51-
init-module=${NPM_CACHE_DIR}/.npm-init.js
52-
cache=${NPM_CACHE_DIR}
53-
tmp=${NPM_TMP_DIR}
54-
registry=https://registry.npmjs.org
55-
EOT
56-
5746
# install node dependencies
58-
# npm prepare runs after install and will compile the library
59-
# TODO(NODE-3555): rollup dependencies for node polyfills have broken peerDeps. We can remove this flag once we've removed them.
60-
npm install --legacy-peer-deps
47+
npm install
6148

6249
set +o xtrace
6350
echo "Running: nvm use ${NODE_VERSION}"

.evergreen/run-typescript.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
set -o errexit # Exit the script with error if any of the commands fail
33

4-
# source "${PROJECT_DIRECTORY}/.evergreen/init-nvm.sh"
4+
source "${PROJECT_DIRECTORY}/.evergreen/init-nvm.sh"
55

66
set -o xtrace
77

.mocharc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
"timeout": 10000,
1313
"failZero": true,
1414
"sort": true,
15-
"color": true
15+
"color": true,
16+
"node-option": [
17+
"experimental-vm-modules"
18+
]
1619
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import MagicString from 'magic-string';
2+
3+
const CRYPTO_IMPORT_ESM_SRC = `const nodejsRandomBytes = await (async () => {
4+
try {
5+
return (await import('node: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 node: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;
20+
}
21+
if (!code.includes('const nodejsRandomBytes')) {
22+
throw new Error(`Unexpected! 'const nodejsRandomBytes' is missing from ${id}`);
23+
}
24+
25+
const start = code.indexOf('const nodejsRandomBytes');
26+
const endString = `return require('node:crypto').randomBytes;`;
27+
const end = code.indexOf(endString) + endString.length;
28+
29+
if (start < 0 || end < 0) {
30+
throw new Error(
31+
`Unexpected! 'const nodejsRandomBytes' or 'return require('node:crypto').randomBytes;' not found`
32+
);
33+
}
34+
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);
38+
39+
return {
40+
code: magicString.toString(),
41+
map: magicString.generateMap({ hires: true })
42+
};
43+
}
44+
}

0 commit comments

Comments
 (0)