Use native ESM for dev scripts (#12296)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Karan Sapolia
2021-01-30 23:06:21 +05:30
committed by GitHub
parent f8fe8eaab1
commit b63be942ce
39 changed files with 205 additions and 193 deletions

View File

@@ -5,8 +5,11 @@
* This script write the link to the website in every READMEs.
*/
const { join } = require("path");
const { readdirSync, writeFileSync } = require("fs");
import { join } from "path";
import { readdirSync, writeFileSync } from "fs";
import { createRequire } from "url";
const require = createRequire(import.meta.url);
const cwd = process.cwd();

View File

@@ -1,9 +1,14 @@
"use strict";
import path from "path";
import fs from "fs";
import { createRequire } from "module";
import { fileURLToPath } from "url";
const path = require("path");
const fs = require("fs");
const require = createRequire(import.meta.url);
const root = path.resolve(__dirname, "../../");
const root = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../"
);
function getTsPkgs(subRoot) {
return fs

View File

@@ -1,5 +1,6 @@
const fs = require("fs");
const path = require("path");
import fs from "fs";
import path from "path";
const cwd = process.cwd();
const packageJSONPath = path.resolve(cwd, "./package.json");
const content = JSON.parse(fs.readFileSync(packageJSONPath));

1
scripts/package.json Normal file
View File

@@ -0,0 +1 @@
{ "type": "module" }

View File

@@ -1,7 +1,10 @@
const fs = require("fs").promises;
const path = require("path");
const merge = require("mergeiterator");
const TestRunner = require("../utils/parser-test-runner");
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
import merge from "mergeiterator";
import TestRunner from "../utils/parser-test-runner.js";
const dirname = path.dirname(fileURLToPath(import.meta.url));
const flowOptionsMapping = {
esproposal_class_instance_fields: "classProperties",
@@ -88,8 +91,8 @@ async function* loadTests(root) {
}
const runner = new TestRunner({
testDir: path.join(__dirname, "../../../build/flow/src/parser/test/flow"),
allowlist: path.join(__dirname, "allowlist.txt"),
testDir: path.join(dirname, "../../../build/flow/src/parser/test/flow"),
allowlist: path.join(dirname, "allowlist.txt"),
shouldUpdate: process.argv.includes("--update-allowlist"),
async *getTests() {

View File

@@ -1,6 +1,9 @@
const path = require("path");
const TestStream = require("test262-stream");
const TestRunner = require("../utils/parser-test-runner");
import path from "path";
import { fileURLToPath } from "url";
import TestStream from "test262-stream";
import TestRunner from "../utils/parser-test-runner.js";
const dirname = path.dirname(fileURLToPath(import.meta.url));
const ignoredFeatures = [
"__getter__",
@@ -163,8 +166,8 @@ function* getPlugins(features) {
}
const runner = new TestRunner({
testDir: path.join(__dirname, "../../../build/test262"),
allowlist: path.join(__dirname, "allowlist.txt"),
testDir: path.join(dirname, "../../../build/test262"),
allowlist: path.join(dirname, "allowlist.txt"),
logInterval: 500,
shouldUpdate: process.argv.includes("--update-allowlist"),

View File

@@ -8,7 +8,7 @@ Note that babel-parser should not throw for the TypeChecking Diagnostics
The commented out diagnostic codes will introduce false positive cases that should be addressed in separate PRs.
*/
module.exports = [
export default [
// "TS1005", // '{0}' expected.
"TS1009", // Trailing comma not allowed.
"TS1014", // A rest parameter must be last in a parameter list.

View File

@@ -1,8 +1,11 @@
const path = require("path");
const fs = require("fs").promises;
const ts = require("../../../build/typescript");
const TestRunner = require("../utils/parser-test-runner");
const parsingErrorCodes = require("./error-codes");
import path from "path";
import fs from "fs/promises";
import { fileURLToPath } from "url";
import ts from "../../../build/typescript/lib/typescript.js";
import TestRunner from "../utils/parser-test-runner.js";
import parsingErrorCodes from "./error-codes.js";
const dirname = path.dirname(fileURLToPath(import.meta.url));
async function* loadTests(dir) {
const names = await fs.readdir(dir);
@@ -21,7 +24,7 @@ const plugins = [
"dynamicImport",
];
const TSTestsPath = path.join(__dirname, "../../../build/typescript/tests");
const TSTestsPath = path.join(dirname, "../../../build/typescript/tests");
// Check if the baseline errors contain the codes that should also be thrown from babel-parser
async function baselineContainsParserErrorCodes(testName) {
@@ -45,7 +48,7 @@ async function baselineContainsParserErrorCodes(testName) {
const runner = new TestRunner({
testDir: path.join(TSTestsPath, "./cases/compiler"),
allowlist: path.join(__dirname, "allowlist.txt"),
allowlist: path.join(dirname, "allowlist.txt"),
logInterval: 50,
shouldUpdate: process.argv.includes("--update-allowlist"),

View File

@@ -1,8 +1,6 @@
"use strict";
const fs = require("fs").promises;
const chalk = require("chalk");
const { parse: parser } = require("../../../packages/babel-parser");
import fs from "fs/promises";
import chalk from "chalk";
import { parse as parser } from "../../../packages/babel-parser/lib/index.js";
const dot = chalk.gray(".");
@@ -234,4 +232,4 @@ class TestRunner {
}
}
module.exports = exports = TestRunner;
export default TestRunner;

View File

@@ -1,20 +1,27 @@
const path = require("path");
const fs = require("fs");
const dirname = path.join(__dirname, "..");
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const monorepoRoot = path.join(
path.dirname(fileURLToPath(import.meta.url)),
".."
);
const BABEL_SRC_REGEXP =
path.sep === "/"
? /packages\/(babel-[^/]+)\/src\//
: /packages\\(babel-[^\\]+)\\src\\/;
module.exports = function () {
export default function () {
return {
name: "babel-source",
load(id) {
const matches = id.match(BABEL_SRC_REGEXP);
if (matches) {
// check if browser field exists for this file and replace
const packageFolder = path.join(dirname, "packages", matches[1]);
const packageFolder = path.join(monorepoRoot, "packages", matches[1]);
const packageJson = require(path.join(packageFolder, "package.json"));
if (
@@ -46,7 +53,7 @@ module.exports = function () {
resolveId(importee) {
if (importee === "@babel/runtime/regenerator") {
return path.join(
dirname,
monorepoRoot,
"packages",
"babel-runtime",
"regenerator",
@@ -61,7 +68,7 @@ module.exports = function () {
const { pkg, internal } = matches.groups;
// resolve babel package names to their src index file
const packageFolder = path.join(dirname, "packages", `babel-${pkg}`);
const packageFolder = path.join(monorepoRoot, "packages", `babel-${pkg}`);
let packageJsonSource;
try {
@@ -98,4 +105,4 @@ module.exports = function () {
}
},
};
};
}

View File

@@ -1,22 +1,9 @@
"use strict";
import prettier from "prettier";
// TODO: Remove this `if` in Babel 8
// Prettier only supports Node.js 10+, so we can fallback to not formatting
// o CI on older Node.js versions
export default function formatCode(code, filename) {
const prettierConfig = prettier.resolveConfig.sync(filename);
prettierConfig.filepath = filename;
prettierConfig.parser = filename.endsWith(".ts") ? "babel-ts" : "babel";
if (process.env.CI && parseInt(process.versions.node, 10) < 10) {
module.exports = function formatCode(code) {
return code;
};
} else {
const prettier = require("prettier");
module.exports = function formatCode(code, filename) {
filename = filename || __filename;
const prettierConfig = prettier.resolveConfig.sync(filename);
prettierConfig.filepath = filename;
prettierConfig.parser = filename.endsWith(".ts") ? "babel-ts" : "babel";
return prettier.format(code, prettierConfig);
};
return prettier.format(code, prettierConfig);
}