Skip to content

Commit

Permalink
Make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 1, 2020
1 parent 4074818 commit 2204c30
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
30 changes: 26 additions & 4 deletions babel.config.js
Expand Up @@ -12,6 +12,8 @@ module.exports = function(api) {
};
const envOpts = Object.assign({}, envOptsNoTargets);

const compileDynamicImport = env === "test" || env === "development";

let convertESM = true;
let ignoreLib = true;
let includeRegeneratorRuntime = false;
Expand Down Expand Up @@ -106,11 +108,10 @@ module.exports = function(api) {
["@babel/plugin-proposal-optional-chaining", { loose: true }],
["@babel/plugin-proposal-nullish-coalescing-operator", { loose: true }],

compileDynamicImport ? dynamicImportUrlToPath : null,
compileDynamicImport ? "@babel/plugin-proposal-dynamic-import" : null,

convertESM ? "@babel/transform-modules-commonjs" : null,
// Until Jest supports native mjs, we must simulate it 馃し
env === "test" || env === "development"
? "@babel/plugin-proposal-dynamic-import"
: null,
].filter(Boolean),
overrides: [
{
Expand Down Expand Up @@ -152,3 +153,24 @@ module.exports = function(api) {

return config;
};

// 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.
// NOTE: This plugin must run before @babel/plugin-transform-modules-commonjs
function dynamicImportUrlToPath({ template }) {
return {
visitor: {
CallExpression(path) {
if (path.get("callee").isImport()) {
path.get("arguments.0").replaceWith(
template.expression.ast`
require("url").fileURLToPath(${path.node.arguments[0]})
`
);
}
},
},
};
}
3 changes: 2 additions & 1 deletion eslint/babel-eslint-parser/test/index.js
@@ -1,4 +1,5 @@
import path from "path";
import { pathToFileURL } from "url";
import escope from "eslint-scope";
import unpad from "dedent";
import { parseForESLint } from "../src";
Expand Down Expand Up @@ -71,7 +72,7 @@ describe("Babel and Espree", () => {
const espreePath = require.resolve("espree", {
paths: [path.dirname(require.resolve("eslint"))],
});
espree = await import(espreePath);
espree = await import(pathToFileURL(espreePath));
});

describe("compatibility", () => {
Expand Down

0 comments on commit 2204c30

Please sign in to comment.