Skip to content

Commit

Permalink
Update @babel/* packages (#13231)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 30, 2021
1 parent b99c4f0 commit 41c0224
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 193 deletions.
80 changes: 26 additions & 54 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ module.exports = function (api) {
["@babel/proposal-object-rest-spread", { useBuiltIns: true }],

convertESM ? "@babel/proposal-export-namespace-from" : null,
convertESM ? "@babel/transform-modules-commonjs" : null,
convertESM ? pluginNodeImportInteropBabel : pluginNodeImportInteropRollup,
convertESM
? ["@babel/transform-modules-commonjs", { importInterop }]
: pluginNodeImportInteropRollup,
convertESM ? pluginImportMetaUrl : null,

pluginPackageJsonMacro,
Expand Down Expand Up @@ -186,7 +187,7 @@ module.exports = function (api) {
].map(normalize),
plugins: [
// Explicitly use the lazy version of CommonJS modules.
["@babel/transform-modules-commonjs", { lazy: true }],
["@babel/transform-modules-commonjs", { importInterop, lazy: true }],
],
},
convertESM && {
Expand Down Expand Up @@ -218,6 +219,28 @@ module.exports = function (api) {
return config;
};

function importInterop(source) {
if (
// These internal files are "real CJS" (whose default export is
// on module.exports) and not compiled ESM.
source.startsWith("@babel/compat-data/") ||
source.includes("babel-eslint-shared-fixtures/utils") ||
// @babel/preset-modules is an external package, and it uses
// module.exports for the default export
source.startsWith("@babel/preset-modules/")
) {
return "node";
}
if (source[0] === "." || source.startsWith("@babel/")) {
// We don't need to worry about interop for internal files, since we know
// for sure that they are ESM modules compiled to CJS
return "none";
}

// For external modules, we want to match the Node.js behavior
return "node";
}

// env vars from the cli are always strings, so !!ENV_VAR returns true for "false"
function bool(value) {
return value && value !== "false" && value !== "0";
Expand Down Expand Up @@ -423,57 +446,6 @@ function pluginPackageJsonMacro({ types: t }) {
};
}

// Match the Node.js behavior (the default import is module.exports)
function pluginNodeImportInteropBabel({ template }) {
return {
visitor: {
ImportDeclaration(path) {
const specifiers = path.get("specifiers");
if (specifiers.length === 0) {
return;
}

const { source } = path.node;
if (
source.value.startsWith(".") ||
source.value.startsWith("@babel/") ||
source.value === "charcodes"
) {
// For internal modules, it's either "all CJS" or "all ESM".
// We don't need to worry about interop.
return;
}

const defImport = specifiers.find(s => s.isImportDefaultSpecifier());
const nsImport = specifiers.find(s => s.isImportNamespaceSpecifier());

if (defImport) {
path.insertAfter(
template.ast`
const ${defImport.node.local} = require(${source});
`
);
defImport.remove();
}

if (nsImport) {
path.insertAfter(
template.ast`
const ${nsImport.node.local} = {
...require(${source}),
default: require(${source}),
};
`
);
nsImport.remove();
}

if (path.node.specifiers.length === 0) path.remove();
},
},
};
}

function pluginNodeImportInteropRollup({ types: t }) {
const depsUsing__esModuleAndDefaultExport = src =>
src.startsWith("babel-plugin-polyfill-") || src === "regenerator-transform";
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
"test:runtime:node": "node test/runtime-integration/node.cjs"
},
"devDependencies": {
"@babel/cli": "^7.13.0",
"@babel/core": "^7.13.1",
"@babel/cli": "^7.13.16",
"@babel/core": "^7.14.0",
"@babel/eslint-config-internal": "workspace:*",
"@babel/eslint-parser": "workspace:*",
"@babel/eslint-plugin-development": "workspace:*",
"@babel/eslint-plugin-development-internal": "workspace:*",
"@babel/plugin-proposal-dynamic-import": "^7.13.8",
"@babel/plugin-proposal-export-namespace-from": "^7.12.13",
"@babel/plugin-proposal-object-rest-spread": "^7.13.0",
"@babel/plugin-transform-modules-commonjs": "^7.13.0",
"@babel/plugin-transform-runtime": "^7.13.7",
"@babel/preset-env": "^7.13.5",
"@babel/preset-flow": "^7.12.13",
"@babel/plugin-proposal-object-rest-spread": "^7.13.8",
"@babel/plugin-transform-modules-commonjs": "^7.14.0",
"@babel/plugin-transform-runtime": "^7.13.15",
"@babel/preset-env": "^7.14.0",
"@babel/preset-flow": "^7.13.13",
"@babel/preset-typescript": "^7.13.0",
"@babel/register": "^7.13.0",
"@babel/runtime": "^7.13.7",
"@babel/register": "^7.13.16",
"@babel/runtime": "^7.14.0",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-commonjs": "patch:@rollup/plugin-commonjs@^17.1.0#./.yarn/patches/@rollup__plugin-commonjs.patch",
"@rollup/plugin-json": "^4.1.0",
Expand Down

0 comments on commit 41c0224

Please sign in to comment.