forked from prettier/prettier
/
build.mjs
196 lines (170 loc) · 5.39 KB
/
build.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
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();
process.on("unhandledRejection", (err) => {
// No need to print it twice.
if (!loggedErrors.has(err)) {
console.error(err);
}
process.exit(1);
});
const CACHE_VERSION = "v38"; // This need update when updating build scripts
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));
}
function fitTerminal(input, suffix = "") {
const columns = Math.min(process.stdout.columns || 40, 80);
const WIDTH = columns - maxLength + 1;
if (input.length < WIDTH) {
const repeatCount = WIDTH - input.length - 1 - suffix.length;
input += chalk.dim(".").repeat(repeatCount) + suffix;
}
return input;
}
async function createBundle(bundleConfig, cache, options) {
const { output, target, format, type } = bundleConfig;
process.stdout.write(fitTerminal(output));
try {
const { cached, skipped } = await bundler(bundleConfig, cache, options);
if (skipped) {
console.log(status.SKIPPED);
return;
}
if (cached) {
console.log(status.CACHED);
return;
}
const file = path.join("dist", 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") {
const content = await fs.readFile(file, "utf8");
if (content.includes("\ufffe")) {
throw new Error("Bundled umd file should not have U+FFFE character.");
}
}
if (options["print-size"]) {
// Clear previous line
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
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"
) {
const esmFile = path.join("dist/esm", output.replace(".js", ".mjs"));
sizeTexts.push(`esm ${await getSizeText(esmFile)}`);
}
process.stdout.write(fitTerminal(output, `${sizeTexts.join(", ")} `));
}
console.log(status.DONE);
} catch (error) {
console.log(status.FAIL + "\n");
handleError(error);
}
}
function handleError(error) {
loggedErrors.add(error);
console.error(error);
throw error;
}
async function cacheFiles(cache) {
// Copy built files to .cache
try {
await execa("rm", ["-rf", path.join(".cache", "files")]);
await execa("mkdir", ["-p", path.join(".cache", "files")]);
await execa("mkdir", ["-p", path.join(".cache", "files", "esm")]);
const manifest = cache.updated;
for (const file of Object.keys(manifest.files)) {
await execa("cp", [
file,
path.join(".cache", file.replace("dist", "files")),
]);
}
} catch {
// Don't fail the build
}
}
async function preparePackage() {
const pkg = await utils.readJson("package.json");
pkg.bin = "./bin-prettier.js";
pkg.engines.node = ">=10.13.0";
delete pkg.dependencies;
delete pkg.devDependencies;
delete pkg.browserslist;
pkg.scripts = {
prepublishOnly:
"node -e \"assert.equal(require('.').version, require('..').version)\"",
};
pkg.files = ["*.js", "esm/*.mjs"];
await utils.writeJson("dist/package.json", pkg);
await utils.copyFile("./README.md", "./dist/README.md");
await utils.copyFile("./LICENSE", "./dist/LICENSE");
}
async function run(params) {
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 {
await execa("rm", ["-rf", "dist"]);
}
await execa("mkdir", ["-p", "dist"]);
await execa("mkdir", ["-p", "dist/esm"]);
if (params["purge-cache"]) {
await execa("rm", ["-rf", ".cache"]);
}
let bundleCache;
if (shouldUseCache) {
bundleCache = new Cache(".cache/", CACHE_VERSION);
await bundleCache.load();
}
console.log(chalk.inverse(" Building packages "));
for (const bundleConfig of configs) {
await createBundle(bundleConfig, bundleCache, params);
}
if (shouldUseCache) {
await cacheFiles(bundleCache);
await bundleCache.save();
}
if (shouldPreparePackage) {
await preparePackage();
}
}
run(
minimist(process.argv.slice(2), {
boolean: ["purge-cache", "playground", "print-size"],
string: ["file"],
})
);