-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathbuild.mjs
129 lines (104 loc) · 3.5 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
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs-extra';
import { exec as execCb } from 'child_process';
import { promisify } from 'util';
import { rollup } from 'rollup';
import chalk from 'chalk';
import * as Terser from 'terser';
import { createConfig } from './config.mjs';
import { reportSize } from './info.mjs';
import { generateDts } from './generate-dts.mjs';
const exec = promisify(execCb);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
async function buildLocales() {
const localesDir = path.join(__dirname, '../packages/i18n/src/locale');
const files = fs.readdirSync(localesDir);
console.log(chalk.cyan('Building i18n...'));
for (let i = 0; i < files.length; i++) {
const file = files[i];
process.stdout.write(`${chalk.green(`Output File ${i}/${files.length}: `)} ${file}`);
const input = path.join(__dirname, '../packages/i18n/src/locale', file);
const out = path.join(__dirname, '../packages/i18n/dist/locale', file);
fs.copySync(input, out);
console.log('/n');
}
}
async function minify({ code, pkg, bundleName }) {
const pkgout = path.join(__dirname, `../packages/${pkg}/dist`);
const output = await Terser.minify(code, {
compress: true,
mangle: true,
});
const fileName = bundleName.replace(/\.js$/, '.prod.js');
const filePath = `${pkgout}/${fileName}.mjs`;
fs.outputFileSync(filePath, output.code);
const stats = reportSize({ code: output.code, path: filePath });
console.log(`${chalk.green('Output File:')} ${fileName} ${stats}`);
}
async function build(pkg) {
if (pkg === 'nuxt') {
console.log(chalk.magenta(`Generating bundle for ${pkg}`));
const result =
process.platform === 'win32'
? await exec('cd packages/nuxt && pnpm build && cd ../..')
: await exec('cd packages/nuxt && pnpm build && cd -');
console.log(result.stdout);
if (result.stderr) {
console.error(result.stderr);
process.exit(1);
}
console.log(`${chalk.magenta('✅ Bundled ' + pkg)}`);
return;
}
console.log(chalk.magenta(`Generating bundle for ${pkg}`));
const pkgout = path.join(__dirname, `../packages/${pkg}/dist`);
await fs.emptyDir(pkgout);
for (const format of ['esm', 'iife', 'cjs']) {
const { input, output, bundleName } = await createConfig(pkg, format);
const bundle = await rollup(input);
const {
output: [{ code }],
} = await bundle.generate(output);
const outputPath = path.join(pkgout, bundleName);
fs.outputFileSync(outputPath, code);
const stats = reportSize({ code, path: outputPath });
console.log(`${chalk.green('Output File:')} ${bundleName} ${stats}`);
if (format === 'iife') {
await minify({ bundleName, pkg, code });
}
}
await generateDts(pkg);
console.log(`${chalk.magenta('✅ Bundled ' + pkg)}`);
return true;
}
(async function Bundle() {
const arg = [...process.argv][2];
if (arg === 'vee-validate' || !arg) {
await build('vee-validate');
}
if (arg === 'rules' || !arg) {
await build('rules');
}
if (arg === 'zod' || !arg) {
await build('zod');
}
if (arg === 'yup' || !arg) {
await build('yup');
}
if (arg === 'valibot' || !arg) {
await build('valibot');
}
if (arg === 'nuxt' || !arg) {
await build('nuxt');
}
if (arg === 'joi' || !arg) {
await build('joi');
}
if (arg === 'i18n' || !arg) {
await build('i18n');
await buildLocales();
}
if (process.platform === 'win32') process.exit(0);
})();