forked from aris-creator/jancok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
261 lines (237 loc) · 7.18 KB
/
build.js
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Produces production builds and stitches together d.ts files.
To specify the package to build, simply pass its name and the desired build
formats to output (defaults to `buildOptions.formats` specified in that package,
or "esm,cjs"):
```
# name supports fuzzy match. will build all packages with name containing "dom":
yarn build dom
# specify the format to output
yarn build core --formats cjs
```
*/
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");
const execa = require("execa");
const { gzipSync } = require("zlib");
const { compress } = require("brotli");
const {
targets: buildTargets,
allTargets,
fuzzyMatchTarget,
} = require("./utils");
const { build: esBuild } = require("esbuild");
const args = require("minimist")(process.argv.slice(2));
const isCIRun = !!args.ci;
const targets = args._;
const formats = args.formats || args.f;
const devOnly = args.devOnly || args.d;
const isRelease = args.release;
const buildTypes = args.t || args.types || isRelease || isCIRun;
const buildAllMatching = args.all || args.a;
run();
async function run() {
try {
if (!targets.length) {
const buildedCorrectly = await buildAll(buildTargets);
if (buildedCorrectly === false) process.exit(1);
if (isCIRun) {
for (let index = 0; index < allTargets.length; index++) {
const pkgDir = path.resolve(`packages/${allTargets[index]}`);
await execa("npx", ["yalc", "push"], {
stdio: "inherit",
cwd: pkgDir,
});
}
}
checkAllSizes(buildTargets);
} else {
await buildAll(fuzzyMatchTarget(targets, buildAllMatching));
checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching));
}
} catch (e) {
console.error("Build error", e);
process.exit(1);
}
}
async function buildAll(targets) {
for (const target of targets) {
const result = await build(target);
if (result === false) return false;
}
if (buildTypes) {
console.log(
chalk.bold(
chalk.yellow(
`Updating docs/landing/resources/api folder with public documentation`
)
)
);
if (!fs.existsSync("temp")) {
console.log(
chalk.bold(chalk.yellow(`No definition found in "temp" directory.`))
);
return;
}
await execa(
"api-documenter",
["markdown", "-i", "./temp", "-o", "./docs/landing/resources/api"],
{ stdio: "inherit" }
);
}
}
async function runBuild({ target, packageJson, format = "esm" }) {
const buildTarget = format === "esm" ? "es2020" : "es2015";
try {
const external = Object.keys(packageJson.dependencies);
await esBuild({
entryPoints: [path.join("packages", target, "src", "index.ts")],
outfile: path.join("packages", target, "dist", `${target}.${format}.js`),
minify: false,
bundle: true,
external,
target: buildTarget,
format,
});
return true;
} catch (e) {
console.error("Error building " + target, e);
return false;
}
}
async function buildPackage(target, packageJson) {
const formats =
(packageJson.buildOptions && packageJson.buildOptions.formats) || [];
const results = await Promise.all(
formats.map((format) => {
return runBuild({
target,
packageJson,
format,
});
})
);
return !results.includes(false);
}
async function build(target) {
console.log();
console.log(`Building ${chalk.bold(target)} package...`);
const { performance } = require("perf_hooks");
const t0 = performance.now();
const pkgDir = path.resolve(`packages/${target}`);
const pkg = require(`${pkgDir}/package.json`);
// run custom package build if there is one
if (pkg.scripts && pkg.scripts.build) {
await execa("yarn", ["build"], { stdio: "inherit", cwd: pkgDir });
return;
}
// if building a specific format, do not remove dist.
if (!formats) {
await fs.remove(`${pkgDir}/dist`);
}
const buildResult = await buildPackage(target, pkg);
if (!buildResult) return false;
const t1 = performance.now();
console.log(chalk.green(`Build success: ${Math.round(t1 - t0)} ms`));
if (buildTypes && pkg.types) {
console.log();
console.log(
chalk.bold(chalk.yellow(`Rolling up type definitions for ${target}...`))
);
try {
await execa(
"yarn",
[
"tsc",
"--emitDeclarationOnly",
"--declaration",
"--skipLibCheck",
"--project",
path.join("packages", target, `tsconfig-${target}.json`),
],
{
stdio: "inherit",
}
);
} catch (e) {
console.error("Problem with generationg declarations file", e);
return false;
}
// build types
const { Extractor, ExtractorConfig } = require("@microsoft/api-extractor");
const extractorConfigPath = path.resolve(pkgDir, `api-extractor.json`);
const extractorConfig = ExtractorConfig.loadFileAndPrepare(
extractorConfigPath
);
const result = Extractor.invoke(extractorConfig, {
localBuild: !isCIRun,
showVerboseMessages: true,
});
if (result.succeeded) {
// concat additional d.ts to rolled-up dts (mostly for JSX)
if (pkg.buildOptions && pkg.buildOptions.dts) {
const dtsPath = path.resolve(pkgDir, pkg.types);
const existing = await fs.readFile(dtsPath, "utf-8");
const toAdd = await Promise.all(
pkg.buildOptions.dts.map((file) => {
return fs.readFile(path.resolve(pkgDir, file), "utf-8");
})
);
await fs.writeFile(dtsPath, existing + "\n" + toAdd.join("\n"));
}
console.log(
chalk.bold(chalk.green(`API Extractor completed successfully.`))
);
} else {
console.error(
`API Extractor completed with ${result.errorCount} errors` +
` and ${result.warningCount} warnings`
);
console.log(
chalk.bold(
chalk.red(
`Probably public API has changed. You need to run 'yarn build' locally and commit generated public API documentation.`
)
)
);
process.exitCode = 1;
return false;
}
await fs.remove(`${pkgDir}/dist/packages`);
}
const t2 = performance.now();
console.log(chalk.green(`Whole package build: ${Math.round(t2 - t0)} ms`));
console.log();
}
function checkAllSizes(targets) {
if (devOnly) {
return;
}
console.log();
for (const target of targets) {
checkSize(target);
}
console.log();
}
function checkSize(target) {
const pkgDir = path.resolve(`packages/${target}`);
checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`);
checkFileSize(`${pkgDir}/dist/${target}.runtime.global.prod.js`);
}
function checkFileSize(filePath) {
if (!fs.existsSync(filePath)) {
return;
}
const file = fs.readFileSync(filePath);
const minSize = (file.length / 1024).toFixed(2) + "kb";
const gzipped = gzipSync(file);
const gzippedSize = (gzipped.length / 1024).toFixed(2) + "kb";
const compressed = compress(file);
const compressedSize = (compressed.length / 1024).toFixed(2) + "kb";
console.log(
`${chalk.gray(
chalk.bold(path.basename(filePath))
)} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}`
);
}