Skip to content

Commit

Permalink
[internal] Use the Node.js behavior for default imports
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 25, 2021
1 parent 962d814 commit bb567b1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
39 changes: 39 additions & 0 deletions babel.config.js
Expand Up @@ -150,6 +150,7 @@ module.exports = function (api) {

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

pluginPackageJsonMacro,

Expand Down Expand Up @@ -405,3 +406,41 @@ function pluginPackageJsonMacro({ types: t }) {
},
};
}

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

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

specifiers[0].remove();
},
},
};
}
Expand Up @@ -13,10 +13,12 @@ import assert from "assert";
import fs from "fs";
import path from "path";
import vm from "vm";
import checkDuplicatedNodes from "babel-check-duplicated-nodes";
import QuickLRU from "quick-lru";
import escapeRegExp from "./escape-regexp.cjs";

import _checkDuplicatedNodes from "babel-check-duplicated-nodes";
const checkDuplicatedNodes = _checkDuplicatedNodes.default;

const cachedScripts = new QuickLRU({ maxSize: 10 });
const contextModuleCache = new WeakMap();
const sharedTestContext = createContext();
Expand Down
10 changes: 4 additions & 6 deletions packages/babel-highlight/src/index.ts
@@ -1,7 +1,8 @@
/// <reference path="../../../lib/third-party-libs.d.ts" />

import jsTokens, * as jsTokensNs from "js-tokens";
import type { Token, JSXToken } from "js-tokens";
import jsTokens from "js-tokens";

import {
isStrictReservedWord,
isKeyword,
Expand Down Expand Up @@ -158,9 +159,6 @@ if (process.env.BABEL_8_BREAKING) {
}
};
} else {
// This is only available in js-tokens@4, and not in js-tokens@6
const { matchToToken } = jsTokensNs as any;

/**
* RegExp to test for what seems to be a JSX tag name.
*/
Expand Down Expand Up @@ -204,8 +202,8 @@ if (process.env.BABEL_8_BREAKING) {

tokenize = function* (text: string) {
let match;
while ((match = (jsTokens as any).exec(text))) {
const token = matchToToken(match);
while ((match = (jsTokens as any).default.exec(text))) {
const token = (jsTokens as any).matchToToken(match);

yield {
type: getTokenType(token, match.index, text),
Expand Down

0 comments on commit bb567b1

Please sign in to comment.