jiek 1.1.13 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -84
- package/bin/jiek-build.js +16 -0
- package/dist/cli-only-build.cjs +743 -0
- package/dist/cli-only-build.d.cts +91 -0
- package/dist/cli-only-build.d.ts +91 -0
- package/dist/cli-only-build.js +735 -0
- package/dist/cli.cjs +211 -560
- package/dist/cli.d.cts +0 -69
- package/dist/cli.d.ts +0 -69
- package/dist/cli.js +211 -560
- package/dist/index.d.cts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/rollup/index.cjs +86 -46
- package/dist/rollup/index.js +86 -44
- package/package.json +54 -11
- package/src/cli-only-build.ts +7 -0
- package/src/cli.ts +1 -7
- package/src/commands/base.ts +13 -3
- package/src/commands/build.ts +197 -39
- package/src/commands/descriptions.ts +12 -0
- package/src/commands/meta.ts +5 -0
- package/src/rollup/base.ts +40 -0
- package/src/rollup/index.ts +106 -37
- package/src/utils/filterSupport.ts +2 -6
@@ -0,0 +1,735 @@
|
|
1
|
+
import fs from 'node:fs';
|
2
|
+
import { createRequire } from 'node:module';
|
3
|
+
import path from 'node:path';
|
4
|
+
import { filterPackagesFromDir } from '@pnpm/filter-workspace-packages';
|
5
|
+
import { program } from 'commander';
|
6
|
+
import { load } from 'js-yaml';
|
7
|
+
import { isWorkspaceDir, getWorkspaceDir } from '@jiek/utils/getWorkspaceDir';
|
8
|
+
import { MultiBar, Presets } from 'cli-progress';
|
9
|
+
import { execaCommand } from 'execa';
|
10
|
+
|
11
|
+
let root;
|
12
|
+
function getRoot() {
|
13
|
+
if (root)
|
14
|
+
return root;
|
15
|
+
const rootOption = program.getOptionValue("root");
|
16
|
+
root = rootOption ? path.isAbsolute(rootOption) ? rootOption : path.resolve(process.cwd(), rootOption) : void 0;
|
17
|
+
return root;
|
18
|
+
}
|
19
|
+
|
20
|
+
let wd;
|
21
|
+
let notWorkspace$1 = false;
|
22
|
+
function getWD() {
|
23
|
+
if (wd)
|
24
|
+
return { wd, notWorkspace: notWorkspace$1 };
|
25
|
+
const root = getRoot();
|
26
|
+
if (root !== void 0) {
|
27
|
+
const isWorkspace = isWorkspaceDir(root, type$1);
|
28
|
+
notWorkspace$1 = !isWorkspace;
|
29
|
+
wd = root;
|
30
|
+
return { wd, notWorkspace: notWorkspace$1 };
|
31
|
+
}
|
32
|
+
try {
|
33
|
+
wd = getWorkspaceDir(type$1);
|
34
|
+
} catch (e) {
|
35
|
+
if ("message" in e && e.message === "workspace root not found") {
|
36
|
+
wd = root;
|
37
|
+
notWorkspace$1 = true;
|
38
|
+
} else {
|
39
|
+
throw e;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
return { wd, notWorkspace: notWorkspace$1 };
|
43
|
+
}
|
44
|
+
|
45
|
+
let type$1 = "";
|
46
|
+
try {
|
47
|
+
const require = createRequire(import.meta.url);
|
48
|
+
require.resolve("@pnpm/filter-workspace-packages");
|
49
|
+
type$1 = "pnpm";
|
50
|
+
} catch {
|
51
|
+
}
|
52
|
+
function filterPackagesGraph(filters) {
|
53
|
+
return Promise.all(filters.map(async (filter) => getSelectedProjectsGraph(filter)));
|
54
|
+
}
|
55
|
+
async function getSelectedProjectsGraph(filter = program.getOptionValue("filter")) {
|
56
|
+
let root = getRoot();
|
57
|
+
const { wd, notWorkspace } = getWD();
|
58
|
+
if (notWorkspace) {
|
59
|
+
return {
|
60
|
+
wd,
|
61
|
+
root,
|
62
|
+
value: {
|
63
|
+
[wd]: JSON.parse(fs.readFileSync(path.resolve(wd, "package.json"), "utf-8"))
|
64
|
+
}
|
65
|
+
};
|
66
|
+
}
|
67
|
+
if (type$1 === "pnpm") {
|
68
|
+
const pnpmWorkspaceFilePath = path.resolve(wd, "pnpm-workspace.yaml");
|
69
|
+
const pnpmWorkspaceFileContent = fs.readFileSync(pnpmWorkspaceFilePath, "utf-8");
|
70
|
+
const pnpmWorkspace = load(pnpmWorkspaceFileContent);
|
71
|
+
if (root === wd && !filter) {
|
72
|
+
throw new Error("root path is workspace root, please provide a filter");
|
73
|
+
}
|
74
|
+
if (root === void 0) {
|
75
|
+
root = process.cwd();
|
76
|
+
}
|
77
|
+
if (root !== wd && !filter) {
|
78
|
+
const packageJSONIsExist = fs.existsSync(path.resolve(root, "package.json"));
|
79
|
+
if (!packageJSONIsExist) {
|
80
|
+
throw new Error("root path is not workspace root, please provide a filter");
|
81
|
+
}
|
82
|
+
const packageJSON = JSON.parse(fs.readFileSync(path.resolve(root, "package.json"), "utf-8"));
|
83
|
+
if (!packageJSON.name) {
|
84
|
+
throw new Error("root path is not workspace root, please provide a filter");
|
85
|
+
}
|
86
|
+
filter = packageJSON.name;
|
87
|
+
}
|
88
|
+
const { selectedProjectsGraph } = await filterPackagesFromDir(wd, [{
|
89
|
+
filter: filter ?? "",
|
90
|
+
followProdDepsOnly: true
|
91
|
+
}], {
|
92
|
+
prefix: root,
|
93
|
+
workspaceDir: wd,
|
94
|
+
patterns: pnpmWorkspace.packages
|
95
|
+
});
|
96
|
+
return {
|
97
|
+
wd,
|
98
|
+
root,
|
99
|
+
value: Object.entries(selectedProjectsGraph).reduce((acc, [key, value]) => {
|
100
|
+
acc[key] = value.package.manifest;
|
101
|
+
return acc;
|
102
|
+
}, {})
|
103
|
+
};
|
104
|
+
}
|
105
|
+
throw new Error(`not supported package manager ${type$1}`);
|
106
|
+
}
|
107
|
+
|
108
|
+
var name = "jiek";
|
109
|
+
var type = "module";
|
110
|
+
var version = "2.0.1";
|
111
|
+
var description$1 = "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.";
|
112
|
+
var author = "YiJie <yijie4188@gmail.com>";
|
113
|
+
var repository = {
|
114
|
+
url: "nwylzw/jiek",
|
115
|
+
directory: "packages/jiek"
|
116
|
+
};
|
117
|
+
var homepage = "https://github.com/NWYLZW/jiek/tree/master/packages/jiek#readme";
|
118
|
+
var bugs = "https://github.com/NWYLZW/jiek/issues?q=is%3Aissue+is%3Aopen+jiek";
|
119
|
+
var bin = {
|
120
|
+
jiek: "bin/jiek.js",
|
121
|
+
jk: "bin/jiek.js",
|
122
|
+
"jiek-build": "bin/jiek-build.js",
|
123
|
+
jb: "bin/jiek-build.js"
|
124
|
+
};
|
125
|
+
var files = [
|
126
|
+
"dist",
|
127
|
+
"src",
|
128
|
+
"bin",
|
129
|
+
"LICENSE",
|
130
|
+
"README.md"
|
131
|
+
];
|
132
|
+
var scripts = {
|
133
|
+
prepublish: "jb --noMin"
|
134
|
+
};
|
135
|
+
var exports = {
|
136
|
+
"./package.json": "./package.json",
|
137
|
+
".": "./src/index.ts",
|
138
|
+
"./cli": "./src/cli.ts",
|
139
|
+
"./cli-only-build": "./src/cli-only-build.ts",
|
140
|
+
"./rollup": "./src/rollup/index.ts"
|
141
|
+
};
|
142
|
+
var imports = {
|
143
|
+
"#~/*": "./src/*"
|
144
|
+
};
|
145
|
+
var dependencies = {
|
146
|
+
"@jiek/pkger": "workspace:^",
|
147
|
+
"@jiek/rollup-plugin-dts": "^6.2.1",
|
148
|
+
"@jiek/utils": "workspace:^",
|
149
|
+
"@rollup/plugin-commonjs": "^28.0.0",
|
150
|
+
"@rollup/plugin-json": "^6.0.1",
|
151
|
+
"@rollup/plugin-node-resolve": "^15.3.0",
|
152
|
+
"cli-progress": "^3.12.0",
|
153
|
+
commander: "^12.0.0",
|
154
|
+
"detect-indent": "^6.1.0",
|
155
|
+
execa: "9.3.1",
|
156
|
+
inquirer: "^8.2.6",
|
157
|
+
"js-yaml": "^4.1.0",
|
158
|
+
"jsonc-parser": "^3.2.1",
|
159
|
+
rollup: "4.13.2"
|
160
|
+
};
|
161
|
+
var peerDependencies = {
|
162
|
+
"@rollup/plugin-terser": "^0.4.4",
|
163
|
+
"@pnpm/filter-workspace-packages": "^7.2.13",
|
164
|
+
"esbuild-register": "^3.5.0",
|
165
|
+
postcss: "^8.4.47",
|
166
|
+
"rollup-plugin-postcss": "^4.0.2",
|
167
|
+
"rollup-plugin-esbuild": "^6.1.0",
|
168
|
+
"rollup-plugin-swc3": "^0.12.1",
|
169
|
+
typescript: "^4.0.0||^5.0.0"
|
170
|
+
};
|
171
|
+
var peerDependenciesMeta = {
|
172
|
+
"@rollup/plugin-terser": {
|
173
|
+
optional: true
|
174
|
+
},
|
175
|
+
"@pnpm/filter-workspace-packages": {
|
176
|
+
optional: true
|
177
|
+
},
|
178
|
+
"esbuild-register": {
|
179
|
+
optional: true
|
180
|
+
},
|
181
|
+
postcss: {
|
182
|
+
optional: true
|
183
|
+
},
|
184
|
+
"rollup-plugin-postcss": {
|
185
|
+
optional: true
|
186
|
+
},
|
187
|
+
"rollup-plugin-esbuild": {
|
188
|
+
optional: true
|
189
|
+
},
|
190
|
+
"rollup-plugin-swc3": {
|
191
|
+
optional: true
|
192
|
+
},
|
193
|
+
typescript: {
|
194
|
+
optional: true
|
195
|
+
}
|
196
|
+
};
|
197
|
+
var devDependencies = {
|
198
|
+
"@npm/types": "^1.0.2",
|
199
|
+
"@pnpm/filter-workspace-packages": "^7.2.13",
|
200
|
+
"@pnpm/workspace.pkgs-graph": "^2.0.15",
|
201
|
+
"@rollup/plugin-terser": "^0.4.4",
|
202
|
+
"@types/cli-progress": "^3.11.5",
|
203
|
+
"@types/inquirer": "^9.0.7",
|
204
|
+
"@types/js-yaml": "^4.0.9",
|
205
|
+
"@types/micromatch": "^4.0.6",
|
206
|
+
"esbuild-register": "^3.5.0",
|
207
|
+
micromatch: "^4.0.5",
|
208
|
+
"node-sass": "^9.0.0",
|
209
|
+
postcss: "^8.4.47",
|
210
|
+
"rollup-plugin-postcss": "^4.0.2",
|
211
|
+
"rollup-plugin-esbuild": "^6.1.0",
|
212
|
+
"rollup-plugin-swc3": "^0.12.1"
|
213
|
+
};
|
214
|
+
var publishConfig = {
|
215
|
+
exports: {
|
216
|
+
"./package.json": "./package.json",
|
217
|
+
".": {
|
218
|
+
source: "./src/index.ts",
|
219
|
+
require: "./dist/index.cjs",
|
220
|
+
"default": "./dist/index.js"
|
221
|
+
},
|
222
|
+
"./cli": {
|
223
|
+
source: "./src/cli.ts",
|
224
|
+
require: "./dist/cli.cjs",
|
225
|
+
"default": "./dist/cli.js"
|
226
|
+
},
|
227
|
+
"./cli-only-build": {
|
228
|
+
source: "./src/cli-only-build.ts",
|
229
|
+
require: "./dist/cli-only-build.cjs",
|
230
|
+
"default": "./dist/cli-only-build.js"
|
231
|
+
},
|
232
|
+
"./rollup": {
|
233
|
+
source: "./src/rollup/index.ts",
|
234
|
+
require: "./dist/rollup/index.cjs",
|
235
|
+
"default": "./dist/rollup/index.js"
|
236
|
+
}
|
237
|
+
},
|
238
|
+
main: "./dist/index.cjs",
|
239
|
+
module: "./dist/index.js",
|
240
|
+
typesVersions: {
|
241
|
+
"<5.0": {
|
242
|
+
"*": [
|
243
|
+
"*",
|
244
|
+
"./dist/*",
|
245
|
+
"./dist/*/index.d.ts",
|
246
|
+
"./dist/*/index.d.mts",
|
247
|
+
"./dist/*/index.d.cts"
|
248
|
+
]
|
249
|
+
}
|
250
|
+
}
|
251
|
+
};
|
252
|
+
var pkg = {
|
253
|
+
name: name,
|
254
|
+
type: type,
|
255
|
+
version: version,
|
256
|
+
description: description$1,
|
257
|
+
author: author,
|
258
|
+
repository: repository,
|
259
|
+
homepage: homepage,
|
260
|
+
bugs: bugs,
|
261
|
+
bin: bin,
|
262
|
+
files: files,
|
263
|
+
scripts: scripts,
|
264
|
+
exports: exports,
|
265
|
+
imports: imports,
|
266
|
+
dependencies: dependencies,
|
267
|
+
peerDependencies: peerDependencies,
|
268
|
+
peerDependenciesMeta: peerDependenciesMeta,
|
269
|
+
devDependencies: devDependencies,
|
270
|
+
publishConfig: publishConfig
|
271
|
+
};
|
272
|
+
|
273
|
+
const entriesDescription = `
|
274
|
+
Specify the build entry-points of the package.json's 'exports' field.
|
275
|
+
Support glob pattern and array.
|
276
|
+
.e.g. '.', './*', './sub/*', './a,./b'.
|
277
|
+
`.trim();
|
278
|
+
const filterDescription = `
|
279
|
+
Filter the packages from the workspace.
|
280
|
+
Support fuzzy match and array.
|
281
|
+
.e.g. 'core,utils'.
|
282
|
+
`.trim();
|
283
|
+
const outdirDescription = `
|
284
|
+
The output directory of the build, which relative to the target subpackage root directory.
|
285
|
+
Support with variables: 'PKG_NAME',
|
286
|
+
.e.g. 'dist/{{PKG_NAME}}'.
|
287
|
+
`.trim();
|
288
|
+
|
289
|
+
const { notWorkspace } = getWD();
|
290
|
+
const IS_WORKSPACE = !notWorkspace;
|
291
|
+
|
292
|
+
program.name("jk/jiek").version(pkg.version).description(`${pkg.description} - Version ${pkg.version}`).option("--root <root>", "The root path of the project").option("-c, --config-path <configPath>", "Custom jiek config path");
|
293
|
+
if (type$1 !== "" && IS_WORKSPACE) {
|
294
|
+
program.option("-f, --filter <filter>", filterDescription);
|
295
|
+
}
|
296
|
+
|
297
|
+
const require$2 = createRequire(import.meta.url);
|
298
|
+
function packageIsExist(name) {
|
299
|
+
try {
|
300
|
+
require$2.resolve(name);
|
301
|
+
return true;
|
302
|
+
} catch (e) {
|
303
|
+
return false;
|
304
|
+
}
|
305
|
+
}
|
306
|
+
let tsRegisterName;
|
307
|
+
const registers = [
|
308
|
+
process.env.JIEK_TS_REGISTER,
|
309
|
+
"esbuild-register",
|
310
|
+
"@swc-node/register",
|
311
|
+
"ts-node/register"
|
312
|
+
].filter(Boolean);
|
313
|
+
for (const register of registers) {
|
314
|
+
if (packageIsExist(register)) {
|
315
|
+
tsRegisterName = register;
|
316
|
+
break;
|
317
|
+
}
|
318
|
+
}
|
319
|
+
|
320
|
+
const require$1 = createRequire(import.meta.url);
|
321
|
+
let configName = "jiek.config";
|
322
|
+
function getConfigPath(root, dir) {
|
323
|
+
const isSupportTsLoader = !!tsRegisterName;
|
324
|
+
function configWithExtIsExist(ext) {
|
325
|
+
const filenames = [
|
326
|
+
path.resolve(process.cwd(), `${configName}.${ext}`),
|
327
|
+
path.resolve(process.cwd(), `.${configName}.${ext}`),
|
328
|
+
path.resolve(root, `${configName}.${ext}`),
|
329
|
+
path.resolve(root, `.${configName}.${ext}`)
|
330
|
+
];
|
331
|
+
if (dir) {
|
332
|
+
filenames.unshift(...[
|
333
|
+
path.resolve(dir, `${configName}.${ext}`),
|
334
|
+
path.resolve(dir, `.${configName}.${ext}`)
|
335
|
+
]);
|
336
|
+
}
|
337
|
+
for (const filename of filenames) {
|
338
|
+
if (fs.existsSync(filename) && fs.lstatSync(filename).isFile()) {
|
339
|
+
return filename;
|
340
|
+
}
|
341
|
+
}
|
342
|
+
return;
|
343
|
+
}
|
344
|
+
configName = configWithExtIsExist("js") ?? configName;
|
345
|
+
configName = configWithExtIsExist("json") ?? configName;
|
346
|
+
configName = configWithExtIsExist("yaml") ?? configName;
|
347
|
+
if (isSupportTsLoader) {
|
348
|
+
configName = configWithExtIsExist("ts") ?? configName;
|
349
|
+
}
|
350
|
+
return path.resolve(root, configName);
|
351
|
+
}
|
352
|
+
function loadConfig(dirOrOptions) {
|
353
|
+
let dir;
|
354
|
+
let root;
|
355
|
+
if (typeof dirOrOptions === "object") {
|
356
|
+
dir = dirOrOptions.dir;
|
357
|
+
root = dirOrOptions.root ?? getWD().wd;
|
358
|
+
} else {
|
359
|
+
dir = dirOrOptions;
|
360
|
+
root = getWD().wd;
|
361
|
+
}
|
362
|
+
let configPath = program.getOptionValue("configPath");
|
363
|
+
if (!configPath) {
|
364
|
+
configPath = getConfigPath(root, dir);
|
365
|
+
} else {
|
366
|
+
if (!fs.existsSync(configPath)) {
|
367
|
+
throw new Error(`config file not found: ${configPath}`);
|
368
|
+
}
|
369
|
+
if (!path.isAbsolute(configPath)) {
|
370
|
+
configPath = path.resolve(root, configPath);
|
371
|
+
}
|
372
|
+
}
|
373
|
+
const ext = path.extname(configPath);
|
374
|
+
let module;
|
375
|
+
switch (ext) {
|
376
|
+
case ".js":
|
377
|
+
module = require$1(configPath);
|
378
|
+
break;
|
379
|
+
case ".json":
|
380
|
+
return require$1(configPath);
|
381
|
+
case ".yaml":
|
382
|
+
return load(fs.readFileSync(configPath, "utf-8"));
|
383
|
+
case ".ts":
|
384
|
+
if (tsRegisterName) {
|
385
|
+
require$1(tsRegisterName);
|
386
|
+
module = require$1(configPath);
|
387
|
+
break;
|
388
|
+
}
|
389
|
+
throw new Error(
|
390
|
+
"ts config file is not supported without ts register, please install esbuild-register or set JIEK_TS_REGISTER env for custom ts register"
|
391
|
+
);
|
392
|
+
case ".config":
|
393
|
+
module = {};
|
394
|
+
break;
|
395
|
+
default:
|
396
|
+
throw new Error(`unsupported config file type: ${ext}`);
|
397
|
+
}
|
398
|
+
if (!module)
|
399
|
+
throw new Error("config file is empty");
|
400
|
+
return module.default ?? module;
|
401
|
+
}
|
402
|
+
|
403
|
+
const BUILDER_TYPES = ["esbuild", "swc"];
|
404
|
+
const BUILDER_TYPE_PACKAGE_NAME_MAP = {
|
405
|
+
esbuild: "rollup-plugin-esbuild",
|
406
|
+
swc: "rollup-plugin-swc3"
|
407
|
+
};
|
408
|
+
|
409
|
+
const FILE_TEMPLATE = (manifest) => `
|
410
|
+
module.exports = require('jiek/rollup').template(${JSON.stringify(manifest, null, 2)})
|
411
|
+
`.trimStart();
|
412
|
+
const require = createRequire(import.meta.url);
|
413
|
+
const isDefault = process.env.JIEK_IS_ONLY_BUILD === "true";
|
414
|
+
const description = `
|
415
|
+
Build the package according to the 'exports' field from the package.json.
|
416
|
+
If you want to rewrite the \`rollup\` command options, you can pass the options after '--'.
|
417
|
+
${isDefault ? "This command is the default command." : ""}
|
418
|
+
`.trim();
|
419
|
+
async function checkDependency(dependency) {
|
420
|
+
try {
|
421
|
+
require.resolve(dependency);
|
422
|
+
} catch (e) {
|
423
|
+
console.error(`The package '${dependency}' is not installed, please install it first.`);
|
424
|
+
const answer = prompt("Do you want to install it now? (Y/n)", "Y");
|
425
|
+
const { notWorkspace } = getWD();
|
426
|
+
if (answer === "Y") {
|
427
|
+
await execaCommand(`pnpm install -${notWorkspace ? "" : "w"}D ${dependency}`);
|
428
|
+
} else {
|
429
|
+
return;
|
430
|
+
}
|
431
|
+
}
|
432
|
+
}
|
433
|
+
let DEFAULT_BUILDER_TYPE;
|
434
|
+
Object.entries(BUILDER_TYPE_PACKAGE_NAME_MAP).forEach(([type, packageName]) => {
|
435
|
+
try {
|
436
|
+
require.resolve(packageName);
|
437
|
+
DEFAULT_BUILDER_TYPE = type;
|
438
|
+
} catch {
|
439
|
+
}
|
440
|
+
});
|
441
|
+
if (!DEFAULT_BUILDER_TYPE) {
|
442
|
+
DEFAULT_BUILDER_TYPE = "esbuild";
|
443
|
+
}
|
444
|
+
function parseBoolean(v) {
|
445
|
+
if (v === void 0)
|
446
|
+
return true;
|
447
|
+
return Boolean(v);
|
448
|
+
}
|
449
|
+
const buildFilterDescription = `
|
450
|
+
${filterDescription}
|
451
|
+
If you pass the --filter option, it will merge into the filters of the command.
|
452
|
+
`.trim();
|
453
|
+
const buildEntriesDescription = `
|
454
|
+
${entriesDescription}
|
455
|
+
If you pass the --entries option, it will merge into the entries of the command.
|
456
|
+
`.trim();
|
457
|
+
const command = isDefault ? program.name("jb/jiek-build").helpCommand(false) : program;
|
458
|
+
if (IS_WORKSPACE) {
|
459
|
+
if (isDefault) {
|
460
|
+
command.argument("[filters]", buildFilterDescription);
|
461
|
+
} else {
|
462
|
+
command.command("build [filters]");
|
463
|
+
}
|
464
|
+
} else {
|
465
|
+
if (isDefault) {
|
466
|
+
command.argument("[entries]", buildEntriesDescription);
|
467
|
+
} else {
|
468
|
+
command.command("build [entries]");
|
469
|
+
}
|
470
|
+
}
|
471
|
+
command.description(description).option("-t, --type <TYPE>", `The type of build, support ${BUILDER_TYPES.map((s) => `"${s}"`).join(", ")}.`, (v) => {
|
472
|
+
if (!BUILDER_TYPES.includes(v)) {
|
473
|
+
throw new Error(`The value of 'type' must be ${BUILDER_TYPES.map((s) => `"${s}"`).join(", ")}`);
|
474
|
+
}
|
475
|
+
return String(v);
|
476
|
+
}, "esbuild").option("-o, --outdir <OUTDIR>", outdirDescription, String, "dist").option("-e, --entries <ENTRIES>", entriesDescription).option("--external <EXTERNAL>", "Specify the external dependencies of the package.", String).option("-nj, --noJs", "Do not output js files.", parseBoolean).option("-nd, --noDts", "Do not output dts files.", parseBoolean).option("-nm, --noMin", "Do not output minify files.", parseBoolean).option(
|
477
|
+
"--minType <MINTYPE>",
|
478
|
+
'The type of minify, support "builder" and "terser".',
|
479
|
+
(v) => {
|
480
|
+
if (!["builder", "terser"].includes(v)) {
|
481
|
+
throw new Error('The value of `minType` must be "builder" or "terser"');
|
482
|
+
}
|
483
|
+
return String(v);
|
484
|
+
}
|
485
|
+
).option("-nc, --noClean", "Do not clean the output directory before building.", parseBoolean).option(
|
486
|
+
"-om, --onlyMin",
|
487
|
+
"Only output minify files, but dts files will still be output, it only replaces the js files.",
|
488
|
+
parseBoolean
|
489
|
+
).option("--tsconfig <TSCONFIG>", "The path of the tsconfig file which is used to generate js and dts files.", String).option("--dtsconfig <DTSCONFIG>", "The path of the tsconfig file which is used to generate dts files.", String).option("-w, --watch", "Watch the file changes.", parseBoolean).option("-s, --silent", "Don't display logs.", parseBoolean).option("-v, --verbose", "Display debug logs.", parseBoolean).action(async (commandFiltersOrEntries, options) => {
|
490
|
+
let {
|
491
|
+
type,
|
492
|
+
outdir,
|
493
|
+
watch,
|
494
|
+
silent,
|
495
|
+
verbose,
|
496
|
+
entries: optionEntries,
|
497
|
+
external,
|
498
|
+
noJs: withoutJs,
|
499
|
+
noDts: withoutDts,
|
500
|
+
noMin: withoutMin,
|
501
|
+
minType: minifyType,
|
502
|
+
noClean,
|
503
|
+
onlyMin,
|
504
|
+
tsconfig,
|
505
|
+
dtsconfig
|
506
|
+
} = options;
|
507
|
+
const resolvedType = type ?? DEFAULT_BUILDER_TYPE;
|
508
|
+
if (!withoutJs) {
|
509
|
+
await checkDependency(BUILDER_TYPE_PACKAGE_NAME_MAP[resolvedType]);
|
510
|
+
if (minifyType === "builder") {
|
511
|
+
minifyType = resolvedType;
|
512
|
+
}
|
513
|
+
}
|
514
|
+
if (!withoutMin) {
|
515
|
+
await checkDependency(
|
516
|
+
{
|
517
|
+
...BUILDER_TYPE_PACKAGE_NAME_MAP,
|
518
|
+
terser: "@rollup/plugin-terser"
|
519
|
+
}[resolvedType]
|
520
|
+
);
|
521
|
+
}
|
522
|
+
let shouldPassThrough = false;
|
523
|
+
const passThroughOptions = process.argv.reduce(
|
524
|
+
(acc, value) => {
|
525
|
+
if (shouldPassThrough) {
|
526
|
+
acc.push(value);
|
527
|
+
}
|
528
|
+
if (value === "--") {
|
529
|
+
shouldPassThrough = true;
|
530
|
+
}
|
531
|
+
return acc;
|
532
|
+
},
|
533
|
+
[]
|
534
|
+
);
|
535
|
+
const { build } = loadConfig();
|
536
|
+
silent = silent ?? build?.silent ?? false;
|
537
|
+
if (withoutMin && onlyMin) {
|
538
|
+
throw new Error("Cannot use both --without-minify and --only-minify");
|
539
|
+
}
|
540
|
+
if (onlyMin && withoutJs) {
|
541
|
+
throw new Error("Cannot use --without-js and --only-minify at the same time");
|
542
|
+
}
|
543
|
+
let entries = [
|
544
|
+
optionEntries,
|
545
|
+
IS_WORKSPACE ? void 0 : commandFiltersOrEntries
|
546
|
+
].filter(Boolean).join(",");
|
547
|
+
if (entries.length === 0) {
|
548
|
+
entries = void 0;
|
549
|
+
}
|
550
|
+
const env = {
|
551
|
+
...process.env,
|
552
|
+
JIEK_BUILDER: type,
|
553
|
+
JIEK_OUT_DIR: outdir,
|
554
|
+
JIEK_CLEAN: String(!noClean),
|
555
|
+
JIEK_ENTRIES: entries,
|
556
|
+
JIEK_EXTERNAL: external,
|
557
|
+
JIEK_WITHOUT_JS: String(withoutJs),
|
558
|
+
JIEK_WITHOUT_DTS: String(withoutDts),
|
559
|
+
JIEK_WITHOUT_MINIFY: String(withoutMin),
|
560
|
+
JIEK_ONLY_MINIFY: String(onlyMin),
|
561
|
+
JIEK_MINIFY_TYPE: minifyType,
|
562
|
+
JIEK_TSCONFIG: tsconfig,
|
563
|
+
JIEK_DTSCONFIG: dtsconfig
|
564
|
+
};
|
565
|
+
const multiBars = new MultiBar({
|
566
|
+
clearOnComplete: false,
|
567
|
+
hideCursor: true,
|
568
|
+
format: "- {bar} | {status} | {pkgName} | {input} | {message}"
|
569
|
+
}, Presets.shades_classic);
|
570
|
+
const buildPackage = async ({
|
571
|
+
wd,
|
572
|
+
value = {}
|
573
|
+
}) => {
|
574
|
+
if (Object.keys(value).length === 0) {
|
575
|
+
throw new Error("no package found");
|
576
|
+
}
|
577
|
+
const wdNodeModules = path.resolve(wd, "node_modules");
|
578
|
+
if (!fs.existsSync(wdNodeModules)) {
|
579
|
+
fs.mkdirSync(wdNodeModules);
|
580
|
+
}
|
581
|
+
const jiekTempDir = (...paths) => path.resolve(wdNodeModules, ".jiek", ...paths);
|
582
|
+
if (!fs.existsSync(jiekTempDir())) {
|
583
|
+
fs.mkdirSync(jiekTempDir());
|
584
|
+
}
|
585
|
+
const rollupBinaryPath = require.resolve("rollup").replace(/dist\/rollup.js$/, "dist/bin/rollup");
|
586
|
+
let i = 0;
|
587
|
+
await Promise.all(
|
588
|
+
Object.entries(value).map(async ([dir, manifest]) => {
|
589
|
+
if (!manifest.name) {
|
590
|
+
throw new Error("package.json must have a name field");
|
591
|
+
}
|
592
|
+
const escapeManifestName = manifest.name.replace(/^@/g, "").replace(/\//g, "+");
|
593
|
+
const configFile = jiekTempDir(
|
594
|
+
`${escapeManifestName ?? `anonymous-${i++}`}.rollup.config.js`
|
595
|
+
);
|
596
|
+
fs.writeFileSync(configFile, FILE_TEMPLATE(manifest));
|
597
|
+
const command2 = [rollupBinaryPath, "--silent", "-c", configFile];
|
598
|
+
if (tsRegisterName) {
|
599
|
+
command2.unshift(`node -r ${tsRegisterName}`);
|
600
|
+
}
|
601
|
+
if (watch) {
|
602
|
+
command2.push("--watch");
|
603
|
+
}
|
604
|
+
command2.push(...passThroughOptions);
|
605
|
+
const child = execaCommand(command2.join(" "), {
|
606
|
+
ipc: true,
|
607
|
+
cwd: dir,
|
608
|
+
env: {
|
609
|
+
...env,
|
610
|
+
JIEK_NAME: manifest.name,
|
611
|
+
JIEK_ROOT: wd
|
612
|
+
}
|
613
|
+
});
|
614
|
+
const bars = {};
|
615
|
+
const times = {};
|
616
|
+
const locks = {};
|
617
|
+
let inputMaxLen = 10;
|
618
|
+
child.on("message", (e) => {
|
619
|
+
if (e.type === "debug")
|
620
|
+
console.log(...Array.isArray(e.data) ? e.data : [e.data]);
|
621
|
+
});
|
622
|
+
!silent && child.on("message", (e) => {
|
623
|
+
if (e.type === "init") {
|
624
|
+
const { leafMap, targetsLength } = e.data;
|
625
|
+
const leafs = Array.from(leafMap.entries()).flatMap(
|
626
|
+
([input, pathAndCondiions]) => pathAndCondiions.map(([path2, ...conditions]) => ({
|
627
|
+
input,
|
628
|
+
path: path2,
|
629
|
+
conditions
|
630
|
+
}))
|
631
|
+
);
|
632
|
+
let initMessage = `Package '${manifest.name}' has ${targetsLength} targets to build`;
|
633
|
+
if (watch) {
|
634
|
+
initMessage += " and watching...";
|
635
|
+
}
|
636
|
+
console.log(initMessage);
|
637
|
+
leafs.forEach(({ input }) => {
|
638
|
+
inputMaxLen = Math.max(inputMaxLen, input.length);
|
639
|
+
});
|
640
|
+
leafs.forEach(({ input, path: path2 }) => {
|
641
|
+
const key = `${input}:${path2}`;
|
642
|
+
if (bars[key])
|
643
|
+
return;
|
644
|
+
bars[key] = multiBars.create(50, 0, {
|
645
|
+
pkgName: manifest.name,
|
646
|
+
input: input.padEnd(inputMaxLen + 5),
|
647
|
+
status: "waiting".padEnd(10)
|
648
|
+
}, {
|
649
|
+
barsize: 20,
|
650
|
+
linewrap: true
|
651
|
+
});
|
652
|
+
});
|
653
|
+
}
|
654
|
+
if (e.type === "progress") {
|
655
|
+
const {
|
656
|
+
path: path2,
|
657
|
+
tags,
|
658
|
+
input,
|
659
|
+
event,
|
660
|
+
message
|
661
|
+
} = e.data;
|
662
|
+
const bar = bars[`${input}:${path2}`];
|
663
|
+
if (!bar)
|
664
|
+
return;
|
665
|
+
const time = times[`${input}:${path2}`];
|
666
|
+
bar.update(
|
667
|
+
{
|
668
|
+
start: 0,
|
669
|
+
resolve: 20,
|
670
|
+
end: 50
|
671
|
+
}[event ?? "start"] ?? 0,
|
672
|
+
{
|
673
|
+
input: (time ? `${input}(x${time.toString().padStart(2, "0")})` : input).padEnd(inputMaxLen + 5),
|
674
|
+
status: event?.padEnd(10),
|
675
|
+
message: `${tags?.join(", ")}: ${message}`
|
676
|
+
}
|
677
|
+
);
|
678
|
+
}
|
679
|
+
if (e.type === "watchChange") {
|
680
|
+
const {
|
681
|
+
path: path2,
|
682
|
+
input
|
683
|
+
} = e.data;
|
684
|
+
const key = `${input}:${path2}`;
|
685
|
+
const bar = bars[key];
|
686
|
+
if (!bar)
|
687
|
+
return;
|
688
|
+
let time = times[key] ?? 1;
|
689
|
+
if (!locks[key]) {
|
690
|
+
time += 1;
|
691
|
+
times[key] = time;
|
692
|
+
setTimeout(() => {
|
693
|
+
locks[key] = false;
|
694
|
+
}, 100);
|
695
|
+
bar.update(0, {
|
696
|
+
input: `${input}(x${time.toString().padStart(2, "0")})`.padEnd(inputMaxLen + 5),
|
697
|
+
status: "watching".padEnd(10),
|
698
|
+
message: "watching..."
|
699
|
+
});
|
700
|
+
}
|
701
|
+
locks[key] = true;
|
702
|
+
}
|
703
|
+
});
|
704
|
+
await new Promise((resolve, reject) => {
|
705
|
+
let errorStr = "";
|
706
|
+
child.stderr?.on("data", (data) => {
|
707
|
+
errorStr += data;
|
708
|
+
});
|
709
|
+
child.once("exit", (code) => code === 0 ? resolve() : reject(new Error(`rollup build failed:
|
710
|
+
${errorStr}`)));
|
711
|
+
verbose && child.stdout?.pipe(process.stdout);
|
712
|
+
});
|
713
|
+
})
|
714
|
+
);
|
715
|
+
};
|
716
|
+
const commandFilters = IS_WORKSPACE ? commandFiltersOrEntries : void 0;
|
717
|
+
const filters = [
|
718
|
+
.../* @__PURE__ */ new Set([
|
719
|
+
...program.getOptionValue("filter")?.split(",").map((s) => s.trim()).filter((s) => s.length > 0) ?? [],
|
720
|
+
...commandFilters?.split(",").map((s) => s.trim()).filter((s) => s.length > 0) ?? []
|
721
|
+
])
|
722
|
+
];
|
723
|
+
try {
|
724
|
+
if (filters.length > 0) {
|
725
|
+
const packages = await filterPackagesGraph(filters);
|
726
|
+
await Promise.all(packages.map(buildPackage));
|
727
|
+
} else {
|
728
|
+
await buildPackage(await getSelectedProjectsGraph());
|
729
|
+
}
|
730
|
+
} finally {
|
731
|
+
multiBars.stop();
|
732
|
+
}
|
733
|
+
});
|
734
|
+
|
735
|
+
program.parse(process.argv);
|