Skip to content

Commit

Permalink
Also handle NS imports
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 25, 2021
1 parent bb567b1 commit 439c25e
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions babel.config.js
Expand Up @@ -150,7 +150,7 @@ module.exports = function (api) {

convertESM ? "@babel/proposal-export-namespace-from" : null,
convertESM ? "@babel/transform-modules-commonjs" : null,
convertESM ? pluginDefaultImportNode : null,
convertESM ? pluginNodeImportInterop : null,

pluginPackageJsonMacro,

Expand Down Expand Up @@ -408,38 +408,51 @@ function pluginPackageJsonMacro({ types: t }) {
}

// Match the Node.js behavior (the default import is module.exports)
function pluginDefaultImportNode({ types: t }) {
function pluginNodeImportInterop({ template }) {
return {
visitor: {
ImportDeclaration(path) {
const specifiers = path.get("specifiers");
if (
specifiers.length === 0 ||
!specifiers[0].isImportDefaultSpecifier()
) {
if (specifiers.length === 0) {
return;
}

const { source } = path.node;
if (
source.value.startsWith(".") ||
source.value.startsWith("@babel/")
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;
}

path.insertAfter(
t.variableDeclaration("const", [
t.variableDeclarator(
specifiers[0].node.local,
t.callExpression(t.identifier("require"), [path.node.source])
),
])
);
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();
}

specifiers[0].remove();
if (path.node.specifiers.length === 0) path.remove();
},
},
};
Expand Down

0 comments on commit 439c25e

Please sign in to comment.