Avoid using CJS globals in internal source files (#12963)
* Lint against CJS globals in modules * Use `import.meta.url` instead of `__filename` in `src` files * Prepare fixtures runner for `import.meta.url` * Use `import.meta.url` instead of `__filename` in `test/index` files * Remove `__dirname` from remaining test files dirname * Avoid using `module` in `src` files * Avoid using `require` in `src` files * Avoid using `require` in `test` files * Update `@types/node` * Compile dynamic import in `@babel/node` * Fix windows * Use `@babel/plugin-proposal-dynamic-import` from npm
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
import runner from "@babel/helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
runner(import.meta.url);
|
||||
|
||||
Reference in New Issue
Block a user