diff --git a/.eslintrc.cjs b/.eslintrc.cjs index a121f4e265ff..24ee25de11ec 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -2,6 +2,8 @@ const path = require("path"); +const cjsGlobals = ["__dirname", "__filename", "require", "module", "exports"]; + module.exports = { root: true, plugins: [ @@ -68,6 +70,25 @@ module.exports = { "import/extensions": ["error", { json: "always", cjs: "always" }], }, }, + { + files: [ + "packages/*/src/**/*.{js,ts}", + "codemods/*/src/**/*.{js,ts}", + "eslint/*/src/**/*.{js,ts}", + "packages/*/test/**/*.js", + "codemods/*/test/**/*.js", + "eslint/*/test/**/*.js", + "packages/babel-helper-transform-fixture-test-runner/src/helpers.{ts,js}", + "test/**/*.js", + ], + excludedFiles: [ + // @babel/register is the require() hook, so it will always be CJS-based + "packages/babel-register/**/*.js", + ], + rules: { + "no-restricted-globals": ["error", ...cjsGlobals], + }, + }, { files: ["packages/babel-plugin-*/src/index.{js,ts}"], excludedFiles: ["packages/babel-plugin-transform-regenerator/**/*.js"], diff --git a/babel.config.js b/babel.config.js index d09102538b47..d6a7cc57cf38 100644 --- a/babel.config.js +++ b/babel.config.js @@ -159,6 +159,7 @@ module.exports = function (api) { convertESM ? "@babel/proposal-export-namespace-from" : null, convertESM ? "@babel/transform-modules-commonjs" : null, convertESM ? pluginNodeImportInterop : null, + convertESM ? pluginImportMetaUrl : null, pluginPackageJsonMacro, @@ -177,14 +178,17 @@ module.exports = function (api) { plugins: ["babel-plugin-transform-charcodes"], assumptions: parserAssumptions, }, - { + convertESM && { test: ["./packages/babel-cli", "./packages/babel-core"].map(normalize), plugins: [ // Explicitly use the lazy version of CommonJS modules. - convertESM - ? ["@babel/transform-modules-commonjs", { lazy: true }] - : null, - ].filter(Boolean), + ["@babel/transform-modules-commonjs", { lazy: true }], + ], + }, + convertESM && { + test: ["./packages/babel-node/src"].map(normalize), + // Used to conditionally import kexec + plugins: ["@babel/plugin-proposal-dynamic-import"], }, { test: sources.map(normalize), @@ -465,3 +469,83 @@ function pluginNodeImportInterop({ template }) { }, }; } + +function pluginImportMetaUrl({ types: t, template }) { + const isImportMeta = node => + t.isMetaProperty(node) && + t.isIdentifier(node.meta, { name: "import" }) && + t.isIdentifier(node.property, { name: "meta" }); + + const isImportMetaUrl = node => + t.isMemberExpression(node, { computed: false }) && + t.isIdentifier(node.property, { name: "url" }) && + isImportMeta(node.object); + + return { + visitor: { + Program(programPath) { + // We must be sure to run this before the instanbul plugins, because its + // instrumentation breaks our detection. + programPath.traverse({ + // fileURLToPath(import.meta.url) + CallExpression(path) { + const { node } = path; + + if ( + !t.isIdentifier(node.callee, { name: "fileURLToPath" }) || + node.arguments.length !== 1 + ) { + return; + } + + const arg = node.arguments[0]; + + if ( + !t.isMemberExpression(arg, { computed: false }) || + !t.isIdentifier(arg.property, { name: "url" }) || + !isImportMeta(arg.object) + ) { + return; + } + + path.replaceWith(t.identifier("__filename")); + }, + + // const require = createRequire(import.meta.url) + VariableDeclarator(path) { + const { node } = path; + + if ( + !t.isIdentifier(node.id, { name: "require" }) || + !t.isCallExpression(node.init) || + !t.isIdentifier(node.init.callee, { name: "createRequire" }) || + node.init.arguments.length !== 1 || + !isImportMetaUrl(node.init.arguments[0]) + ) { + return; + } + + // Let's just remove this declaration to unshadow the "global" cjs require. + path.remove(); + }, + + // import.meta.url + MemberExpression(path) { + if (!isImportMetaUrl(path.node)) return; + + path.replaceWith( + template.expression + .ast`\`file://\${__filename.replace(/\\\\/g, "/")}\`` + ); + }, + + MetaProperty(path) { + if (isImportMeta(path.node)) { + throw path.buildCodeFrameError("Unsupported import.meta"); + } + }, + }); + }, + }, + }; +} diff --git a/codemods/babel-plugin-codemod-object-assign-to-object-spread/test/index.js b/codemods/babel-plugin-codemod-object-assign-to-object-spread/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/codemods/babel-plugin-codemod-object-assign-to-object-spread/test/index.js +++ b/codemods/babel-plugin-codemod-object-assign-to-object-spread/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/codemods/babel-plugin-codemod-optional-catch-binding/test/index.js b/codemods/babel-plugin-codemod-optional-catch-binding/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/codemods/babel-plugin-codemod-optional-catch-binding/test/index.js +++ b/codemods/babel-plugin-codemod-optional-catch-binding/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/eslint/babel-eslint-parser/test/index.js b/eslint/babel-eslint-parser/test/index.js index 350e17ea2e58..e8a2d73b8544 100644 --- a/eslint/babel-eslint-parser/test/index.js +++ b/eslint/babel-eslint-parser/test/index.js @@ -1,10 +1,13 @@ import path from "path"; import escope from "eslint-scope"; import unpad from "dedent"; +import { fileURLToPath } from "url"; +import { createRequire } from "module"; import { parseForESLint } from "../src"; const BABEL_OPTIONS = { - configFile: require.resolve( + configFile: path.resolve( + path.dirname(fileURLToPath(import.meta.url)), "../../babel-eslint-shared-fixtures/config/babel.config.js", ), }; @@ -73,6 +76,8 @@ describe("Babel and Espree", () => { } beforeAll(async () => { + const require = createRequire(import.meta.url); + // Use the version of Espree that is a dependency of // the version of ESLint we are testing against. const espreePath = require.resolve("espree", { diff --git a/eslint/babel-eslint-plugin-development-internal/src/index.js b/eslint/babel-eslint-plugin-development-internal/src/index.js index 8761f3903c07..41efa01891c5 100644 --- a/eslint/babel-eslint-plugin-development-internal/src/index.js +++ b/eslint/babel-eslint-plugin-development-internal/src/index.js @@ -1,7 +1,7 @@ import dryErrorMessages from "./rules/dry-error-messages"; -module.exports = { - rules: { - "dry-error-messages": dryErrorMessages, - }, +export const rules = { + "dry-error-messages": dryErrorMessages, }; + +export default { rules }; diff --git a/eslint/babel-eslint-plugin-development-internal/test/rules/dry-error-messages.js b/eslint/babel-eslint-plugin-development-internal/test/rules/dry-error-messages.js index 7f5ecd284b82..e5fc984d3d9e 100644 --- a/eslint/babel-eslint-plugin-development-internal/test/rules/dry-error-messages.js +++ b/eslint/babel-eslint-plugin-development-internal/test/rules/dry-error-messages.js @@ -1,11 +1,14 @@ import path from "path"; import rule from "../../src/rules/dry-error-messages"; import RuleTester from "../../../babel-eslint-shared-fixtures/utils/RuleTester"; +import { fileURLToPath } from "url"; -const FILENAME = path.resolve(__dirname, "test/lib/index.js"); +const dirname = path.dirname(fileURLToPath(import.meta.url)); + +const FILENAME = path.resolve(dirname, "test/lib/index.js"); const ERRORS_MODULE = "errorsModule"; -const MODULE_SAME_DIR = path.resolve(__dirname, "test/lib/errorsModule.js"); -const MODULE_PARENT_DIR = path.resolve(__dirname, "test/errorsModule.js"); +const MODULE_SAME_DIR = path.resolve(dirname, "test/lib/errorsModule.js"); +const MODULE_PARENT_DIR = path.resolve(dirname, "test/errorsModule.js"); const ruleTester = new RuleTester(); diff --git a/eslint/babel-eslint-plugin-development/src/index.js b/eslint/babel-eslint-plugin-development/src/index.js index 946da90045e2..0dc11be3ed85 100644 --- a/eslint/babel-eslint-plugin-development/src/index.js +++ b/eslint/babel-eslint-plugin-development/src/index.js @@ -2,10 +2,10 @@ import noDeprecatedClone from "./rules/no-deprecated-clone"; import noUndefinedIdentifier from "./rules/no-undefined-identifier"; import pluginName from "./rules/plugin-name"; -module.exports = { - rules: { - "no-deprecated-clone": noDeprecatedClone, - "no-undefined-identifier": noUndefinedIdentifier, - "plugin-name": pluginName, - }, +export const rules = { + "no-deprecated-clone": noDeprecatedClone, + "no-undefined-identifier": noUndefinedIdentifier, + "plugin-name": pluginName, }; + +export default { rules }; diff --git a/eslint/babel-eslint-plugin/src/index.js b/eslint/babel-eslint-plugin/src/index.js index 96fd40caf70e..d2faaa60e79a 100644 --- a/eslint/babel-eslint-plugin/src/index.js +++ b/eslint/babel-eslint-plugin/src/index.js @@ -4,19 +4,20 @@ import noUnusedExpressions from "./rules/no-unused-expressions"; import objectCurlySpacing from "./rules/object-curly-spacing"; import semi from "./rules/semi"; -module.exports = { - rules: { - "new-cap": newCap, - "no-invalid-this": noInvalidThis, - "no-unused-expressions": noUnusedExpressions, - "object-curly-spacing": objectCurlySpacing, - semi, - }, - rulesConfig: { - "new-cap": "off", - "no-invalid-this": "off", - "no-unused-expressions": "off", - "object-curly-spacing": "off", - semi: "off", - }, +export const rules = { + "new-cap": newCap, + "no-invalid-this": noInvalidThis, + "no-unused-expressions": noUnusedExpressions, + "object-curly-spacing": objectCurlySpacing, + semi, }; + +export const rulesConfig = { + "new-cap": "off", + "no-invalid-this": "off", + "no-unused-expressions": "off", + "object-curly-spacing": "off", + semi: "off", +}; + +export default { rules, rulesConfig }; diff --git a/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js b/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js index 609cb8f50b79..b8cec5e450d9 100644 --- a/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js +++ b/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js @@ -1,5 +1,7 @@ import eslint from "eslint"; import unpad from "dedent"; +import path from "path"; +import { fileURLToPath } from "url"; import * as parser from "../../../babel-eslint-parser"; export default function verifyAndAssertMessages( @@ -24,7 +26,8 @@ export default function verifyAndAssertMessages( sourceType, requireConfigFile: false, babelOptions: { - configFile: require.resolve( + configFile: path.resolve( + path.dirname(fileURLToPath(import.meta.url)), "../../../babel-eslint-shared-fixtures/config/babel.config.js", ), }, diff --git a/eslint/babel-eslint-tests/test/integration/eslint-plugin-import.js b/eslint/babel-eslint-tests/test/integration/eslint-plugin-import.js index 28aa3d6ac379..4b5fdc55cb81 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint-plugin-import.js +++ b/eslint/babel-eslint-tests/test/integration/eslint-plugin-import.js @@ -1,12 +1,16 @@ import eslint from "eslint"; import path from "path"; +import { fileURLToPath } from "url"; describe("https://github.com/babel/babel-eslint/issues/558", () => { it("doesn't crash with eslint-plugin-import", () => { const engine = new eslint.CLIEngine({ ignore: false }); engine.executeOnFiles( ["a.js", "b.js", "c.js"].map(file => - path.resolve(__dirname, `../fixtures/eslint-plugin-import/${file}`), + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + `../fixtures/eslint-plugin-import/${file}`, + ), ), ); }); diff --git a/eslint/babel-eslint-tests/test/integration/eslint/config.js b/eslint/babel-eslint-tests/test/integration/eslint/config.js index e82465b86f9b..989e9a8287d0 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/config.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/config.js @@ -1,4 +1,6 @@ import eslint from "eslint"; +import path from "path"; +import { fileURLToPath } from "url"; import * as parser from "@babel/eslint-parser"; describe("ESLint config", () => { @@ -10,7 +12,8 @@ describe("ESLint config", () => { parser: "@babel/eslint-parser", parserOptions: { babelOptions: { - configFile: require.resolve( + configFile: path.resolve( + path.dirname(fileURLToPath(import.meta.url)), "../../../../babel-eslint-shared-fixtures/config/babel.config.js", ), }, diff --git a/eslint/babel-eslint-tests/test/integration/eslint/rules/strict.js b/eslint/babel-eslint-tests/test/integration/eslint/rules/strict.js index 5753a114a320..769ba76d2f29 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/rules/strict.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/rules/strict.js @@ -2,11 +2,17 @@ import eslint from "eslint"; import fs from "fs"; import path from "path"; import * as parser from "../../../../../babel-eslint-parser"; +import { fileURLToPath } from "url"; eslint.linter.defineParser("@babel/eslint-parser", parser); const paths = { - fixtures: path.join(__dirname, "../../..", "fixtures", "rules"), + fixtures: path.join( + path.dirname(fileURLToPath(import.meta.url)), + "../../..", + "fixtures", + "rules", + ), }; const encoding = "utf8"; diff --git a/eslint/babel-eslint-tests/test/integration/eslint/verify.js b/eslint/babel-eslint-tests/test/integration/eslint/verify.js index 6f63452871e9..a025c55fdc58 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/verify.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/verify.js @@ -1,4 +1,6 @@ import verifyAndAssertMessages from "../../helpers/verifyAndAssertMessages"; +import path from "path"; +import { fileURLToPath } from "url"; describe("verify", () => { it("arrow function support (issue #1)", () => { @@ -1080,7 +1082,8 @@ describe("verify", () => { parserOptions: { sourceType, babelOptions: { - configFile: require.resolve( + configFile: path.resolve( + path.dirname(fileURLToPath(import.meta.url)), "../../../../babel-eslint-shared-fixtures/config/babel.config.decorators-legacy.js", ), }, diff --git a/lib/third-party-libs.js.flow b/lib/third-party-libs.js.flow index f114c0bc1ef6..fb405b854c7e 100644 --- a/lib/third-party-libs.js.flow +++ b/lib/third-party-libs.js.flow @@ -2,6 +2,10 @@ * Basic declarations for the npm modules we use. */ + declare module "module" { + declare export function createRequire(url: any): any; + } + declare module "debug" { declare export default (namespace: string) => (formatter: string, ...args: any[]) => void; } diff --git a/package.json b/package.json index ddf457d6bb49..6fa5643296b5 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "@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", diff --git a/packages/babel-cli/src/babel/util.js b/packages/babel-cli/src/babel/util.js index 5dfa6217bca5..70d2c6b48d4b 100644 --- a/packages/babel-cli/src/babel/util.js +++ b/packages/babel-cli/src/babel/util.js @@ -4,6 +4,7 @@ import readdirRecursive from "fs-readdir-recursive"; import * as babel from "@babel/core"; import path from "path"; import fs from "fs"; +import { createRequire } from "module"; export function chmod(src: string, dest: string): void { try { @@ -119,6 +120,9 @@ process.on("uncaughtException", function (err) { }); export function requireChokidar(): Object { + // $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504 + const require = createRequire(import /*::("")*/.meta.url); + try { // todo(babel 8): revert `@nicolo-ribaudo/chokidar-2` hack return parseInt(process.versions.node) >= 8 diff --git a/packages/babel-cli/test/index.js b/packages/babel-cli/test/index.js index a8fc722456cd..0837afd80b0d 100644 --- a/packages/babel-cli/test/index.js +++ b/packages/babel-cli/test/index.js @@ -1,17 +1,23 @@ -const readdir = require("fs-readdir-recursive"); -const helper = require("@babel/helper-fixtures"); -const rimraf = require("rimraf"); -const { sync: makeDirSync } = require("make-dir"); -const child = require("child_process"); -const escapeRegExp = require("lodash/escapeRegExp"); -const merge = require("lodash/merge"); -const path = require("path"); -const fs = require("fs"); -const { chmod } = require("../lib/babel/util"); - -const fixtureLoc = path.join(__dirname, "fixtures"); -const tmpLoc = path.join(__dirname, "tmp"); -const rootDir = path.resolve(__dirname, "../../.."); +import readdir from "fs-readdir-recursive"; +import * as helper from "@babel/helper-fixtures"; +import rimraf from "rimraf"; +import { sync as makeDirSync } from "make-dir"; +import child from "child_process"; +import escapeRegExp from "lodash/escapeRegExp"; +import merge from "lodash/merge"; +import path from "path"; +import fs from "fs"; +import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +import { chmod } from "../lib/babel/util"; + +const require = createRequire(import.meta.url); + +const dirname = path.dirname(fileURLToPath(import.meta.url)); +const fixtureLoc = path.join(dirname, "fixtures"); +const tmpLoc = path.join(dirname, "tmp"); +const rootDir = path.resolve(dirname, "../../.."); const fileFilter = function (x) { return x !== ".DS_Store"; @@ -131,7 +137,7 @@ const assertTest = function (stdout, stderr, opts, cwd) { }; const buildTest = function (binName, testName, opts) { - const binLoc = path.join(__dirname, "../lib", binName); + const binLoc = path.join(dirname, "../lib", binName); return function (callback) { saveInFiles(opts.inFiles); diff --git a/packages/babel-core/src/config/files/configuration.js b/packages/babel-core/src/config/files/configuration.js index 45162669757e..fe3da4593ad3 100644 --- a/packages/babel-core/src/config/files/configuration.js +++ b/packages/babel-core/src/config/files/configuration.js @@ -18,6 +18,10 @@ import type { CallerMetadata } from "../validation/options"; import * as fs from "../../gensync-utils/fs"; +import { createRequire } from "module"; +// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504 +const require = createRequire(import /*::("")*/.meta.url); + const debug = buildDebug("babel:config:loading:files:configuration"); export const ROOT_CONFIG_FILENAMES = [ diff --git a/packages/babel-core/src/config/files/module-types.js b/packages/babel-core/src/config/files/module-types.js index 0e6d4a431886..de75ada49337 100644 --- a/packages/babel-core/src/config/files/module-types.js +++ b/packages/babel-core/src/config/files/module-types.js @@ -2,6 +2,9 @@ import { isAsync, waitFor } from "../../gensync-utils/async"; import type { Handler } from "gensync"; import path from "path"; import { pathToFileURL } from "url"; +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); let import_; try { diff --git a/packages/babel-core/src/config/files/plugins.js b/packages/babel-core/src/config/files/plugins.js index 747592cad9bd..4e2f4ba88e27 100644 --- a/packages/babel-core/src/config/files/plugins.js +++ b/packages/babel-core/src/config/files/plugins.js @@ -9,6 +9,10 @@ import path from "path"; import { type Handler } from "gensync"; import loadCjsOrMjsDefault from "./module-types"; +import { createRequire } from "module"; +// $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504 +const require = createRequire(import /*::("")*/.meta.url); + const debug = buildDebug("babel:config:loading:files:plugins"); const EXACT_RE = /^module:/; diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 8f09a707c48d..ed58229050ac 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -3,6 +3,13 @@ import sourceMap from "source-map"; import path from "path"; import Plugin from "../lib/config/plugin"; import generator from "@babel/generator"; +import { fileURLToPath } from "url"; + +import presetEnv from "../../babel-preset-env"; +import pluginSyntaxFlow from "../../babel-plugin-syntax-flow"; +import pluginFlowStripTypes from "../../babel-plugin-transform-flow-strip-types"; + +const cwd = path.dirname(fileURLToPath(import.meta.url)); function assertIgnored(result) { expect(result).toBeNull(); @@ -13,54 +20,26 @@ function assertNotIgnored(result) { } function parse(code, opts) { - return babel.parse(code, { - cwd: __dirname, - configFile: false, - ...opts, - }); + return babel.parse(code, { cwd, configFile: false, ...opts }); } function transform(code, opts) { - return babel.transform(code, { - cwd: __dirname, - configFile: false, - ...opts, - }); + return babel.transform(code, { cwd, configFile: false, ...opts }); } function transformFile(filename, opts, cb) { - return babel.transformFile( - filename, - { - cwd: __dirname, - configFile: false, - ...opts, - }, - cb, - ); + return babel.transformFile(filename, { cwd, configFile: false, ...opts }, cb); } function transformFileSync(filename, opts) { - return babel.transformFileSync(filename, { - cwd: __dirname, - configFile: false, - ...opts, - }); + return babel.transformFileSync(filename, { cwd, configFile: false, ...opts }); } function transformAsync(code, opts) { - return babel.transformAsync(code, { - cwd: __dirname, - configFile: false, - ...opts, - }); + return babel.transformAsync(code, { cwd, configFile: false, ...opts }); } function transformFromAst(ast, code, opts) { - return babel.transformFromAst(ast, code, { - cwd: __dirname, - configFile: false, - ...opts, - }); + return babel.transformFromAst(ast, code, { cwd, configFile: false, ...opts }); } describe("parser and generator options", function () { @@ -111,7 +90,7 @@ describe("parser and generator options", function () { function newTransformWithPlugins(string) { return transform(string, { ast: true, - plugins: [__dirname + "/../../babel-plugin-syntax-flow"], + plugins: [cwd + "/../../babel-plugin-syntax-flow"], parserOpts: { parser: recast.parse, }, @@ -173,17 +152,13 @@ describe("api", function () { babelrc: false, }; Object.freeze(options); - transformFile( - __dirname + "/fixtures/api/file.js", - options, - function (err, res) { - if (err) return done(err); - expect(res.code).toBe("foo();"); - // keep user options untouched - expect(options).toEqual({ babelrc: false }); - done(); - }, - ); + transformFile(cwd + "/fixtures/api/file.js", options, function (err, res) { + if (err) return done(err); + expect(res.code).toBe("foo();"); + // keep user options untouched + expect(options).toEqual({ babelrc: false }); + done(); + }); }); it("transformFileSync", function () { @@ -191,9 +166,9 @@ describe("api", function () { babelrc: false, }; Object.freeze(options); - expect( - transformFileSync(__dirname + "/fixtures/api/file.js", options).code, - ).toBe("foo();"); + expect(transformFileSync(cwd + "/fixtures/api/file.js", options).code).toBe( + "foo();", + ); expect(options).toEqual({ babelrc: false }); }); @@ -249,15 +224,15 @@ describe("api", function () { it("options throw on falsy true", function () { return expect(function () { transform("", { - plugins: [__dirname + "/../../babel-plugin-syntax-jsx", false], + plugins: [cwd + "/../../babel-plugin-syntax-jsx", false], }); }).toThrow(/.plugins\[1\] must be a string, object, function/); }); it("options merge backwards", function () { return transformAsync("", { - presets: [__dirname + "/../../babel-preset-env"], - plugins: [__dirname + "/../../babel-plugin-syntax-jsx"], + presets: [cwd + "/../../babel-preset-env"], + plugins: [cwd + "/../../babel-plugin-syntax-jsx"], }).then(function (result) { expect(result.options.plugins[0].manipulateOptions.toString()).toEqual( expect.stringContaining("jsx"), @@ -336,18 +311,12 @@ describe("api", function () { }, // env preset - require(__dirname + "/../../babel-preset-env"), + presetEnv, // Third preset for Flow. - function () { - return { - plugins: [ - require(__dirname + "/../../babel-plugin-syntax-flow"), - require(__dirname + - "/../../babel-plugin-transform-flow-strip-types"), - ], - }; - }, + () => ({ + plugins: [pluginSyntaxFlow, pluginFlowStripTypes], + }), ], }); } @@ -393,9 +362,9 @@ describe("api", function () { process.env.BABEL_ENV = "development"; const result = transform("", { - cwd: path.join(__dirname, "fixtures", "config", "complex-plugin-config"), + cwd: path.join(cwd, "fixtures", "config", "complex-plugin-config"), filename: path.join( - __dirname, + cwd, "fixtures", "config", "complex-plugin-config", @@ -792,7 +761,7 @@ describe("api", function () { it("only syntax plugin available", function (done) { transformFile( - __dirname + "/fixtures/api/parsing-errors/only-syntax/file.js", + cwd + "/fixtures/api/parsing-errors/only-syntax/file.js", options, function (err) { expect(err.message).toMatch( @@ -809,7 +778,7 @@ describe("api", function () { it("both syntax and transform plugin available", function (done) { transformFile( - __dirname + "/fixtures/api/parsing-errors/syntax-and-transform/file.js", + cwd + "/fixtures/api/parsing-errors/syntax-and-transform/file.js", options, function (err) { expect(err.message).toMatch( diff --git a/packages/babel-core/test/assumptions.js b/packages/babel-core/test/assumptions.js index 322e728d6eea..bf97c3bbb6af 100644 --- a/packages/babel-core/test/assumptions.js +++ b/packages/babel-core/test/assumptions.js @@ -1,7 +1,11 @@ +import path from "path"; +import { fileURLToPath } from "url"; import { loadOptions as loadOptionsOrig, transformSync } from "../lib"; +const cwd = path.dirname(fileURLToPath(import.meta.url)); + function loadOptions(opts) { - return loadOptionsOrig({ cwd: __dirname, ...opts }); + return loadOptionsOrig({ cwd, ...opts }); } function withAssumptions(assumptions) { diff --git a/packages/babel-core/test/async.js b/packages/babel-core/test/async.js index 2dfafe1617f2..d7996c10baf3 100644 --- a/packages/babel-core/test/async.js +++ b/packages/babel-core/test/async.js @@ -1,4 +1,5 @@ -import { join } from "path"; +import path from "path"; +import { fileURLToPath } from "url"; import * as babel from ".."; import { @@ -14,7 +15,11 @@ const nodeGte8 = (...args) => { }; describe("asynchronicity", () => { - const base = join(__dirname, "fixtures", "async"); + const base = path.join( + path.dirname(fileURLToPath(import.meta.url)), + "fixtures", + "async", + ); let cwd; beforeEach(function () { diff --git a/packages/babel-core/test/config-chain.js b/packages/babel-core/test/config-chain.js index f710d347ed9e..7e345fd013bd 100644 --- a/packages/babel-core/test/config-chain.js +++ b/packages/babel-core/test/config-chain.js @@ -1,9 +1,12 @@ import fs from "fs"; import os from "os"; import path from "path"; +import { fileURLToPath } from "url"; import escapeRegExp from "lodash/escapeRegExp"; import * as babel from "../lib"; +const dirname = path.dirname(fileURLToPath(import.meta.url)); + import { isMJS, loadOptionsAsync, skipUnsupportedESM } from "./helpers/esm"; // TODO: In Babel 8, we can directly uses fs.promises which is supported by @@ -42,11 +45,11 @@ const pfs = }); function fixture(...args) { - return path.join(__dirname, "fixtures", "config", ...args); + return path.join(dirname, "fixtures", "config", ...args); } function loadOptions(opts) { - return babel.loadOptions({ cwd: __dirname, ...opts }); + return babel.loadOptions({ cwd: dirname, ...opts }); } function pairs(items) { @@ -1320,7 +1323,7 @@ describe("buildConfigChain", function () { it("should throw when `preset` requires `filename` but it was not passed", () => { expect(() => { loadOptions({ - presets: [require("./fixtures/config-loading/preset4")], + presets: ["./fixtures/config-loading/preset4"], }); }).toThrow(/Preset \/\* your preset \*\/ requires a filename/); }); @@ -1328,7 +1331,7 @@ describe("buildConfigChain", function () { it("should throw when `preset.overrides` requires `filename` but it was not passed", () => { expect(() => { loadOptions({ - presets: [require("./fixtures/config-loading/preset5")], + presets: ["./fixtures/config-loading/preset5"], }); }).toThrow(/Preset \/\* your preset \*\/ requires a filename/); }); diff --git a/packages/babel-core/test/config-loading.js b/packages/babel-core/test/config-loading.js index 9968dc2c08e3..da84e590d420 100644 --- a/packages/babel-core/test/config-loading.js +++ b/packages/babel-core/test/config-loading.js @@ -3,12 +3,16 @@ import loadConfigRunner, { createConfigItem, } from "../lib/config"; import path from "path"; +import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); const loadConfig = loadConfigRunner.sync; describe("@babel/core config loading", () => { const FILEPATH = path.join( - __dirname, + path.dirname(fileURLToPath(import.meta.url)), "fixtures", "config-loading", "folder", diff --git a/packages/babel-core/test/helpers/esm.js b/packages/babel-core/test/helpers/esm.js index 23f70eb097ca..c485f1fa8422 100644 --- a/packages/babel-core/test/helpers/esm.js +++ b/packages/babel-core/test/helpers/esm.js @@ -1,8 +1,14 @@ import cp from "child_process"; import util from "util"; import path from "path"; +import { fileURLToPath } from "url"; +import { createRequire } from "module"; + import * as babel from "../../lib"; +const require = createRequire(import.meta.url); +const dirname = path.dirname(fileURLToPath(import.meta.url)); + // "minNodeVersion": "10.0.0" <-- For Ctrl+F when dropping node 10 const nodeSupportsESM = parseInt(process.versions.node) >= 12; const isWindows = process.platform === "win32"; @@ -30,7 +36,7 @@ export function skipUnsupportedESM(esm, name) { return false; } -export function loadOptionsAsync({ filename, cwd = __dirname }, mjs) { +export function loadOptionsAsync({ filename, cwd = dirname }, mjs) { if (mjs) { // import() crashes with jest return spawn("load-options-async", filename, cwd); diff --git a/packages/babel-core/test/option-manager.js b/packages/babel-core/test/option-manager.js index 75698baec824..3bdd98beb44d 100644 --- a/packages/babel-core/test/option-manager.js +++ b/packages/babel-core/test/option-manager.js @@ -1,11 +1,11 @@ import { loadOptions as loadOptionsOrig } from "../lib"; import path from "path"; +import { fileURLToPath } from "url"; + +const cwd = path.dirname(fileURLToPath(import.meta.url)); function loadOptions(opts) { - return loadOptionsOrig({ - cwd: __dirname, - ...opts, - }); + return loadOptionsOrig({ cwd, ...opts }); } describe("option-manager", () => { @@ -220,9 +220,7 @@ describe("option-manager", () => { it("throws for resolved but erroring preset", () => { return expect(() => { loadOptions({ - presets: [ - path.join(__dirname, "fixtures/option-manager/not-a-preset"), - ], + presets: [path.join(cwd, "fixtures/option-manager/not-a-preset")], }); }).toThrow( /While processing: .*option-manager(?:\/|\\\\)not-a-preset\.js/, @@ -234,9 +232,7 @@ describe("option-manager", () => { function presetTest(name) { it(name, function () { const options = loadOptions({ - presets: [ - path.join(__dirname, "fixtures/option-manager/presets", name), - ], + presets: [path.join(cwd, "fixtures/option-manager/presets", name)], }); expect(Array.isArray(options.plugins)).toBe(true); @@ -249,9 +245,7 @@ describe("option-manager", () => { it(name, function () { expect(() => loadOptions({ - presets: [ - path.join(__dirname, "fixtures/option-manager/presets", name), - ], + presets: [path.join(cwd, "fixtures/option-manager/presets", name)], }), ).toThrow(msg); }); diff --git a/packages/babel-core/test/parse.js b/packages/babel-core/test/parse.js index 5e1956604970..b22ff86b4a90 100644 --- a/packages/babel-core/test/parse.js +++ b/packages/babel-core/test/parse.js @@ -1,9 +1,18 @@ import fs from "fs"; import path from "path"; import { parse } from "../lib"; +import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); function fixture(...args) { - return path.join(__dirname, "fixtures", "parse", ...args); + return path.join( + path.dirname(fileURLToPath(import.meta.url)), + "fixtures", + "parse", + ...args, + ); } describe("parse", function () { diff --git a/packages/babel-core/test/path.js b/packages/babel-core/test/path.js index 2edb48921b07..416bb4435790 100644 --- a/packages/babel-core/test/path.js +++ b/packages/babel-core/test/path.js @@ -1,12 +1,16 @@ import { transform } from "../lib/index"; import Plugin from "../lib/config/plugin"; +import { fileURLToPath } from "url"; +import path from "path"; + +const cwd = path.dirname(fileURLToPath(import.meta.url)); describe("traversal path", function () { it("replaceWithSourceString", function () { const expectCode = "function foo() {}"; const actualCode = transform(expectCode, { - cwd: __dirname, + cwd, plugins: [ new Plugin({ visitor: { @@ -25,7 +29,7 @@ describe("traversal path", function () { const expectCode = "var fn = () => true;"; const actualCode = transform(expectCode, { - cwd: __dirname, + cwd, plugins: [ new Plugin({ visitor: { @@ -55,7 +59,7 @@ describe("traversal path", function () { const expectCode = "var fn = () => { return true; }"; const actualCode = transform(expectCode, { - cwd: __dirname, + cwd, plugins: [ new Plugin({ visitor: { @@ -77,7 +81,7 @@ describe("traversal path", function () { const expectCode = "for (KEY in right);"; const actualCode = transform(expectCode, { - cwd: __dirname, + cwd, plugins: [ new Plugin({ visitor: { @@ -108,7 +112,7 @@ describe("traversal path", function () { const expectCode = "for (var KEY in right);"; const actualCode = transform(expectCode, { - cwd: __dirname, + cwd, plugins: [ new Plugin({ visitor: { @@ -130,7 +134,7 @@ describe("traversal path", function () { const expectCode = "for (KEY;;);"; const actualCode = transform(expectCode, { - cwd: __dirname, + cwd, plugins: [ new Plugin({ visitor: { @@ -161,7 +165,7 @@ describe("traversal path", function () { const expectCode = "for (var KEY;;);"; const actualCode = transform(expectCode, { - cwd: __dirname, + cwd, plugins: [ new Plugin({ visitor: { diff --git a/packages/babel-core/test/plugins.js b/packages/babel-core/test/plugins.js index 54d3623798e7..a7851bde5c3d 100644 --- a/packages/babel-core/test/plugins.js +++ b/packages/babel-core/test/plugins.js @@ -1,3 +1,8 @@ import runner from "@babel/helper-transform-fixture-test-runner"; +import { fileURLToPath } from "url"; +import path from "path"; -runner(`${__dirname}/fixtures/plugins`, "plugins"); +runner( + path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures/plugins"), + "plugins", +); diff --git a/packages/babel-core/test/resolution.js b/packages/babel-core/test/resolution.js index 3fc2c3a2bcad..191e5436a01b 100644 --- a/packages/babel-core/test/resolution.js +++ b/packages/babel-core/test/resolution.js @@ -1,8 +1,13 @@ import * as babel from "../lib/index"; import path from "path"; +import { fileURLToPath } from "url"; describe("addon resolution", function () { - const base = path.join(__dirname, "fixtures", "resolution"); + const base = path.join( + path.dirname(fileURLToPath(import.meta.url)), + "fixtures", + "resolution", + ); let cwd; beforeEach(function () { diff --git a/packages/babel-core/test/targets.js b/packages/babel-core/test/targets.js index 00214c9b7f06..c90e921df3e5 100644 --- a/packages/babel-core/test/targets.js +++ b/packages/babel-core/test/targets.js @@ -1,8 +1,11 @@ import { loadOptions as loadOptionsOrig } from "../lib"; -import { join } from "path"; +import { join, dirname } from "path"; +import { fileURLToPath } from "url"; + +const cwd = dirname(fileURLToPath(import.meta.url)); function loadOptions(opts) { - return loadOptionsOrig({ cwd: __dirname, ...opts }); + return loadOptionsOrig({ cwd, ...opts }); } function withTargets(targets) { @@ -83,7 +86,7 @@ describe("browserslist", () => { it("loads .browserslistrc by default", () => { expect( loadOptions({ - cwd: join(__dirname, "fixtures", "targets"), + cwd: join(cwd, "fixtures", "targets"), }).targets, ).toEqual({ chrome: "80.0.0" }); }); @@ -91,7 +94,7 @@ describe("browserslist", () => { it("loads .browserslistrc relative to the input file", () => { expect( loadOptions({ - cwd: join(__dirname, "fixtures", "targets"), + cwd: join(cwd, "fixtures", "targets"), filename: "./nested/test.js", }).targets, ).toEqual({ edge: "14.0.0" }); @@ -101,7 +104,7 @@ describe("browserslist", () => { it("can disable config loading", () => { expect( loadOptions({ - cwd: join(__dirname, "fixtures", "targets"), + cwd: join(cwd, "fixtures", "targets"), browserslistConfigFile: false, }).targets, ).toEqual({}); @@ -110,7 +113,7 @@ describe("browserslist", () => { it("can specify a custom file", () => { expect( loadOptions({ - cwd: join(__dirname, "fixtures", "targets"), + cwd: join(cwd, "fixtures", "targets"), browserslistConfigFile: "./.browserslistrc-firefox", }).targets, ).toEqual({ firefox: "74.0.0" }); @@ -119,7 +122,7 @@ describe("browserslist", () => { it("is relative to the project root", () => { expect( loadOptions({ - cwd: join(__dirname, "fixtures", "targets"), + cwd: join(cwd, "fixtures", "targets"), root: "..", filename: "./nested/test.js", browserslistConfigFile: "./targets/.browserslistrc-firefox", @@ -132,7 +135,7 @@ describe("browserslist", () => { it("is forwarded to browserslist", () => { expect( loadOptions({ - cwd: join(__dirname, "fixtures", "targets"), + cwd: join(cwd, "fixtures", "targets"), browserslistEnv: "browserslist-loading-test", }).targets, ).toEqual({ chrome: "70.0.0" }); diff --git a/packages/babel-core/test/transformation.js b/packages/babel-core/test/transformation.js index 196beec3830a..f4eb1ef790a4 100644 --- a/packages/babel-core/test/transformation.js +++ b/packages/babel-core/test/transformation.js @@ -1,3 +1,11 @@ import runner from "@babel/helper-transform-fixture-test-runner"; +import { fileURLToPath } from "url"; +import path from "path"; -runner(`${__dirname}/fixtures/transformation`, "transformation"); +runner( + path.join( + path.dirname(fileURLToPath(import.meta.url)), + "/fixtures/transformation", + ), + "transformation", +); diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index 8c597b818db7..adbee798e783 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -6,6 +6,7 @@ import fs from "fs"; import path from "path"; import fixtures from "@babel/helper-fixtures"; import sourcemap from "source-map"; +import { fileURLToPath } from "url"; describe("generation", function () { it("completeness", function () { @@ -758,7 +759,9 @@ describe("CodeGenerator", function () { }); }); -const suites = fixtures(`${__dirname}/fixtures`); +const suites = fixtures( + path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"), +); suites.forEach(function (testSuite) { describe("generation/" + testSuite.title, function () { @@ -783,7 +786,10 @@ suites.forEach(function (testSuite) { ...task.options.parserOpts, }); const options = { - sourceFileName: path.relative(__dirname, actual.loc), + sourceFileName: path.relative( + path.dirname(fileURLToPath(import.meta.url)), + actual.loc, + ), ...task.options, sourceMaps: task.sourceMap ? true : task.options.sourceMaps, }; diff --git a/packages/babel-helper-compilation-targets/test/custom-browserslist-env/custom-browserslist-env.spec.js b/packages/babel-helper-compilation-targets/test/custom-browserslist-env/custom-browserslist-env.spec.js index 953d510a07c8..4fa8a4a304ba 100644 --- a/packages/babel-helper-compilation-targets/test/custom-browserslist-env/custom-browserslist-env.spec.js +++ b/packages/babel-helper-compilation-targets/test/custom-browserslist-env/custom-browserslist-env.spec.js @@ -1,9 +1,14 @@ import getTargets from "../.."; +import { fileURLToPath } from "url"; +import path from "path"; it("allows custom browserslist env", () => { const actual = getTargets( {}, - { configPath: __dirname, browserslistEnv: "custom" }, + { + configPath: path.dirname(fileURLToPath(import.meta.url)), + browserslistEnv: "custom", + }, ); expect(actual).toEqual({ ie: "11.0.0" }); diff --git a/packages/babel-helper-compilation-targets/test/targets-parser.spec.js b/packages/babel-helper-compilation-targets/test/targets-parser.spec.js index e9c78872a0de..fbef769ccd42 100644 --- a/packages/babel-helper-compilation-targets/test/targets-parser.spec.js +++ b/packages/babel-helper-compilation-targets/test/targets-parser.spec.js @@ -1,5 +1,6 @@ import browserslist from "browserslist"; -import { join } from "path"; +import { join, dirname } from "path"; +import { fileURLToPath } from "url"; import getTargets from ".."; describe("getTargets", () => { @@ -250,7 +251,13 @@ describe("getTargets", () => { { esmodules: "intersect", }, - { configPath: join(__dirname, "fixtures", "foo.js") }, + { + configPath: join( + dirname(fileURLToPath(import.meta.url)), + "fixtures", + "foo.js", + ), + }, ), ).toMatchSnapshot(); }); diff --git a/packages/babel-helper-compilation-targets/test/targets-supported.js b/packages/babel-helper-compilation-targets/test/targets-supported.js index fd018172ef29..aa4a2085de80 100644 --- a/packages/babel-helper-compilation-targets/test/targets-supported.js +++ b/packages/babel-helper-compilation-targets/test/targets-supported.js @@ -1,6 +1,4 @@ -"use strict"; - -const { targetsSupported } = require("../lib/filter-items"); +import { targetsSupported } from "../lib/filter-items"; describe("targetsSupported", () => { const MAX_VERSION = `${Number.MAX_SAFE_INTEGER}.0.0`; diff --git a/packages/babel-helper-create-class-features-plugin/test/index.js b/packages/babel-helper-create-class-features-plugin/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-helper-create-class-features-plugin/test/index.js +++ b/packages/babel-helper-create-class-features-plugin/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-helper-create-regexp-features-plugin/test/index.js b/packages/babel-helper-create-regexp-features-plugin/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-helper-create-regexp-features-plugin/test/index.js +++ b/packages/babel-helper-create-regexp-features-plugin/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-helper-fixtures/src/index.ts b/packages/babel-helper-fixtures/src/index.ts index 6c904c7965ba..2f8beb723497 100644 --- a/packages/babel-helper-fixtures/src/index.ts +++ b/packages/babel-helper-fixtures/src/index.ts @@ -2,6 +2,10 @@ import cloneDeep from "lodash/cloneDeep"; import semver from "semver"; import path from "path"; import fs from "fs"; +import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); const nodeVersion = semver.clean(process.version.slice(1)); @@ -279,7 +283,11 @@ function wrapPackagesArray(type, names, optionsDir) { val[0] = path.resolve(optionsDir, val[0]); } else { - const monorepoPath = __dirname + "/../../babel-" + type + "-" + val[0]; + const monorepoPath = path.join( + path.dirname(fileURLToPath(import.meta.url)), + "../..", + `babel-${type}-${val[0]}`, + ); if (fs.existsSync(monorepoPath)) { val[0] = monorepoPath; diff --git a/packages/babel-helper-module-imports/test/index.js b/packages/babel-helper-module-imports/test/index.js index 7f6a2206c16d..c1ef591d8aed 100644 --- a/packages/babel-helper-module-imports/test/index.js +++ b/packages/babel-helper-module-imports/test/index.js @@ -1,7 +1,11 @@ import * as babel from "@babel/core"; +import { fileURLToPath } from "url"; +import path from "path"; import { ImportInjector } from "../"; +const cwd = path.dirname(fileURLToPath(import.meta.url)); + function test(sourceType, opts, initializer, inputCode, expectedCode) { if (typeof opts === "function") { expectedCode = inputCode; @@ -15,7 +19,7 @@ function test(sourceType, opts, initializer, inputCode, expectedCode) { } const result = babel.transform(inputCode, { - cwd: __dirname, + cwd, sourceType, filename: "example" + (sourceType === "module" ? ".mjs" : ".js"), babelrc: false, diff --git a/packages/babel-helper-plugin-test-runner/src/index.js b/packages/babel-helper-plugin-test-runner/src/index.js index b7cd1cef6a95..44e5a7d56f32 100644 --- a/packages/babel-helper-plugin-test-runner/src/index.js +++ b/packages/babel-helper-plugin-test-runner/src/index.js @@ -1,7 +1,24 @@ import testRunner from "@babel/helper-transform-fixture-test-runner"; import path from "path"; +import { URL } from "url"; export default function (loc) { - const name = path.basename(path.dirname(loc)); - testRunner(loc + "/fixtures", name); + if (!process.env.BABEL_8_BREAKING) { + if (!loc.startsWith("file://")) { + const name = path.basename(path.dirname(loc)); + testRunner(loc + "/fixtures", name); + return; + } + } + + let fixtures = new URL("./fixtures", loc).pathname; + if (process.platform === "win32") { + // Remove the leading / before the drive letter + // TODO: After dropping Node.js 10 support, use fileURLToPath + fixtures = fixtures.slice(1); + } + + const name = path.basename(new URL("..", loc).pathname); + + testRunner(fixtures, name); } diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.js b/packages/babel-helper-transform-fixture-test-runner/src/index.js index 511df65feca0..146f1ae90454 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.js @@ -15,6 +15,10 @@ import path from "path"; import vm from "vm"; import QuickLRU from "quick-lru"; import escapeRegExp from "./escape-regexp.cjs"; +import { fileURLToPath } from "url"; + +import { createRequire } from "module"; +const require = createRequire(import.meta.url); import _checkDuplicatedNodes from "babel-check-duplicated-nodes"; const checkDuplicatedNodes = _checkDuplicatedNodes.default; @@ -43,14 +47,17 @@ function createContext() { // global creation in tests, which could cause things to bleed between tests. runModuleInTestContext( "regenerator-runtime", - __filename, + fileURLToPath(import.meta.url), context, moduleCache, ); // Populate the "babelHelpers" global with Babel's helper utilities. runCacheableScriptInTestContext( - path.join(__dirname, "babel-helpers-in-memory.js"), + path.join( + path.dirname(fileURLToPath(import.meta.url)), + "babel-helpers-in-memory.js", + ), buildExternalHelpers, context, moduleCache, @@ -330,7 +337,10 @@ function validateFile(actualCode, expectedLoc, expectedCode) { } function normalizeOutput(code, normalizePathSeparator) { - const projectRoot = path.resolve(__dirname, "../../../"); + const projectRoot = path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + "../../../", + ); const cwdSymbol = ""; let result = code .trim() diff --git a/packages/babel-helper-transform-fixture-test-runner/test/index.js b/packages/babel-helper-transform-fixture-test-runner/test/index.js index bc872cfa5183..a13d6687ee66 100644 --- a/packages/babel-helper-transform-fixture-test-runner/test/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/test/index.js @@ -1,4 +1,7 @@ import { runCodeInTestContext } from ".."; +import { fileURLToPath } from "url"; + +const filename = fileURLToPath(import.meta.url); describe("helper-transform-fixture-test-runner", function () { it("should not execute code in Node's global context", function () { @@ -10,7 +13,7 @@ describe("helper-transform-fixture-test-runner", function () { global.foo = "inner"; `, { - filename: `${__filename}.fake1`, + filename: `${filename}.fake1`, }, ); @@ -20,7 +23,7 @@ describe("helper-transform-fixture-test-runner", function () { expect(global.foo).toBe("inner"); `, { - filename: `${__filename}.fake2`, + filename: `${filename}.fake2`, }, ); } finally { @@ -30,14 +33,14 @@ describe("helper-transform-fixture-test-runner", function () { delete global.foo; `, { - filename: `${__filename}.fake3`, + filename: `${filename}.fake3`, }, ); } }); it("should print correct trace position when error is thrown in the first line", () => { const opts = { - filename: `${__filename}.fake4`, + filename: `${filename}.fake4`, }; runCodeInTestContext( `try { throw new Error() } catch (e) { diff --git a/packages/babel-helpers/test/index.js b/packages/babel-helpers/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-helpers/test/index.js +++ b/packages/babel-helpers/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-node/src/_babel-node.js b/packages/babel-node/src/_babel-node.js index 19735a5a00c9..9792d975831a 100644 --- a/packages/babel-node/src/_babel-node.js +++ b/packages/babel-node/src/_babel-node.js @@ -8,6 +8,10 @@ import vm from "vm"; import "core-js/stable"; import "regenerator-runtime/runtime"; import register from "@babel/register"; +import { fileURLToPath } from "url"; + +import { createRequire } from "module"; +const require = createRequire(import.meta.url); const program = new commander.Command("babel-node"); @@ -194,7 +198,7 @@ if (program.eval || program.print) { // add back on node and concat the sliced args process.argv = ["node"].concat(args); - process.execArgv.push(__filename); + process.execArgv.push(fileURLToPath(import.meta.url)); Module.runMain(); } else { diff --git a/packages/babel-node/src/babel-node.js b/packages/babel-node/src/babel-node.js index fa0b0eca4fc2..bbd64140b970 100755 --- a/packages/babel-node/src/babel-node.js +++ b/packages/babel-node/src/babel-node.js @@ -5,8 +5,12 @@ import getV8Flags from "v8flags"; import path from "path"; +import child_process from "child_process"; +import { fileURLToPath } from "url"; -let args = [path.join(__dirname, "_babel-node")]; +let args = [ + path.join(path.dirname(fileURLToPath(import.meta.url)), "_babel-node"), +]; let babelArgs = process.argv.slice(2); let userArgs; @@ -41,7 +45,7 @@ const aliases = new Map([ ["-gc", "--expose-gc"], ]); -getV8Flags(function (err, v8Flags) { +getV8Flags(async function (err, v8Flags) { for (let i = 0; i < babelArgs.length; i++) { const arg = babelArgs[i]; const flag = arg.split("=")[0]; @@ -69,17 +73,17 @@ getV8Flags(function (err, v8Flags) { } try { - const kexec = require("kexec"); + const { default: kexec } = await import("kexec"); kexec(process.argv[0], args); } catch (err) { if ( + err.code !== "ERR_MODULE_NOT_FOUND" && err.code !== "MODULE_NOT_FOUND" && err.code !== "UNDECLARED_DEPENDENCY" ) { throw err; } - const child_process = require("child_process"); const proc = child_process.spawn(process.argv[0], args, { stdio: ["inherit", "inherit", "inherit", "ipc"], }); diff --git a/packages/babel-node/test/index.js b/packages/babel-node/test/index.js index ca4571dfb0be..f9fcdbb6d88d 100644 --- a/packages/babel-node/test/index.js +++ b/packages/babel-node/test/index.js @@ -1,15 +1,20 @@ -const includes = require("lodash/includes"); -const readdir = require("fs-readdir-recursive"); -const helper = require("@babel/helper-fixtures"); -const rimraf = require("rimraf"); -const { sync: makeDirSync } = require("make-dir"); -const child = require("child_process"); -const merge = require("lodash/merge"); -const path = require("path"); -const fs = require("fs"); - -const fixtureLoc = path.join(__dirname, "fixtures"); -const tmpLoc = path.join(__dirname, "tmp"); +import includes from "lodash/includes"; +import readdir from "fs-readdir-recursive"; +import * as helper from "@babel/helper-fixtures"; +import rimraf from "rimraf"; +import { sync as makeDirSync } from "make-dir"; +import child from "child_process"; +import merge from "lodash/merge"; +import path from "path"; +import fs from "fs"; +import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); + +const dirname = path.dirname(fileURLToPath(import.meta.url)); +const fixtureLoc = path.join(dirname, "fixtures"); +const tmpLoc = path.join(dirname, "tmp"); const fileFilter = function (x) { return x !== ".DS_Store"; @@ -91,7 +96,7 @@ const assertTest = function (stdout, stderr, opts) { }; const buildTest = function (binName, testName, opts) { - const binLoc = path.join(__dirname, "../lib", binName); + const binLoc = path.join(dirname, "../lib", binName); return function (callback) { saveInFiles(opts.inFiles); diff --git a/packages/babel-parser/test/estree-throws.js b/packages/babel-parser/test/estree-throws.js index 8eeae5dbb515..c2f45bef2314 100644 --- a/packages/babel-parser/test/estree-throws.js +++ b/packages/babel-parser/test/estree-throws.js @@ -1,5 +1,9 @@ import path from "path"; import { runThrowTestsWithEstree } from "./helpers/runFixtureTests"; import { parse } from "../lib"; +import { fileURLToPath } from "url"; -runThrowTestsWithEstree(path.join(__dirname, "fixtures"), parse); +runThrowTestsWithEstree( + path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"), + parse, +); diff --git a/packages/babel-parser/test/expressions.js b/packages/babel-parser/test/expressions.js index 6ecb36971ffa..814ebbd65ade 100644 --- a/packages/babel-parser/test/expressions.js +++ b/packages/babel-parser/test/expressions.js @@ -1,5 +1,11 @@ import path from "path"; import { runFixtureTests } from "./helpers/runFixtureTests"; import { parseExpression } from "../lib"; +import { fileURLToPath } from "url"; -runFixtureTests(path.join(__dirname, "expressions"), parseExpression); +const fixtures = path.join( + path.dirname(fileURLToPath(import.meta.url)), + "expressions", +); + +runFixtureTests(fixtures, parseExpression); diff --git a/packages/babel-parser/test/helpers/runFixtureTests.js b/packages/babel-parser/test/helpers/runFixtureTests.js index 46d19c2a1a1c..165792e80272 100644 --- a/packages/babel-parser/test/helpers/runFixtureTests.js +++ b/packages/babel-parser/test/helpers/runFixtureTests.js @@ -2,8 +2,12 @@ import { multiple as getFixtures } from "@babel/helper-fixtures"; import { codeFrameColumns } from "@babel/code-frame"; import fs from "fs"; import path from "path"; +import { fileURLToPath } from "url"; -const rootPath = path.join(__dirname, "../../../.."); +const rootPath = path.join( + path.dirname(fileURLToPath(import.meta.url)), + "../../../..", +); const serialized = "$$ babel internal serialized type"; diff --git a/packages/babel-parser/test/index.js b/packages/babel-parser/test/index.js index 7ec91fb86a05..a87e7b44cc9d 100644 --- a/packages/babel-parser/test/index.js +++ b/packages/babel-parser/test/index.js @@ -1,5 +1,10 @@ import path from "path"; import { runFixtureTests } from "./helpers/runFixtureTests"; import { parse } from "../lib"; +import { fileURLToPath } from "url"; -runFixtureTests(path.join(__dirname, "fixtures"), parse); +const fixtures = path.join( + path.dirname(fileURLToPath(import.meta.url)), + "fixtures", +); +runFixtureTests(fixtures, parse); diff --git a/packages/babel-plugin-external-helpers/test/index.js b/packages/babel-plugin-external-helpers/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-external-helpers/test/index.js +++ b/packages/babel-plugin-external-helpers/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-async-generator-functions/test/index.js b/packages/babel-plugin-proposal-async-generator-functions/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-async-generator-functions/test/index.js +++ b/packages/babel-plugin-proposal-async-generator-functions/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-class-properties/test/index.js b/packages/babel-plugin-proposal-class-properties/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-class-properties/test/index.js +++ b/packages/babel-plugin-proposal-class-properties/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-class-static-block/test/index.js b/packages/babel-plugin-proposal-class-static-block/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-class-static-block/test/index.js +++ b/packages/babel-plugin-proposal-class-static-block/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-decorators/test/index.js b/packages/babel-plugin-proposal-decorators/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-decorators/test/index.js +++ b/packages/babel-plugin-proposal-decorators/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-do-expressions/test/index.js b/packages/babel-plugin-proposal-do-expressions/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-do-expressions/test/index.js +++ b/packages/babel-plugin-proposal-do-expressions/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-dynamic-import/test/index.js b/packages/babel-plugin-proposal-dynamic-import/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-dynamic-import/test/index.js +++ b/packages/babel-plugin-proposal-dynamic-import/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-export-default-from/test/index.js b/packages/babel-plugin-proposal-export-default-from/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-export-default-from/test/index.js +++ b/packages/babel-plugin-proposal-export-default-from/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-export-namespace-from/test/index.js b/packages/babel-plugin-proposal-export-namespace-from/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-export-namespace-from/test/index.js +++ b/packages/babel-plugin-proposal-export-namespace-from/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-function-bind/test/index.js b/packages/babel-plugin-proposal-function-bind/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-function-bind/test/index.js +++ b/packages/babel-plugin-proposal-function-bind/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-function-sent/test/index.js b/packages/babel-plugin-proposal-function-sent/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-function-sent/test/index.js +++ b/packages/babel-plugin-proposal-function-sent/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-json-strings/test/index.js b/packages/babel-plugin-proposal-json-strings/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-json-strings/test/index.js +++ b/packages/babel-plugin-proposal-json-strings/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/index.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-logical-assignment-operators/test/index.js +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-nullish-coalescing-operator/test/index.js b/packages/babel-plugin-proposal-nullish-coalescing-operator/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-nullish-coalescing-operator/test/index.js +++ b/packages/babel-plugin-proposal-nullish-coalescing-operator/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-numeric-separator/test/index.js b/packages/babel-plugin-proposal-numeric-separator/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-numeric-separator/test/index.js +++ b/packages/babel-plugin-proposal-numeric-separator/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-object-rest-spread/test/index.js b/packages/babel-plugin-proposal-object-rest-spread/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-object-rest-spread/test/index.js +++ b/packages/babel-plugin-proposal-object-rest-spread/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-optional-catch-binding/test/index.js b/packages/babel-plugin-proposal-optional-catch-binding/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-optional-catch-binding/test/index.js +++ b/packages/babel-plugin-proposal-optional-catch-binding/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-optional-chaining/test/index.js b/packages/babel-plugin-proposal-optional-chaining/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-optional-chaining/test/index.js +++ b/packages/babel-plugin-proposal-optional-chaining/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-partial-application/test/index.js b/packages/babel-plugin-proposal-partial-application/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-partial-application/test/index.js +++ b/packages/babel-plugin-proposal-partial-application/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/index.js b/packages/babel-plugin-proposal-pipeline-operator/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/index.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-private-methods/test/index.js b/packages/babel-plugin-proposal-private-methods/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-private-methods/test/index.js +++ b/packages/babel-plugin-proposal-private-methods/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/index.js b/packages/babel-plugin-proposal-private-property-in-object/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/index.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-record-and-tuple/test/index.js b/packages/babel-plugin-proposal-record-and-tuple/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-record-and-tuple/test/index.js +++ b/packages/babel-plugin-proposal-record-and-tuple/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-throw-expressions/test/index.js b/packages/babel-plugin-proposal-throw-expressions/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-throw-expressions/test/index.js +++ b/packages/babel-plugin-proposal-throw-expressions/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-unicode-property-regex/test/index.js b/packages/babel-plugin-proposal-unicode-property-regex/test/index.js index 8c71ab59f548..21a55ce6b5e7 100644 --- a/packages/babel-plugin-proposal-unicode-property-regex/test/index.js +++ b/packages/babel-plugin-proposal-unicode-property-regex/test/index.js @@ -1,2 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); + +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-arrow-functions/test/index.js b/packages/babel-plugin-transform-arrow-functions/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-arrow-functions/test/index.js +++ b/packages/babel-plugin-transform-arrow-functions/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-async-to-generator/test/index.js b/packages/babel-plugin-transform-async-to-generator/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-async-to-generator/test/index.js +++ b/packages/babel-plugin-transform-async-to-generator/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-block-scoped-functions/test/index.js b/packages/babel-plugin-transform-block-scoped-functions/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-block-scoped-functions/test/index.js +++ b/packages/babel-plugin-transform-block-scoped-functions/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-block-scoping/test/index.js b/packages/babel-plugin-transform-block-scoping/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-block-scoping/test/index.js +++ b/packages/babel-plugin-transform-block-scoping/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-classes/test/index.js b/packages/babel-plugin-transform-classes/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-classes/test/index.js +++ b/packages/babel-plugin-transform-classes/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-computed-properties/test/index.js b/packages/babel-plugin-transform-computed-properties/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-computed-properties/test/index.js +++ b/packages/babel-plugin-transform-computed-properties/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-destructuring/test/index.js b/packages/babel-plugin-transform-destructuring/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-destructuring/test/index.js +++ b/packages/babel-plugin-transform-destructuring/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-dotall-regex/test/index.js b/packages/babel-plugin-transform-dotall-regex/test/index.js index 8c71ab59f548..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-dotall-regex/test/index.js +++ b/packages/babel-plugin-transform-dotall-regex/test/index.js @@ -1,2 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); + +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-duplicate-keys/test/index.js b/packages/babel-plugin-transform-duplicate-keys/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-duplicate-keys/test/index.js +++ b/packages/babel-plugin-transform-duplicate-keys/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-exponentiation-operator/test/index.js b/packages/babel-plugin-transform-exponentiation-operator/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-exponentiation-operator/test/index.js +++ b/packages/babel-plugin-transform-exponentiation-operator/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-flow-comments/test/index.js b/packages/babel-plugin-transform-flow-comments/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-flow-comments/test/index.js +++ b/packages/babel-plugin-transform-flow-comments/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-flow-strip-types/test/index.js b/packages/babel-plugin-transform-flow-strip-types/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-flow-strip-types/test/index.js +++ b/packages/babel-plugin-transform-flow-strip-types/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-for-of/test/index.js b/packages/babel-plugin-transform-for-of/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-for-of/test/index.js +++ b/packages/babel-plugin-transform-for-of/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-function-name/test/index.js b/packages/babel-plugin-transform-function-name/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-function-name/test/index.js +++ b/packages/babel-plugin-transform-function-name/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-instanceof/test/index.js b/packages/babel-plugin-transform-instanceof/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-instanceof/test/index.js +++ b/packages/babel-plugin-transform-instanceof/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-jscript/test/index.js b/packages/babel-plugin-transform-jscript/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-jscript/test/index.js +++ b/packages/babel-plugin-transform-jscript/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-member-expression-literals/test/index.js b/packages/babel-plugin-transform-member-expression-literals/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-member-expression-literals/test/index.js +++ b/packages/babel-plugin-transform-member-expression-literals/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-modules-amd/test/index.js b/packages/babel-plugin-transform-modules-amd/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-modules-amd/test/index.js +++ b/packages/babel-plugin-transform-modules-amd/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js b/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js index d0c3e4894e65..4fb76081850a 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js @@ -1,12 +1,16 @@ -const babel = require("@babel/core"); +import * as babel from "@babel/core"; +import { fileURLToPath } from "url"; +import path from "path"; + +import transformCommonJS from ".."; test("Doesn't use the same object for two different nodes in the AST", function () { const code = 'import Foo from "bar"; Foo; Foo;'; const ast = babel.transform(code, { - cwd: __dirname, + cwd: path.dirname(fileURLToPath(import.meta.url)), ast: true, - plugins: [[require("../"), { loose: true }]], + plugins: [[transformCommonJS, { loose: true }]], }).ast; expect(ast.program.body[0].declarations[0].id.type).toBe("Identifier"); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js b/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js index 30d9a1f4745a..cdb44988f7a4 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js @@ -1,5 +1,9 @@ -const babel = require("@babel/core"); -const vm = require("vm"); +import * as babel from "@babel/core"; +import vm from "vm"; +import { fileURLToPath } from "url"; +import path from "path"; + +import transformCommonJS from ".."; test("Re-export doesn't overwrite __esModule flag", function () { let code = 'export * from "./dep";'; @@ -13,14 +17,14 @@ test("Re-export doesn't overwrite __esModule flag", function () { }, require: function (id) { if (id === "./dep") return depStub; - return require(id); + throw new Error("Unexpected dependency: " + id); }, }; context.exports = context.module.exports; code = babel.transform(code, { - cwd: __dirname, - plugins: [[require("../"), { loose: true }]], + cwd: path.dirname(fileURLToPath(import.meta.url)), + plugins: [[transformCommonJS, { loose: true }]], ast: false, }).code; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/index.js b/packages/babel-plugin-transform-modules-commonjs/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/index.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-modules-systemjs/test/index.js b/packages/babel-plugin-transform-modules-systemjs/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-modules-systemjs/test/index.js +++ b/packages/babel-plugin-transform-modules-systemjs/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-modules-umd/test/index.js b/packages/babel-plugin-transform-modules-umd/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-modules-umd/test/index.js +++ b/packages/babel-plugin-transform-modules-umd/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-named-capturing-groups-regex/test/index.js b/packages/babel-plugin-transform-named-capturing-groups-regex/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-named-capturing-groups-regex/test/index.js +++ b/packages/babel-plugin-transform-named-capturing-groups-regex/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-new-target/test/index.js b/packages/babel-plugin-transform-new-target/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-new-target/test/index.js +++ b/packages/babel-plugin-transform-new-target/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-object-super/test/index.js b/packages/babel-plugin-transform-object-super/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-object-super/test/index.js +++ b/packages/babel-plugin-transform-object-super/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-parameters/test/index.js b/packages/babel-plugin-transform-parameters/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-parameters/test/index.js +++ b/packages/babel-plugin-transform-parameters/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-property-literals/test/index.js b/packages/babel-plugin-transform-property-literals/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-property-literals/test/index.js +++ b/packages/babel-plugin-transform-property-literals/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-property-mutators/test/index.js b/packages/babel-plugin-transform-property-mutators/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-property-mutators/test/index.js +++ b/packages/babel-plugin-transform-property-mutators/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-proto-to-assign/test/index.js b/packages/babel-plugin-transform-proto-to-assign/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-proto-to-assign/test/index.js +++ b/packages/babel-plugin-transform-proto-to-assign/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-react-constant-elements/test/index.js b/packages/babel-plugin-transform-react-constant-elements/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-react-constant-elements/test/index.js +++ b/packages/babel-plugin-transform-react-constant-elements/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-react-display-name/test/index.js b/packages/babel-plugin-transform-react-display-name/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-react-display-name/test/index.js +++ b/packages/babel-plugin-transform-react-display-name/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-react-inline-elements/test/index.js b/packages/babel-plugin-transform-react-inline-elements/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-react-inline-elements/test/index.js +++ b/packages/babel-plugin-transform-react-inline-elements/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-react-jsx-compat/test/index.js b/packages/babel-plugin-transform-react-jsx-compat/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-react-jsx-compat/test/index.js +++ b/packages/babel-plugin-transform-react-jsx-compat/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-react-jsx-development/test/index.js b/packages/babel-plugin-transform-react-jsx-development/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-react-jsx-development/test/index.js +++ b/packages/babel-plugin-transform-react-jsx-development/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-react-jsx-self/test/index.js b/packages/babel-plugin-transform-react-jsx-self/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-react-jsx-self/test/index.js +++ b/packages/babel-plugin-transform-react-jsx-self/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-react-jsx-source/test/index.js b/packages/babel-plugin-transform-react-jsx-source/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-react-jsx-source/test/index.js +++ b/packages/babel-plugin-transform-react-jsx-source/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-react-jsx/test/index.js b/packages/babel-plugin-transform-react-jsx/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-react-jsx/test/index.js +++ b/packages/babel-plugin-transform-react-jsx/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-react-pure-annotations/test/index.js b/packages/babel-plugin-transform-react-pure-annotations/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-react-pure-annotations/test/index.js +++ b/packages/babel-plugin-transform-react-pure-annotations/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-regenerator/test/index.js b/packages/babel-plugin-transform-regenerator/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-regenerator/test/index.js +++ b/packages/babel-plugin-transform-regenerator/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-reserved-words/test/index.js b/packages/babel-plugin-transform-reserved-words/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-reserved-words/test/index.js +++ b/packages/babel-plugin-transform-reserved-words/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-runtime/src/get-runtime-path/index.js b/packages/babel-plugin-transform-runtime/src/get-runtime-path/index.js index c7a9c09d9228..56e3a5a1ed49 100644 --- a/packages/babel-plugin-transform-runtime/src/get-runtime-path/index.js +++ b/packages/babel-plugin-transform-runtime/src/get-runtime-path/index.js @@ -1,5 +1,8 @@ import path from "path"; +import { createRequire } from "module"; +const require = createRequire(import.meta.url); + export default function (moduleName, dirname, absoluteRuntime) { if (absoluteRuntime === false) return moduleName; diff --git a/packages/babel-plugin-transform-runtime/test/index.js b/packages/babel-plugin-transform-runtime/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-runtime/test/index.js +++ b/packages/babel-plugin-transform-runtime/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-runtime/test/regression.js b/packages/babel-plugin-transform-runtime/test/regression.js index ad661f5c6636..b85c72054e57 100644 --- a/packages/babel-plugin-transform-runtime/test/regression.js +++ b/packages/babel-plugin-transform-runtime/test/regression.js @@ -1,3 +1,7 @@ +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); + it("module.exports.default is correctly updated", () => { const typeofHelper = require("@babel/runtime/helpers/typeof"); diff --git a/packages/babel-plugin-transform-shorthand-properties/test/index.js b/packages/babel-plugin-transform-shorthand-properties/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-shorthand-properties/test/index.js +++ b/packages/babel-plugin-transform-shorthand-properties/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-spread/test/index.js b/packages/babel-plugin-transform-spread/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-spread/test/index.js +++ b/packages/babel-plugin-transform-spread/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-sticky-regex/test/index.js b/packages/babel-plugin-transform-sticky-regex/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-sticky-regex/test/index.js +++ b/packages/babel-plugin-transform-sticky-regex/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-strict-mode/test/index.js b/packages/babel-plugin-transform-strict-mode/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-strict-mode/test/index.js +++ b/packages/babel-plugin-transform-strict-mode/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-template-literals/test/index.js b/packages/babel-plugin-transform-template-literals/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-template-literals/test/index.js +++ b/packages/babel-plugin-transform-template-literals/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js b/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js index 8a11f4133f10..7d84993c92e3 100644 --- a/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js +++ b/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js @@ -1,8 +1,11 @@ import * as babel from "@babel/core"; import fs from "fs"; +import { createRequire } from "module"; import transformTypeofSymbol from ".."; +const require = createRequire(import.meta.url); + const readFile = path => new Promise((resolve, reject) => fs.readFile(path, "utf8", (err, contents) => { diff --git a/packages/babel-plugin-transform-typeof-symbol/test/index.js b/packages/babel-plugin-transform-typeof-symbol/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-typeof-symbol/test/index.js +++ b/packages/babel-plugin-transform-typeof-symbol/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-typescript/test/index.js b/packages/babel-plugin-transform-typescript/test/index.js index 8c71ab59f548..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-typescript/test/index.js +++ b/packages/babel-plugin-transform-typescript/test/index.js @@ -1,2 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); + +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-unicode-escapes/test/index.js b/packages/babel-plugin-transform-unicode-escapes/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-unicode-escapes/test/index.js +++ b/packages/babel-plugin-transform-unicode-escapes/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-plugin-transform-unicode-regex/test/index.js b/packages/babel-plugin-transform-unicode-regex/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-plugin-transform-unicode-regex/test/index.js +++ b/packages/babel-plugin-transform-unicode-regex/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-preset-env/test/fixtures.js b/packages/babel-preset-env/test/fixtures.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-preset-env/test/fixtures.js +++ b/packages/babel-preset-env/test/fixtures.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-preset-env/test/get-option-specific-excludes.spec.js b/packages/babel-preset-env/test/get-option-specific-excludes.spec.js index ea0cd6950c7d..f3cdae4485fa 100644 --- a/packages/babel-preset-env/test/get-option-specific-excludes.spec.js +++ b/packages/babel-preset-env/test/get-option-specific-excludes.spec.js @@ -1,7 +1,4 @@ -"use strict"; - -const getOptionSpecificExcludesFor = require("../lib/get-option-specific-excludes") - .default; +import getOptionSpecificExcludesFor from "../lib/get-option-specific-excludes"; describe("defaults", () => { describe("getOptionSpecificExcludesFor", () => { diff --git a/packages/babel-preset-env/test/index.spec.js b/packages/babel-preset-env/test/index.spec.js index 16f265c86c02..15878bc898ba 100644 --- a/packages/babel-preset-env/test/index.spec.js +++ b/packages/babel-preset-env/test/index.spec.js @@ -1,17 +1,17 @@ -"use strict"; +import compatData from "@babel/compat-data/plugins"; -const babelPresetEnv = require("../lib/index"); -const removeRegeneratorEntryPlugin = require("../lib/polyfills/regenerator") - .default; -const pluginCoreJS2 = require("babel-plugin-polyfill-corejs2").default; -const pluginCoreJS3 = require("babel-plugin-polyfill-corejs3").default; -const pluginRegenerator = require("babel-plugin-polyfill-regenerator").default; -const pluginLegacyBabelPolyfill = require("../lib/polyfills/babel-polyfill") - .default; -const transformations = require("../lib/module-transformations").default; +import * as babelPresetEnv from "../lib/index"; +import removeRegeneratorEntryPlugin from "../lib/polyfills/regenerator"; +import pluginLegacyBabelPolyfill from "../lib/polyfills/babel-polyfill"; +import transformations from "../lib/module-transformations"; +import availablePlugins from "../lib/available-plugins"; -const compatData = require("@babel/compat-data/plugins"); -const availablePlugins = require("../lib/available-plugins").default; +import _pluginCoreJS2 from "babel-plugin-polyfill-corejs2"; +import _pluginCoreJS3 from "babel-plugin-polyfill-corejs3"; +import _pluginRegenerator from "babel-plugin-polyfill-regenerator"; +const pluginCoreJS2 = _pluginCoreJS2.default; +const pluginCoreJS3 = _pluginCoreJS3.default; +const pluginRegenerator = _pluginRegenerator.default; describe("babel-preset-env", () => { describe("transformIncludesAndExcludes", () => { diff --git a/packages/babel-preset-env/test/normalize-options.spec.js b/packages/babel-preset-env/test/normalize-options.spec.js index 948c107cee70..aaac0dc718e5 100644 --- a/packages/babel-preset-env/test/normalize-options.spec.js +++ b/packages/babel-preset-env/test/normalize-options.spec.js @@ -1,17 +1,14 @@ -"use strict"; - -const normalizeOptions = require("../lib/normalize-options.js"); - -const { +import normalizeOptions, { checkDuplicateIncludeExcludes, validateModulesOption, validateUseBuiltInsOption, normalizePluginName, -} = normalizeOptions; +} from "../lib/normalize-options"; + describe("normalize-options", () => { describe("normalizeOptions", () => { it("should return normalized `include` and `exclude`", () => { - const normalized = normalizeOptions.default({ + const normalized = normalizeOptions({ include: [ "babel-plugin-transform-spread", "transform-classes", @@ -59,9 +56,9 @@ describe("normalize-options", () => { `( "should throw if with includes $include and excludes $exclude", ({ include, exclude }) => { - expect(() => - normalizeOptions.default({ include, exclude }), - ).toThrowError(/were found in both/); + expect(() => normalizeOptions({ include, exclude })).toThrowError( + /were found in both/, + ); }, ); @@ -69,7 +66,7 @@ describe("normalize-options", () => { [2, 2.1, 3, 3.5].forEach(corejs => { ["entry", "usage"].forEach(useBuiltIns => { expect(() => - normalizeOptions.default({ useBuiltIns, corejs }), + normalizeOptions({ useBuiltIns, corejs }), ).not.toThrowError(); }); }); @@ -78,28 +75,28 @@ describe("normalize-options", () => { it("should throw if corejs version is invalid", () => { [1, 1.2, 4, 4.5].forEach(corejs => { ["entry", "usage"].forEach(useBuiltIns => { - expect(() => - normalizeOptions.default({ useBuiltIns, corejs }), - ).toThrowError(/The version passed to `corejs` is invalid./); + expect(() => normalizeOptions({ useBuiltIns, corejs })).toThrowError( + /The version passed to `corejs` is invalid./, + ); }); }); }); it("throws when including module plugins", () => { expect(() => - normalizeOptions.default({ include: ["proposal-dynamic-import"] }), + normalizeOptions({ include: ["proposal-dynamic-import"] }), ).toThrow(); expect(() => - normalizeOptions.default({ include: ["transform-modules-amd"] }), + normalizeOptions({ include: ["transform-modules-amd"] }), ).toThrow(); }); it("allows exclusion of module plugins ", () => { expect(() => - normalizeOptions.default({ exclude: ["proposal-dynamic-import"] }), + normalizeOptions({ exclude: ["proposal-dynamic-import"] }), ).not.toThrow(); expect(() => - normalizeOptions.default({ exclude: ["transform-modules-commonjs"] }), + normalizeOptions({ exclude: ["transform-modules-commonjs"] }), ).not.toThrow(); }); }); @@ -116,7 +113,7 @@ describe("normalize-options", () => { describe("RegExp include/exclude", () => { it("should not allow invalid plugins in `include` and `exclude`", () => { const normalizeWithNonExistingPlugin = () => { - normalizeOptions.default({ + normalizeOptions({ include: ["non-existing-plugin"], }); }; @@ -124,7 +121,7 @@ describe("normalize-options", () => { }); it("should expand regular expressions in `include` and `exclude`", () => { - const normalized = normalizeOptions.default({ + const normalized = normalizeOptions({ include: ["^[a-z]*-spread", "babel-plugin-transform-classes"], }); expect(normalized.include).toEqual([ @@ -134,7 +131,7 @@ describe("normalize-options", () => { }); it("should expand regular expressions in `include` and `exclude`", () => { - const normalized = normalizeOptions.default({ + const normalized = normalizeOptions({ useBuiltIns: "entry", corejs: 3, exclude: ["es.math.log.*"], @@ -148,7 +145,7 @@ describe("normalize-options", () => { it("should not allow the same modules in `include` and `exclude`", () => { const normalizeWithNonExistingPlugin = () => { - normalizeOptions.default({ + normalizeOptions({ useBuiltIns: "entry", corejs: 3, include: ["es.math.log2"], @@ -159,7 +156,7 @@ describe("normalize-options", () => { }); it("should not do partial match if not explicitly defined `include` and `exclude`", () => { - const normalized = normalizeOptions.default({ + const normalized = normalizeOptions({ useBuiltIns: "entry", corejs: 3, include: ["es.reflect.set-prototype-of"], diff --git a/packages/babel-preset-env/test/regressions.js b/packages/babel-preset-env/test/regressions.js index 6adee32f5d08..9f201b0adb3e 100644 --- a/packages/babel-preset-env/test/regressions.js +++ b/packages/babel-preset-env/test/regressions.js @@ -1,6 +1,7 @@ import * as babel7_12 from "@babel/core"; import env from ".."; import path from "path"; +import { fileURLToPath } from "url"; describe("#12880", () => { it("read the .browserslistrc file when using @babel/core < 7.13.0", () => { @@ -10,7 +11,11 @@ describe("#12880", () => { const out = babel7_12.transformSync("a ** b; a => b;", { configFile: false, presets: [[env, { modules: false }]], - filename: path.join(__dirname, "regressions", "input.js"), + filename: path.join( + path.dirname(fileURLToPath(import.meta.url)), + "regressions", + "input.js", + ), }); expect(out.code).toMatchInlineSnapshot(` diff --git a/packages/babel-preset-flow/test/flow.js b/packages/babel-preset-flow/test/flow.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-preset-flow/test/flow.js +++ b/packages/babel-preset-flow/test/flow.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-preset-react/test/preset-options.js b/packages/babel-preset-react/test/preset-options.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-preset-react/test/preset-options.js +++ b/packages/babel-preset-react/test/preset-options.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-preset-typescript/test/index.js b/packages/babel-preset-typescript/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-preset-typescript/test/index.js +++ b/packages/babel-preset-typescript/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/packages/babel-register/test/browserify.js b/packages/babel-register/test/browserify.js index 8696083e7257..8fb983b97b8f 100644 --- a/packages/babel-register/test/browserify.js +++ b/packages/babel-register/test/browserify.js @@ -1,11 +1,15 @@ import browserify from "browserify"; import path from "path"; import vm from "vm"; +import { fileURLToPath } from "url"; describe("browserify", function () { it("@babel/register may be used without breaking browserify", function (done) { const bundler = browserify( - path.join(__dirname, "fixtures/browserify/register.js"), + path.join( + path.dirname(fileURLToPath(import.meta.url)), + "fixtures/browserify/register.js", + ), ); bundler.bundle(function (err, bundle) { diff --git a/packages/babel-register/test/cache.js b/packages/babel-register/test/cache.js index 237b82578f53..222889871af8 100644 --- a/packages/babel-register/test/cache.js +++ b/packages/babel-register/test/cache.js @@ -1,7 +1,11 @@ import fs from "fs"; import path from "path"; +import { fileURLToPath } from "url"; -const testCacheFilename = path.join(__dirname, ".babel"); +const testCacheFilename = path.join( + path.dirname(fileURLToPath(import.meta.url)), + ".babel", +); const oldBabelDisableCacheValue = process.env.BABEL_DISABLE_CACHE; process.env.BABEL_CACHE_PATH = testCacheFilename; diff --git a/packages/babel-register/test/index.js b/packages/babel-register/test/index.js index 6114f61a2a1d..8b60ae9d69b5 100644 --- a/packages/babel-register/test/index.js +++ b/packages/babel-register/test/index.js @@ -1,6 +1,8 @@ -import fs from "fs"; -import path from "path"; -import child from "child_process"; +"use strict"; + +const fs = require("fs"); +const path = require("path"); +const child = require("child_process"); let currentHook; let currentOptions; diff --git a/packages/babel-standalone/test/babel.js b/packages/babel-standalone/test/babel.js index 3858571e3963..6bb1307a0678 100644 --- a/packages/babel-standalone/test/babel.js +++ b/packages/babel-standalone/test/babel.js @@ -1,3 +1,6 @@ +import { createRequire } from "module"; +const require = createRequire(import.meta.url); + // Basic smoke tests for @babel/standalone (process.env.TEST_TYPE === "cov" ? describe.skip : describe)( "@babel/standalone", diff --git a/packages/babel-standalone/test/preset-stage-1.test.js b/packages/babel-standalone/test/preset-stage-1.test.js index fbcb49b6ff0b..1be445c5cfcf 100644 --- a/packages/babel-standalone/test/preset-stage-1.test.js +++ b/packages/babel-standalone/test/preset-stage-1.test.js @@ -1,3 +1,6 @@ +import { createRequire } from "module"; +const require = createRequire(import.meta.url); + (process.env.TEST_TYPE === "cov" ? describe.skip : describe)( "stage-1 preset", () => { diff --git a/packages/babel-traverse/test/index.js b/packages/babel-traverse/test/index.js index 1b534b8fc64a..21a55ce6b5e7 100644 --- a/packages/babel-traverse/test/index.js +++ b/packages/babel-traverse/test/index.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/yarn.lock b/yarn.lock index 183641765cbc..bbf152921b51 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1104,15 +1104,15 @@ __metadata: languageName: unknown linkType: soft -"@babel/plugin-proposal-dynamic-import@npm:^7.12.17": - version: 7.12.17 - resolution: "@babel/plugin-proposal-dynamic-import@npm:7.12.17" +"@babel/plugin-proposal-dynamic-import@npm:^7.12.17, @babel/plugin-proposal-dynamic-import@npm:^7.13.8": + version: 7.13.8 + resolution: "@babel/plugin-proposal-dynamic-import@npm:7.13.8" dependencies: - "@babel/helper-plugin-utils": ^7.12.13 - "@babel/plugin-syntax-dynamic-import": ^7.8.0 + "@babel/helper-plugin-utils": ^7.13.0 + "@babel/plugin-syntax-dynamic-import": ^7.8.3 peerDependencies: "@babel/core": ^7.0.0-0 - checksum: ff09c210552e7c0e5a624c4c56f5cba00922f6c5c01fe46fb2ea5c2d68285d31c92e96a39cc2a68d2ed90c8f5a8c21cbae15ee7b83129f24c0656b26b8e73554 + checksum: 47ff82e3e3731f93a9e51a7f16f1807ee5fbd627df67b423bcf9bb206ae9230385d62c7310e41ec6ef732f01f0af81061bbbf5f6cbfe6b95aa8fa01571166c9e languageName: node linkType: hard @@ -4085,9 +4085,9 @@ __metadata: linkType: hard "@types/node@npm:*": - version: 12.12.15 - resolution: "@types/node@npm:12.12.15" - checksum: 3f68af6f288ab0994be8d69f2e9f2a530b276ac8de4bc48492897e9c204492ce706072effdadda21f399490688bfb35ffb0f4aa674f6a2e72b27d0cc3b094842 + version: 14.14.31 + resolution: "@types/node@npm:14.14.31" + checksum: 635dc8a0898a923621e02ca179e17baa39fdfa44f0096fcc1b7046c9b32317e74a99956a7b45ca0e8069874f51f4e7873a418239a318a4b6e7936f6510ac5992 languageName: node linkType: hard @@ -5471,6 +5471,7 @@ __metadata: "@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