Skip to content

Commit 9ed6f1e

Browse files
committed
refactor: build scripts
1 parent 163411a commit 9ed6f1e

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"build:dev": "cross-env NODE_ENV=development node scripts/build",
77
"build:prod": "cross-env NODE_ENV=production node scripts/build",
8+
"build:prod:core": "cross-env NODE_ENV=production node scripts/build --only core",
89
"build-icons-font": "python ./scripts/icon-font/generate.py",
910
"playground:dev": "cross-env NODE_ENV=development node scripts/playground",
1011
"playground:prod": "cross-env NODE_ENV=production node scripts/playground",

scripts/build-size.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

scripts/build.js

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,64 @@ const buildVue = require('./build-vue');
66
const buildSvelte = require('./build-svelte');
77
const buildStyles = require('./build-styles');
88
const buildAngular = require('./build-angular');
9+
const outputCheckSize = require('./check-size');
10+
11+
const env = process.env.NODE_ENV || 'development';
12+
const outputDir = env === 'development' ? 'build' : 'package';
13+
class Build {
14+
constructor() {
15+
this.argv = process.argv.slice(2).map((v) => v.toLowerCase());
16+
this.size = this.argv.includes('--size');
17+
this.tasks = [];
18+
return this;
19+
}
20+
21+
add(flag, buildFn) {
22+
if (!this.argv.includes('--only') || this.argv.includes(flag)) {
23+
this.tasks.push(buildFn);
24+
}
25+
return this;
26+
}
27+
28+
addMultipleFormats(flag, buildFn) {
29+
return this.add(flag, async () =>
30+
Promise.all(['esm', 'cjs'].map((format) => buildFn(format, outputDir))),
31+
);
32+
}
33+
34+
async run() {
35+
let start;
36+
let end;
37+
if (this.size) {
38+
start = outputCheckSize();
39+
}
40+
41+
const res = await Promise.all(this.tasks.map((v) => v())).catch((err) => {
42+
console.error(err);
43+
process.exit(1);
44+
});
45+
if (this.size) {
46+
const sizeMessage = (value, label = '') =>
47+
`difference ${label}: ${value > 0 ? `+${value}` : value} bytes`;
48+
49+
end = outputCheckSize();
50+
51+
console.log(sizeMessage(start.size - start.size));
52+
console.log(sizeMessage(end.gzippedSize - end.gzippedSize, 'gzipped'));
53+
}
54+
return res;
55+
}
56+
}
957

10-
const formats = ['esm', 'cjs'];
1158
(async () => {
12-
const env = process.env.NODE_ENV || 'development';
13-
const outputDir = env === 'development' ? 'build' : 'package';
14-
return Promise.all([
15-
buildJsBundle(),
16-
buildJsCore(),
17-
buildTypes(),
18-
Promise.all(formats.map((format) => buildReact(format, outputDir))),
19-
Promise.all(formats.map((format) => buildVue(format, outputDir))),
20-
Promise.all(formats.map((format) => buildSvelte(format, outputDir))),
21-
buildStyles(outputDir),
22-
buildAngular(),
23-
]).catch((err) => {
24-
console.error(err);
25-
process.exit(1);
26-
});
59+
await new Build()
60+
.add('core', buildJsCore)
61+
.add('bundle', buildJsBundle)
62+
.add('types', buildTypes)
63+
.addMultipleFormats('react', buildReact)
64+
.addMultipleFormats('vue', buildVue)
65+
.addMultipleFormats('svelte', buildSvelte)
66+
.add('angular', buildAngular)
67+
.add('styles', () => buildStyles(outputDir))
68+
.run();
2769
})();

0 commit comments

Comments
 (0)