Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-4713)!: modernize bundling #534

Merged
merged 6 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
durran marked this conversation as resolved.
Show resolved Hide resolved
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"
durran marked this conversation as resolved.
Show resolved Hide resolved
]
}
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