Files
test-repo/scripts/build/build.mjs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

189 lines
5.0 KiB
JavaScript
Raw Normal View History

2021-05-15 20:09:52 +08:00
#!/usr/bin/env node
2021-05-15 20:09:52 +08:00
import path from "node:path";
import fs from "node:fs/promises";
import readline from "node:readline";
import chalk from "chalk";
import minimist from "minimist";
import prettyBytes from "pretty-bytes";
2021-09-10 10:20:43 +08:00
import rimraf from "rimraf";
import {
PROJECT_ROOT,
2021-09-11 09:55:46 +08:00
BUILD_CACHE_DIR,
2021-09-10 10:20:43 +08:00
DIST_DIR,
readJson,
writeJson,
copyFile,
2021-09-11 09:55:46 +08:00
} from "../utils/index.mjs";
import bundler from "./bundler.mjs";
import bundleConfigs from "./config.mjs";
2021-05-15 20:09:52 +08:00
import Cache from "./cache.mjs";
2018-05-29 14:59:57 -03:00
// Errors in promises should be fatal.
const loggedErrors = new Set();
process.on("unhandledRejection", (err) => {
2018-06-05 16:37:52 -03:00
// No need to print it twice.
if (!loggedErrors.has(err)) {
console.error(err);
2017-12-29 12:14:31 -05:00
}
2018-06-05 16:37:52 -03:00
process.exit(1);
2018-05-29 14:59:57 -03:00
});
2021-07-21 09:10:34 +08:00
const CACHE_VERSION = "v38"; // This need update when updating build scripts
2020-10-26 09:44:09 +08:00
const statusConfig = [
{ color: "bgYellow", text: "CACHED" },
{ color: "bgGreen", text: "DONE" },
{ color: "bgRed", text: "FAIL" },
{ color: "bgGray", text: "SKIPPED" },
];
const maxLength = Math.max(...statusConfig.map(({ text }) => text.length)) + 2;
const padStatusText = (text) => {
while (text.length < maxLength) {
text = text.length % 2 ? `${text} ` : ` ${text}`;
}
return text;
};
const status = {};
for (const { color, text } of statusConfig) {
status[text] = chalk[color].black(padStatusText(text));
}
2018-05-29 14:59:57 -03:00
function fitTerminal(input, suffix = "") {
2018-06-14 11:15:04 -03:00
const columns = Math.min(process.stdout.columns || 40, 80);
2020-10-26 09:44:09 +08:00
const WIDTH = columns - maxLength + 1;
2018-05-29 14:59:57 -03:00
if (input.length < WIDTH) {
const repeatCount = WIDTH - input.length - 1 - suffix.length;
input += chalk.dim(".").repeat(repeatCount) + suffix;
2018-05-24 15:30:45 -03:00
}
2018-05-29 14:59:57 -03:00
return input;
}
2020-10-26 09:44:09 +08:00
async function createBundle(bundleConfig, cache, options) {
const { output, target, format, type } = bundleConfig;
2018-05-29 14:59:57 -03:00
process.stdout.write(fitTerminal(output));
try {
2020-10-26 09:44:09 +08:00
const { cached, skipped } = await bundler(bundleConfig, cache, options);
if (skipped) {
console.log(status.SKIPPED);
return;
}
if (cached) {
2020-10-26 09:44:09 +08:00
console.log(status.CACHED);
return;
}
2021-09-10 10:20:43 +08:00
const file = path.join(DIST_DIR, output);
// Files including U+FFEE can't load in Chrome Extension
// `prettier-chrome-extension` https://github.com/prettier/prettier-chrome-extension
// details https://github.com/prettier/prettier/pull/8534
if (target === "universal") {
2021-01-09 02:09:40 +09:00
const content = await fs.readFile(file, "utf8");
if (content.includes("\ufffe")) {
throw new Error("Bundled umd file should not have U+FFFE character.");
2018-06-18 15:16:40 -03:00
}
}
if (options["print-size"]) {
// Clear previous line
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
2021-01-09 02:09:40 +09:00
const getSizeText = async (file) =>
prettyBytes((await fs.stat(file)).size);
const sizeTexts = [await getSizeText(file)];
if (
type !== "core" &&
format !== "esm" &&
bundleConfig.bundler !== "webpack" &&
target === "universal"
) {
2021-09-10 10:20:43 +08:00
const esmFile = path.join(
DIST_DIR,
"esm",
output.replace(".js", ".mjs")
);
2021-01-09 02:09:40 +09:00
sizeTexts.push(`esm ${await getSizeText(esmFile)}`);
}
2021-01-26 09:46:14 +08:00
process.stdout.write(fitTerminal(output, `${sizeTexts.join(", ")} `));
}
2020-10-26 09:44:09 +08:00
console.log(status.DONE);
} catch (error) {
2020-10-26 09:44:09 +08:00
console.log(status.FAIL + "\n");
handleError(error);
}
2018-05-24 15:30:45 -03:00
}
2018-05-29 14:59:57 -03:00
function handleError(error) {
loggedErrors.add(error);
console.error(error);
throw error;
2018-05-24 15:30:45 -03:00
}
2018-05-29 14:59:57 -03:00
async function preparePackage() {
2021-09-10 10:20:43 +08:00
const packageJson = await readJson(path.join(PROJECT_ROOT, "package.json"));
packageJson.bin = "./bin-prettier.js";
packageJson.engines.node = ">=10.13.0";
delete packageJson.dependencies;
delete packageJson.devDependencies;
delete packageJson.browserslist;
packageJson.scripts = {
2018-05-24 15:30:45 -03:00
prepublishOnly:
2018-05-29 00:22:59 -06:00
"node -e \"assert.equal(require('.').version, require('..').version)\"",
2018-05-24 15:30:45 -03:00
};
2021-09-10 10:20:43 +08:00
packageJson.files = ["*.js", "esm/*.mjs"];
await writeJson(path.join(DIST_DIR, "package.json"), packageJson);
2018-05-29 14:59:57 -03:00
2021-09-10 10:20:43 +08:00
for (const file of ["README.md", "LICENSE"]) {
await copyFile(path.join(PROJECT_ROOT, file), path.join(DIST_DIR, file));
}
2018-05-24 15:30:45 -03:00
}
2018-06-18 15:16:40 -03:00
async function run(params) {
2021-09-09 12:23:53 +08:00
const shouldUseCache = !params.file && !params["purge-cache"];
const shouldPreparePackage = !params.playground && !params.file;
let configs = bundleConfigs;
if (params.file) {
configs = configs.filter(({ output }) => output === params.file);
} else {
2021-09-10 10:20:43 +08:00
rimraf.sync(DIST_DIR);
2020-10-26 09:44:09 +08:00
}
2018-06-18 15:16:40 -03:00
if (params["purge-cache"]) {
2021-09-11 09:55:46 +08:00
rimraf.sync(BUILD_CACHE_DIR);
2018-06-18 15:16:40 -03:00
}
2021-09-09 12:23:53 +08:00
let bundleCache;
if (shouldUseCache) {
2021-09-10 10:20:43 +08:00
bundleCache = new Cache({
2021-09-11 09:55:46 +08:00
cacheDir: BUILD_CACHE_DIR,
2021-09-10 10:20:43 +08:00
distDir: DIST_DIR,
version: CACHE_VERSION,
});
2021-09-09 12:23:53 +08:00
await bundleCache.load();
}
2018-05-24 15:30:45 -03:00
2018-05-29 14:59:57 -03:00
console.log(chalk.inverse(" Building packages "));
2021-09-09 12:23:53 +08:00
for (const bundleConfig of configs) {
2020-10-26 09:44:09 +08:00
await createBundle(bundleConfig, bundleCache, params);
2018-05-24 15:30:45 -03:00
}
2021-09-09 12:23:53 +08:00
if (shouldUseCache) {
await bundleCache.save();
}
2018-06-18 15:16:40 -03:00
2021-09-09 12:23:53 +08:00
if (shouldPreparePackage) {
2020-10-26 09:44:09 +08:00
await preparePackage();
}
2018-05-24 15:30:45 -03:00
}
2018-06-18 15:16:40 -03:00
run(
minimist(process.argv.slice(2), {
boolean: ["purge-cache", "playground", "print-size"],
2021-09-09 12:23:53 +08:00
string: ["file"],
2018-06-18 15:16:40 -03:00
})
);