Skip to content

Commit

Permalink
Remove config hack for old Node compatibility (#11199)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 8, 2020
1 parent 43e5379 commit 2026934
Showing 1 changed file with 12 additions and 32 deletions.
44 changes: 12 additions & 32 deletions babel.config.js
Expand Up @@ -154,44 +154,24 @@ module.exports = function(api) {
return config;
};

// !!! WARNING !!! Hacks are coming

// import() uses file:// URLs for absolute imports, while require() uses
// file paths.
// Since this isn't handled by @babel/plugin-transform-modules-commonjs,
// we must handle it here.
// However, fileURLToPath is only supported starting from Node.js 10.
// In older versions, we can remove the pathToFileURL call so that it keeps
// the original absolute path.
// NOTE: This plugin must run before @babel/plugin-transform-modules-commonjs,
// and assumes that the target is the current node version.
function dynamicImportUrlToPath({ template }) {
const currentNodeSupportsURL = !!require("url").pathToFileURL;

if (currentNodeSupportsURL) {
return {
visitor: {
CallExpression(path) {
if (path.get("callee").isImport()) {
path.get("arguments.0").replaceWith(
template.expression.ast`
require("url").fileURLToPath(${path.node.arguments[0]})
`
);
}
},
},
};
} else {
// TODO: Remove in Babel 8 (it's not needed when using Node 10)
return {
visitor: {
CallExpression(path) {
if (path.get("callee").isIdentifier({ name: "pathToFileURL" })) {
path.replaceWith(path.get("arguments.0"));
}
},
return {
visitor: {
CallExpression(path) {
if (path.get("callee").isImport()) {
path.get("arguments.0").replaceWith(
template.expression.ast`
require("url").fileURLToPath(${path.node.arguments[0]})
`
);
}
},
};
}
},
};
}

0 comments on commit 2026934

Please sign in to comment.