Skip to content

Commit

Permalink
feat(NODE-4713)!: modernize bundling (#534)
Browse files Browse the repository at this point in the history
Co-authored-by: Durran Jordan <durran@gmail.com>
  • Loading branch information
nbbeeken and durran committed Dec 16, 2022
1 parent 95d5edf commit 28ce4d5
Show file tree
Hide file tree
Showing 21 changed files with 5,632 additions and 8,312 deletions.
15 changes: 1 addition & 14 deletions .evergreen/install-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,8 @@ nvm use --lts

set -o xtrace



# setup npm cache in a local directory
cat <<EOT > .npmrc
devdir=${NPM_CACHE_DIR}/.node-gyp
init-module=${NPM_CACHE_DIR}/.npm-init.js
cache=${NPM_CACHE_DIR}
tmp=${NPM_TMP_DIR}
registry=https://registry.npmjs.org
EOT

# install node dependencies
# npm prepare runs after install and will compile the library
# TODO(NODE-3555): rollup dependencies for node polyfills have broken peerDeps. We can remove this flag once we've removed them.
npm install --legacy-peer-deps
npm install

set +o xtrace
echo "Running: nvm use ${NODE_VERSION}"
Expand Down
2 changes: 1 addition & 1 deletion .evergreen/run-typescript.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -o errexit # Exit the script with error if any of the commands fail

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

set -o xtrace

Expand Down
5 changes: 4 additions & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"timeout": 10000,
"failZero": true,
"sort": true,
"color": true
"color": true,
"node-option": [
"experimental-vm-modules"
]
}
44 changes: 44 additions & 0 deletions etc/rollup/rollup-plugin-require-rewriter/require_rewriter.mjs
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 })
};
}
}
Loading

0 comments on commit 28ce4d5

Please sign in to comment.