jiek 2.0.2-alpha.1 → 2.0.2-alpha.2

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