Switch some scripts to ESM (#10867)

This commit is contained in:
fisker Cheung authored and GitHub committed 2021-05-15 20:09:52 +08:00
1 parent 383f622942
commit f4a6232848
18 files changed
+139 -134

No files matched your search

+3
View File
@@ -98,6 +98,9 @@ overrides:
- "**/*.mjs"
parserOptions:
sourceType: module
rules:
unicorn/prefer-module: error
unicorn/prefer-node-protocol: error
- files:
- "tests/format/**/jsfmt.spec.js"
- "tests/config/**/*.js"
+5 -4
View File
@@ -115,6 +115,7 @@
"eslint-plugin-react": "7.23.2",
"eslint-plugin-regexp": "0.10.0",
"eslint-plugin-unicorn": "31.0.0",
"esm-utils": "1.0.1",
"execa": "5.0.0",
"jest": "26.6.3",
"jest-snapshot-serializer-ansi": "1.0.0",
@@ -148,15 +149,15 @@
"lint": "run-p lint:*",
"lint:typecheck": "tsc",
"lint:eslint": "cross-env EFF_NO_LINK_RULES=true eslint . --format friendly",
"lint:changelog": "node ./scripts/lint-changelog.js",
"lint:changelog": "node ./scripts/lint-changelog.mjs",
"lint:prettier": "prettier . \"!test*\" --check",
"lint:dist": "eslint --no-eslintrc --no-ignore --no-inline-config --env=es6,browser --parser-options=ecmaVersion:2019 \"dist/!(bin-prettier|index|third-party).js\"",
"lint:spellcheck": "cspell \"**/*\" \".github/**/*\"",
"lint:deps": "node ./scripts/check-deps.js",
"lint:deps": "node ./scripts/check-deps.mjs",
"fix": "run-s fix:eslint fix:prettier",
"fix:eslint": "yarn lint:eslint --fix",
"fix:prettier": "yarn lint:prettier --write",
"build": "node --max-old-space-size=3072 ./scripts/build/build.js",
"build-docs": "node ./scripts/build-docs.js"
"build": "node --max-old-space-size=3072 ./scripts/build/build.mjs",
"build-docs": "node ./scripts/build-docs.mjs"
}
}
@@ -1,15 +1,15 @@
#!/usr/bin/env node
"use strict";
const path = require("path");
const fs = require("fs");
const shell = require("shelljs");
const globby = require("globby");
const prettier = require("prettier");
import path from "node:path";
import fs from "node:fs";
import shell from "shelljs";
import globby from "globby";
import prettier from "prettier";
import createEsmUtils from "esm-utils";
shell.config.fatal = true;
const { __dirname, require } = createEsmUtils(import.meta);
const rootDir = path.join(__dirname, "..");
const docs = path.join(rootDir, "website/static/lib");
@@ -1,16 +1,16 @@
"use strict";
#!/usr/bin/env node
const path = require("path");
const fs = require("fs").promises;
const readline = require("readline");
const chalk = require("chalk");
const execa = require("execa");
const minimist = require("minimist");
const bundler = require("./bundler");
const bundleConfigs = require("./config");
const util = require("./util");
const Cache = require("./cache");
import path from "node:path";
import fs from "node:fs/promises";
import readline from "node:readline";
import chalk from "chalk";
import execa from "execa";
import minimist from "minimist";
import prettyBytes from "pretty-bytes";
import bundler from "./bundler.mjs";
import bundleConfigs from "./config.mjs";
import * as utils from "./utils.mjs";
import Cache from "./cache.mjs";
// Errors in promises should be fatal.
const loggedErrors = new Set();
@@ -84,7 +84,6 @@ async function createBundle(bundleConfig, cache, options) {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
const prettyBytes = require("pretty-bytes");
const getSizeText = async (file) =>
prettyBytes((await fs.stat(file)).size);
const sizeTexts = [await getSizeText(file)];
@@ -133,7 +132,7 @@ async function cacheFiles(cache) {
}
async function preparePackage() {
const pkg = await util.readJson("package.json");
const pkg = await utils.readJson("package.json");
pkg.bin = "./bin-prettier.js";
pkg.engines.node = ">=10.13.0";
delete pkg.dependencies;
@@ -143,10 +142,10 @@ async function preparePackage() {
"node -e \"assert.equal(require('.').version, require('..').version)\"",
};
pkg.files = ["*.js", "esm/*.mjs"];
await util.writeJson("dist/package.json", pkg);
await utils.writeJson("dist/package.json", pkg);
await util.copyFile("./README.md", "./dist/README.md");
await util.copyFile("./LICENSE", "./dist/LICENSE");
await utils.copyFile("./README.md", "./dist/README.md");
await utils.copyFile("./LICENSE", "./dist/LICENSE");
}
async function run(params) {
@@ -1,21 +1,22 @@
"use strict";
const path = require("path");
const fs = require("fs");
const { rollup } = require("rollup");
const webpack = require("webpack");
const { nodeResolve } = require("@rollup/plugin-node-resolve");
const rollupPluginAlias = require("@rollup/plugin-alias");
const commonjs = require("@rollup/plugin-commonjs");
const rollupPluginPolyfillNode = require("rollup-plugin-polyfill-node");
const json = require("@rollup/plugin-json");
const replace = require("@rollup/plugin-replace");
const { terser } = require("rollup-plugin-terser");
const { babel } = require("@rollup/plugin-babel");
const executable = require("./rollup-plugins/executable");
const evaluate = require("./rollup-plugins/evaluate");
const externals = require("./rollup-plugins/externals");
import path from "node:path";
import fs from "node:fs";
import { rollup } from "rollup";
import webpack from "webpack";
import { nodeResolve as rollupPluginNodeResolve } from "@rollup/plugin-node-resolve";
import rollupPluginAlias from "@rollup/plugin-alias";
import rollupPluginCommonjs from "@rollup/plugin-commonjs";
import rollupPluginPolyfillNode from "rollup-plugin-polyfill-node";
import rollupPluginJson from "@rollup/plugin-json";
import rollupPluginReplace from "@rollup/plugin-replace";
import { terser as rollupPluginTerser } from "rollup-plugin-terser";
import { babel as rollupPluginBabel } from "@rollup/plugin-babel";
import WebpackPluginTerser from "terser-webpack-plugin";
import createEsmUtils from "esm-utils";
import rollupPluginExecutable from "./rollup-plugins/executable.mjs";
import rollupPluginEvaluate from "./rollup-plugins/evaluate.mjs";
import rollupPluginExternals from "./rollup-plugins/externals.mjs";
const { __dirname, require } = createEsmUtils(import.meta);
const PROJECT_ROOT = path.join(__dirname, "../..");
const EXTERNALS = [
@@ -195,20 +196,20 @@ function getRollupConfig(bundle) {
alias.entries = [...entries, ...(alias.entries || [])];
config.plugins = [
replace({
rollupPluginReplace({
values: replaceStrings,
delimiters: ["", ""],
preventAssignment: true,
}),
executable(),
evaluate(),
json(),
rollupPluginExecutable(),
rollupPluginEvaluate(),
rollupPluginJson(),
rollupPluginAlias(alias),
nodeResolve({
rollupPluginNodeResolve({
extensions: [".js", ".json"],
preferBuiltins: bundle.target === "node",
}),
commonjs({
rollupPluginCommonjs({
ignoreGlobal: bundle.target === "node",
...bundle.commonjs,
ignore:
@@ -217,12 +218,12 @@ function getRollupConfig(bundle) {
: (id) => /\.\/parser-.*?/.test(id),
requireReturnsDefault: "preferred",
}),
externals(bundle.externals),
rollupPluginExternals(bundle.externals),
bundle.target === "universal" && rollupPluginPolyfillNode(),
babel(babelConfig),
rollupPluginBabel(babelConfig),
bundle.minify !== false &&
bundle.target === "universal" &&
terser({
rollupPluginTerser({
output: {
ascii_only: true,
},
@@ -311,8 +312,9 @@ function getWebpackConfig(bundle) {
};
if (bundle.terserOptions) {
const TerserPlugin = require("terser-webpack-plugin");
config.optimization.minimizer = [new TerserPlugin(bundle.terserOptions)];
config.optimization.minimizer = [
new WebpackPluginTerser(bundle.terserOptions),
];
}
// config.optimization.minimize = false;
@@ -345,7 +347,7 @@ function runWebpack(config) {
});
}
module.exports = async function createBundle(bundle, cache, options) {
async function createBundle(bundle, cache, options) {
const inputOptions = getRollupConfig(bundle);
const outputOptions = getRollupOutputOptions(bundle, options);
@@ -374,4 +376,6 @@ module.exports = async function createBundle(bundle, cache, options) {
}
return { bundled: true };
};
}
export default createBundle;
@@ -1,12 +1,12 @@
"use strict";
const assert = require("assert").strict;
const crypto = require("crypto");
const fs = require("fs").promises;
const path = require("path");
const execa = require("execa");
const { rollup } = require("rollup");
import { strict as assert } from "node:assert";
import crypto from "node:crypto";
import fs from "node:fs/promises";
import path from "node:path";
import execa from "execa";
import { rollup } from "rollup";
import createEsmUtils from "esm-utils";
const { __dirname } = createEsmUtils(import.meta);
const ROOT = path.join(__dirname, "..", "..");
class Cache {
@@ -146,4 +146,4 @@ function getRollupConfig(rollupConfig) {
};
}
module.exports = Cache;
export default Cache;
@@ -1,6 +1,4 @@
"use strict";
const path = require("path");
import path from "node:path";
/**
* @typedef {Object} Bundle
@@ -171,7 +169,7 @@ function getFileOutput(bundle) {
return bundle.output || path.basename(bundle.input);
}
module.exports = [...coreBundles, ...parsers].map((bundle) => ({
export default [...coreBundles, ...parsers].map((bundle) => ({
...bundle,
output: getFileOutput(bundle),
}));
@@ -1,6 +1,8 @@
"use strict";
import createEsmUtils from "esm-utils";
module.exports = function () {
const { require } = createEsmUtils(import.meta);
export default function () {
return {
name: "evaluate",
@@ -25,4 +27,4 @@ module.exports = function () {
};
},
};
};
}
@@ -1,9 +1,7 @@
"use strict";
import fs from "node:fs";
import path from "node:path";
const fs = require("fs");
const path = require("path");
module.exports = function () {
export default function () {
let banner;
let entry;
let file;
@@ -46,4 +44,4 @@ module.exports = function () {
}
},
};
};
}
@@ -1,8 +1,6 @@
"use strict";
import path from "node:path";
const path = require("path");
module.exports = function (modules = []) {
export default function (modules = []) {
const requires = modules.reduce((obj, mod) => {
obj[mod] = path.basename(mod).replace(/\.js$/, "");
return obj;
@@ -17,4 +15,4 @@ module.exports = function (modules = []) {
}
},
};
};
}
@@ -1,6 +1,4 @@
"use strict";
const fs = require("fs").promises;
import fs from "node:fs/promises";
async function readJson(file) {
const data = await fs.readFile(file);
@@ -17,8 +15,4 @@ async function copyFile(from, to) {
return fs.writeFile(to, data);
}
module.exports = {
readJson,
writeJson,
copyFile,
};
export { readJson, writeJson, copyFile };
@@ -1,10 +1,14 @@
"use strict";
import fs from "node:fs/promises";
import chalk from "chalk";
const chalk = require("chalk");
const pkg = require("../package.json");
(async () => {
const packageJson = JSON.parse(
await fs.readFile(new URL("../package.json", import.meta))
);
validateDependencyObject(pkg.dependencies);
validateDependencyObject(pkg.devDependencies);
validateDependencyObject(packageJson.dependencies);
validateDependencyObject(packageJson.devDependencies);
})();
function validateDependencyObject(object) {
for (const key of Object.keys(object)) {
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const globby = require("globby");
const changelogUnreleasedDir = path.join(__dirname, "../changelog_unreleased");
const files = globby.sync(["blog-post-intro.md", "*/*.md"], {
cwd: changelogUnreleasedDir,
absolute: true,
});
for (const file of files) {
fs.unlinkSync(file);
}
+17
View File
@@ -0,0 +1,17 @@
#!/usr/bin/env node
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import globby from "globby";
const changelogUnreleasedDir = fileURLToPath(
new URL("../changelog_unreleased", import.meta.url)
);
const files = globby.sync(["blog-post-intro.md", "*/*.md"], {
cwd: changelogUnreleasedDir,
absolute: true,
});
for (const file of files) {
fs.unlinkSync(file);
}
@@ -1,19 +1,16 @@
#!/usr/bin/env node
"use strict";
import fs from "node:fs/promises";
import execa from "execa";
const fs = require("fs");
const path = require("path");
const execa = require("execa");
const CSPELL_CONFIG_FILE = new URL("../cspell.json", import.meta.url);
const CSPELL_CONFIG_FILE = path.join(__dirname, "../cspell.json");
const updateConfig = (config) =>
fs.writeFileSync(CSPELL_CONFIG_FILE, JSON.stringify(config, undefined, 4));
const updateConfig = async (config) =>
await fs.writeFile(CSPELL_CONFIG_FILE, JSON.stringify(config, undefined, 4));
(async () => {
console.log("Empty words ...");
const config = JSON.parse(fs.readFileSync(CSPELL_CONFIG_FILE, "utf8"));
const config = JSON.parse(await fs.readFile(CSPELL_CONFIG_FILE, "utf8"));
updateConfig({ ...config, words: [] });
console.log("Running spellcheck with empty words ...");
@@ -1,12 +1,12 @@
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const rimraf = require("rimraf");
const semver = require("semver");
import fs from "node:fs";
import path from "node:path";
import rimraf from "rimraf";
import semver from "semver";
import createEsmUtils from "esm-utils";
const { __dirname, require } = createEsmUtils(import.meta);
const changelogUnreleasedDir = path.join(__dirname, "../changelog_unreleased");
const blogDir = path.join(__dirname, "../website/blog");
const introTemplateFile = path.join(
@@ -1,9 +1,11 @@
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const { outdent } = require("outdent");
import path from "node:path";
import fs from "node:fs";
import { outdent } from "outdent";
import createEsmUtils from "esm-utils";
const { __dirname } = createEsmUtils(import.meta);
const CHANGELOG_DIR = "changelog_unreleased";
const TEMPLATE_FILE = "TEMPLATE.md";
const BLOG_POST_INTRO_TEMPLATE_FILE = "BLOG_POST_INTRO_TEMPLATE.md";
+5
View File
@@ -3263,6 +3263,11 @@ eslint@7.26.0:
text-table "^0.2.0"
v8-compile-cache "^2.0.3"
esm-utils@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/esm-utils/-/esm-utils-1.0.1.tgz#297085fbb352140c70789744c0b858fd43f0b391"
integrity sha512-flJFfIoZmKFiz2wqz++NVT4rUia3ht80fDKmixuhmvCdf4JZyAx9RO5SSzfZY5Rv7/vBhB1xYzzFUM5qt1MBBg==
espree@7.3.1, espree@^7.3.0, espree@^7.3.1:
version "7.3.1"
resolved "https://registry.yarnpkg.com/espree/-/espree-7.3.1.tgz#f2df330b752c6f55019f8bd89b7660039c1bbbb6"