From 1e4e597654db286d09a4635e58b525d954c1cbc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 21 Feb 2021 02:33:43 +0100 Subject: [PATCH 1/2] Make sure that Rollup's `dynamicRequireTargets` are included --- .yarn/patches/@rollup__plugin-commonjs.patch | 15 +++++++++++++ Gulpfile.mjs | 8 ++++--- package.json | 2 +- .../src/dynamic-require-entrypoint.cjs | 13 ++++++++++++ packages/babel-standalone/src/index.js | 3 +++ yarn.lock | 21 +++++++++++++++++-- 6 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 .yarn/patches/@rollup__plugin-commonjs.patch create mode 100644 packages/babel-standalone/src/dynamic-require-entrypoint.cjs diff --git a/.yarn/patches/@rollup__plugin-commonjs.patch b/.yarn/patches/@rollup__plugin-commonjs.patch new file mode 100644 index 000000000000..a83b206d065e --- /dev/null +++ b/.yarn/patches/@rollup__plugin-commonjs.patch @@ -0,0 +1,15 @@ +See packages/babel-standalone/src/dynamic-require-entrypoint.cjs for the reason for this diff. + +diff --git a/dist/index.js b/dist/index.js +index 712f6a7d81b115d468a764b4139caa00d6cfc358..73fbf004217f3d44b6420d3082a0846b53e00f4c 100644 +--- a/dist/index.js ++++ b/dist/index.js +@@ -1626,7 +1626,7 @@ function commonjs(options = {}) { + const sourceMap = options.sourceMap !== false; + + function transformAndCheckExports(code, id) { +- if (isDynamicRequireModulesEnabled && this.getModuleInfo(id).isEntry) { ++ if (isDynamicRequireModulesEnabled && (this.getModuleInfo(id).isEntry || id.endsWith("/dynamic-require-entrypoint.cjs"))) { + code = + getDynamicPackagesEntryIntro(dynamicRequireModuleDirPaths, dynamicRequireModuleSet) + code; + } diff --git a/Gulpfile.mjs b/Gulpfile.mjs index 0adabcca88f5..ad5f09905afd 100644 --- a/Gulpfile.mjs +++ b/Gulpfile.mjs @@ -245,8 +245,8 @@ function resolveChain(baseUrl, ...packages) { return packages.reduce( (base, pkg) => - require.resolve(pkg + "/package.json", { paths: [path.dirname(base)] }), - fileURLToPath(baseUrl) + path.dirname(require.resolve(pkg + "/package.json", { paths: [base] })), + path.dirname(fileURLToPath(baseUrl)) ); } @@ -304,6 +304,8 @@ function buildRollup(packages, targetBrowsers) { // Rollup doesn't read export maps, so it loads the cjs fallback "packages/babel-compat-data/*.js", "packages/*/src/**/*.cjs", + // See the comment in this file for the reason to include it + "packages/babel-standalone/src/dynamic-require-entrypoint.cjs", ], dynamicRequireTargets: [ // https://github.com/mathiasbynens/regexpu-core/blob/ffd8fff2e31f4597f6fdfee75d5ac1c5c8111ec3/rewrite-pattern.js#L48 @@ -312,7 +314,7 @@ function buildRollup(packages, targetBrowsers) { "./packages/babel-helper-create-regexp-features-plugin", "regexpu-core", "regenerate-unicode-properties" - ) + "/../**", + ) + "/**/*.js", ], }), rollupBabel({ diff --git a/package.json b/package.json index 963356b6baf0..5fcb9732317e 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "@babel/register": "^7.12.0", "@babel/runtime": "^7.12.0", "@rollup/plugin-babel": "^5.2.0", - "@rollup/plugin-commonjs": "^17.1.0", + "@rollup/plugin-commonjs": "patch:@rollup/plugin-commonjs@^17.1.0#./.yarn/patches/@rollup__plugin-commonjs.patch", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^9.0.0", "@rollup/plugin-replace": "^2.3.3", diff --git a/packages/babel-standalone/src/dynamic-require-entrypoint.cjs b/packages/babel-standalone/src/dynamic-require-entrypoint.cjs new file mode 100644 index 000000000000..9dc0a97be28d --- /dev/null +++ b/packages/babel-standalone/src/dynamic-require-entrypoint.cjs @@ -0,0 +1,13 @@ +/* +We bundle @babel/standalone using the dynamicRequireTargets option. +This option needs to inject requrie() calls to all the modules that +could be dynamically required later. + +By default it injects them in the entrypoint, but when rollup loads the +entrypoint it finds syntax that it doesn't support (because +@rollup/plugin-commonjs must run before @rollup/plugin-babel). + +We use "yarn patch" to modify the @rollup/plugin-commonjs package, so +that it injects those "preload" require() calls in this file rather +than in the entrypoint. +*/ diff --git a/packages/babel-standalone/src/index.js b/packages/babel-standalone/src/index.js index f2e218e897b9..6a5ad0634c9d 100644 --- a/packages/babel-standalone/src/index.js +++ b/packages/babel-standalone/src/index.js @@ -10,6 +10,9 @@ /* global VERSION */ /* eslint-disable max-len */ +// $FlowIgnore +import "./dynamic-require-entrypoint.cjs"; + import { transformFromAst as babelTransformFromAst, transform as babelTransform, diff --git a/yarn.lock b/yarn.lock index eceeda271f93..a3425c914821 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3703,7 +3703,7 @@ __metadata: languageName: node linkType: hard -"@rollup/plugin-commonjs@npm:^17.1.0": +"@rollup/plugin-commonjs@^17.1.0": version: 17.1.0 resolution: "@rollup/plugin-commonjs@npm:17.1.0" dependencies: @@ -3720,6 +3720,23 @@ __metadata: languageName: node linkType: hard +"@rollup/plugin-commonjs@patch:@rollup/plugin-commonjs@^17.1.0#./.yarn/patches/@rollup__plugin-commonjs.patch::locator=babel%40workspace%3A.": + version: 17.1.0 + resolution: "@rollup/plugin-commonjs@patch:@rollup/plugin-commonjs@npm%3A17.1.0#./.yarn/patches/@rollup__plugin-commonjs.patch::version=17.1.0&hash=a0d076&locator=babel%40workspace%3A." + dependencies: + "@rollup/pluginutils": ^3.1.0 + commondir: ^1.0.1 + estree-walker: ^2.0.1 + glob: ^7.1.6 + is-reference: ^1.2.1 + magic-string: ^0.25.7 + resolve: ^1.17.0 + peerDependencies: + rollup: ^2.30.0 + checksum: ccf44f300b5b7b9fcc72e22c414bd86ce461e412a0980fb2f73d6d2b562466409ecaae270a54028c4584650c7d1a0718a6af0d15c5a5b41d6cd20a2159ba34fb + languageName: node + linkType: hard + "@rollup/plugin-json@npm:^4.1.0": version: 4.1.0 resolution: "@rollup/plugin-json@npm:4.1.0" @@ -4857,7 +4874,7 @@ __metadata: "@babel/register": ^7.12.0 "@babel/runtime": ^7.12.0 "@rollup/plugin-babel": ^5.2.0 - "@rollup/plugin-commonjs": ^17.1.0 + "@rollup/plugin-commonjs": "patch:@rollup/plugin-commonjs@^17.1.0#./.yarn/patches/@rollup__plugin-commonjs.patch" "@rollup/plugin-json": ^4.1.0 "@rollup/plugin-node-resolve": ^9.0.0 "@rollup/plugin-replace": ^2.3.3 From 67d30610908afedb774a4b9bc873990a54d15c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 21 Feb 2021 22:32:30 +0100 Subject: [PATCH 2/2] Update packages/babel-standalone/src/dynamic-require-entrypoint.cjs Co-authored-by: Brian Ng --- packages/babel-standalone/src/dynamic-require-entrypoint.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-standalone/src/dynamic-require-entrypoint.cjs b/packages/babel-standalone/src/dynamic-require-entrypoint.cjs index 9dc0a97be28d..ca1282f470ab 100644 --- a/packages/babel-standalone/src/dynamic-require-entrypoint.cjs +++ b/packages/babel-standalone/src/dynamic-require-entrypoint.cjs @@ -1,6 +1,6 @@ /* We bundle @babel/standalone using the dynamicRequireTargets option. -This option needs to inject requrie() calls to all the modules that +This option needs to inject require() calls to all the modules that could be dynamically required later. By default it injects them in the entrypoint, but when rollup loads the