jiek 2.1.12 → 2.1.13-alpha.2

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/bin-helper.cjs CHANGED
@@ -1,6 +1,24 @@
1
- const {
2
- JIEK_BIN__FILEPATH: binFilePath
3
- } = process.env
1
+ const process = require('node:process')
2
+
3
+ /**
4
+ * @type {string | undefined}
5
+ */
6
+ let binFilePath
7
+
8
+ try {
9
+ // eslint-disable-next-line unicorn/error-message
10
+ throw new Error()
11
+ } catch (e) {
12
+ const { stack } = e
13
+ const lines = stack.split('\n')
14
+ const caller = lines[lines.length - 1]
15
+ const match = caller.match(/\(([^)]+)\)$/)
16
+ if (match) {
17
+ binFilePath = match[1].replace(/:\d+:\d+$/, '')
18
+ }
19
+ }
20
+
21
+ binFilePath = binFilePath ?? process.env.JIEK_BIN__FILEPATH
4
22
 
5
23
  const {
6
24
  basename,
@@ -10,6 +10,7 @@ var process$1 = require('node:process');
10
10
  var prompts = require('@inquirer/prompts');
11
11
  var cliProgress = require('cli-progress');
12
12
  var execa = require('execa');
13
+ var Koa = require('koa');
13
14
 
14
15
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
15
16
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -17,6 +18,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
17
18
  var fs__default = /*#__PURE__*/_interopDefault(fs);
18
19
  var path__default = /*#__PURE__*/_interopDefault(path);
19
20
  var process__default = /*#__PURE__*/_interopDefault(process$1);
21
+ var Koa__default = /*#__PURE__*/_interopDefault(Koa);
20
22
 
21
23
  let root;
22
24
  function getRoot() {
@@ -118,7 +120,7 @@ async function getSelectedProjectsGraph(filter = commander.program.getOptionValu
118
120
 
119
121
  var name = "jiek";
120
122
  var type = "module";
121
- var version = "2.1.11";
123
+ var version = "2.1.13-alpha.1";
122
124
  var description$1 = "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.";
123
125
  var author = "YiJie <yijie4188@gmail.com>";
124
126
  var homepage = "https://github.com/NWYLZW/jiek/tree/master/packages/jiek#readme";
@@ -127,6 +129,16 @@ var repository = {
127
129
  directory: "packages/jiek"
128
130
  };
129
131
  var bugs = "https://github.com/NWYLZW/jiek/issues?q=is%3Aissue+is%3Aopen+jiek";
132
+ var keywords = [
133
+ "cli",
134
+ "zero-config",
135
+ "bundler",
136
+ "library",
137
+ "monorepo",
138
+ "builder",
139
+ "rollup",
140
+ "workspace"
141
+ ];
130
142
  var exports$1 = {
131
143
  "./package.json": "./package.json",
132
144
  ".": "./src/index.ts",
@@ -157,7 +169,7 @@ var files = [
157
169
  "src"
158
170
  ];
159
171
  var scripts = {
160
- prepublish: "jb -nm && jk",
172
+ prepublish: "pnpm test && jb -nm && jk",
161
173
  postpublish: "jk",
162
174
  test: "vitest run"
163
175
  };
@@ -170,7 +182,7 @@ var peerDependencies = {
170
182
  "rollup-plugin-postcss": "^4.0.2",
171
183
  "rollup-plugin-swc3": "^0.12.1",
172
184
  typescript: "^4.0.0||^5.0.0",
173
- "vite-bundle-analyzer": "^0.15.1"
185
+ "vite-bundle-analyzer": "^0.15.2"
174
186
  };
175
187
  var dependencies = {
176
188
  "@inquirer/prompts": "^7.1.0",
@@ -188,6 +200,7 @@ var dependencies = {
188
200
  execa: "~9.3.1",
189
201
  "js-yaml": "^4.1.0",
190
202
  "jsonc-parser": "^3.2.1",
203
+ koa: "^2.15.3",
191
204
  rollup: "^4.0.0"
192
205
  };
193
206
  var devDependencies = {
@@ -198,6 +211,7 @@ var devDependencies = {
198
211
  "@types/cli-progress": "^3.11.5",
199
212
  "@types/inquirer": "^9.0.7",
200
213
  "@types/js-yaml": "^4.0.9",
214
+ "@types/koa": "^2.15.0",
201
215
  "@types/micromatch": "^4.0.6",
202
216
  "esbuild-register": "^3.5.0",
203
217
  micromatch: "^4.0.5",
@@ -206,7 +220,7 @@ var devDependencies = {
206
220
  "rollup-plugin-esbuild": "^6.1.0",
207
221
  "rollup-plugin-postcss": "^4.0.2",
208
222
  "rollup-plugin-swc3": "^0.12.1",
209
- "vite-bundle-analyzer": "^0.15.1"
223
+ "vite-bundle-analyzer": "^0.15.2"
210
224
  };
211
225
  var pkg = {
212
226
  name: name,
@@ -217,6 +231,7 @@ var pkg = {
217
231
  homepage: homepage,
218
232
  repository: repository,
219
233
  bugs: bugs,
234
+ keywords: keywords,
220
235
  exports: exports$1,
221
236
  imports: imports,
222
237
  bin: bin,
@@ -251,6 +266,32 @@ if (type$1 !== "" && IS_WORKSPACE) {
251
266
  commander.program.option("-f, --filter <filter>", filterDescription);
252
267
  }
253
268
 
269
+ const BUILDER_TYPES = ["esbuild", "swc"];
270
+ const BUILDER_TYPE_PACKAGE_NAME_MAP = {
271
+ esbuild: "rollup-plugin-esbuild",
272
+ swc: "rollup-plugin-swc3"
273
+ };
274
+
275
+ const createServer = (port, host) => {
276
+ const app = new Koa__default.default();
277
+ app.listen(port, host);
278
+ const streams = /* @__PURE__ */ new Map();
279
+ app.use(async (ctx) => {
280
+ const stream = streams.get(ctx.path);
281
+ if (stream != null) {
282
+ ctx.body = stream;
283
+ }
284
+ });
285
+ return {
286
+ port,
287
+ host,
288
+ rootUrl: `http://${host}:${port}`,
289
+ renderTo: async (path, stream) => {
290
+ streams.set(path, stream);
291
+ }
292
+ };
293
+ };
294
+
254
295
  const require$3 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-only-build.cjs', document.baseURI).href)));
255
296
  function packageIsExist(name) {
256
297
  try {
@@ -354,12 +395,6 @@ function loadConfig(dirOrOptions) {
354
395
  return module.default ?? module;
355
396
  }
356
397
 
357
- const BUILDER_TYPES = ["esbuild", "swc"];
358
- const BUILDER_TYPE_PACKAGE_NAME_MAP = {
359
- esbuild: "rollup-plugin-esbuild",
360
- swc: "rollup-plugin-swc3"
361
- };
362
-
363
398
  const FILE_TEMPLATE = (manifest) => `
364
399
  module.exports = require('jiek/rollup').template(${JSON.stringify(manifest, null, 2)})
365
400
  `.trimStart();
@@ -438,8 +473,8 @@ command = command.description(description).option("-t, --type <TYPE>", `The type
438
473
  parseBoolean
439
474
  );
440
475
  command = command.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);
441
- command = command.option("-w, --watch", "Watch the file changes.", parseBoolean);
442
- command = command.option("--ana", "Enable the bundle analyzer.", parseBoolean).option("--ana.dir <DIR>", "The directory of the bundle analyzer.", ".jk-analyses").option("--ana.mode <MODE>", 'The mode of the bundle analyzer, support "static", "json" and "server".', "server").option("--ana.port <PORT>", "The port of the bundle analyzer.", Number.parseInt).option("--ana.open", "Open the bundle analyzer in the browser.", parseBoolean).option(
476
+ command = command.option("-w, --watch", "Watch the file changes.", parseBoolean).option("-p, --port <PORT>", "The port of the server.", Number.parseInt, 8888);
477
+ command = command.option("--ana", "Enable the bundle analyzer.", parseBoolean).option("--ana.dir <DIR>", "The directory of the bundle analyzer.", ".jk-analyses").option("--ana.mode <MODE>", 'The mode of the bundle analyzer, support "static", "json" and "server".', "server").option("--ana.open", "Open the bundle analyzer in the browser.", parseBoolean).option(
443
478
  "--ana.size <SIZE>",
444
479
  'The default size of the bundle analyzer, support "stat", "parsed" and "gzip".',
445
480
  "parsed"
@@ -491,16 +526,43 @@ command.action(async (commandFiltersOrEntries, options) => {
491
526
  },
492
527
  []
493
528
  );
529
+ const modules = [];
530
+ const cjsModules = [];
531
+ const esmModules = [];
532
+ let render;
494
533
  const analyzer = options.ana ? {
495
534
  dir: options["ana.dir"],
496
535
  mode: options["ana.mode"],
497
- port: options["ana.port"],
498
536
  open: options["ana.open"],
499
537
  size: options["ana.size"]
500
538
  } : void 0;
539
+ if (options.ana && ![
540
+ "stat",
541
+ "parsed",
542
+ "gzip"
543
+ ].includes(analyzer?.size ?? "")) {
544
+ throw new Error('The value of `ana.size` must be "stat", "parsed" or "gzip"');
545
+ }
546
+ const server = analyzer && createServer(options.port, "localhost");
501
547
  if (analyzer) {
502
548
  await checkDependency("vite-bundle-analyzer");
549
+ const { renderView } = await import('vite-bundle-analyzer');
550
+ render = renderView;
503
551
  }
552
+ const anaPaths = /* @__PURE__ */ new Set();
553
+ const refreshAnalyzer = async (subPath = "", renderModules = modules) => {
554
+ if (!(analyzer && server && render))
555
+ return;
556
+ const p = `/ana${subPath}`;
557
+ anaPaths.add(p);
558
+ void server.renderTo(
559
+ p,
560
+ await render(renderModules, {
561
+ title: `Jiek Analyzer - ${subPath}`,
562
+ mode: analyzer.size
563
+ })
564
+ );
565
+ };
504
566
  const { build } = loadConfig();
505
567
  silent = silent ?? build?.silent ?? false;
506
568
  if (withoutMin && onlyMin) {
@@ -545,12 +607,16 @@ command.action(async (commandFiltersOrEntries, options) => {
545
607
  throw new Error("no package found");
546
608
  }
547
609
  const wdNodeModules = path__default.default.resolve(wd, "node_modules");
548
- if (!fs__default.default.existsSync(wdNodeModules)) {
549
- fs__default.default.mkdirSync(wdNodeModules);
610
+ if (!fs.existsSync(wdNodeModules)) {
611
+ fs.mkdirSync(wdNodeModules);
550
612
  }
551
- const jiekTempDir = (...paths) => path__default.default.resolve(wdNodeModules, ".jiek", ...paths);
552
- if (!fs__default.default.existsSync(jiekTempDir())) {
553
- fs__default.default.mkdirSync(jiekTempDir());
613
+ const resolveByJiekTemp = (...paths) => path__default.default.resolve(wdNodeModules, ".jiek", ...paths);
614
+ const jiekTemp = resolveByJiekTemp();
615
+ if (!fs.existsSync(jiekTemp)) {
616
+ try {
617
+ fs.mkdirSync(jiekTemp);
618
+ } catch {
619
+ }
554
620
  }
555
621
  const rollupBinaryPath = require$1.resolve("rollup").replace(/dist\/rollup.js$/, "dist/bin/rollup");
556
622
  let i = 0;
@@ -562,25 +628,25 @@ command.action(async (commandFiltersOrEntries, options) => {
562
628
  if (analyzer) {
563
629
  const anaDir = path__default.default.resolve(dir, analyzer.dir);
564
630
  if (!fs.existsSync(anaDir)) {
565
- fs__default.default.mkdirSync(anaDir, { recursive: true });
631
+ fs.mkdirSync(anaDir, { recursive: true });
566
632
  }
567
633
  const gitIgnorePath = path__default.default.resolve(anaDir, ".gitignore");
568
634
  if (!fs.existsSync(gitIgnorePath)) {
569
- fs__default.default.writeFileSync(gitIgnorePath, "*\n!.gitignore\n");
635
+ fs.writeFileSync(gitIgnorePath, "*\n!.gitignore\n");
570
636
  }
571
637
  const npmIgnorePath = path__default.default.resolve(anaDir, ".npmignore");
572
638
  if (!fs.existsSync(npmIgnorePath)) {
573
- fs__default.default.writeFileSync(npmIgnorePath, "*\n");
639
+ fs.writeFileSync(npmIgnorePath, "*\n");
574
640
  }
575
641
  if (!fs.statSync(anaDir).isDirectory()) {
576
642
  throw new Error(`The directory '${anaDir}' is not a directory.`);
577
643
  }
578
644
  }
579
645
  const escapeManifestName = manifest.name.replace(/^@/g, "").replace(/\//g, "+");
580
- const configFile = jiekTempDir(
646
+ const configFile = resolveByJiekTemp(
581
647
  `${escapeManifestName ?? `anonymous-${i++}`}.rollup.config.js`
582
648
  );
583
- fs__default.default.writeFileSync(configFile, FILE_TEMPLATE(manifest));
649
+ fs.writeFileSync(configFile, FILE_TEMPLATE(manifest));
584
650
  const command2 = [rollupBinaryPath, "--silent", "-c", configFile];
585
651
  if (tsRegisterName != null) {
586
652
  command2.unshift(`node -r ${tsRegisterName}`);
@@ -603,89 +669,141 @@ command.action(async (commandFiltersOrEntries, options) => {
603
669
  const locks = {};
604
670
  let inputMaxLen = 10;
605
671
  child.on("message", (e) => {
606
- if (e.type === "debug")
607
- console.log(...Array.isArray(e.data) ? e.data : [e.data]);
608
- });
609
- !silent && child.on("message", (e) => {
610
- if (e.type === "init") {
611
- const { leafMap, targetsLength } = e.data;
612
- const leafs = Array.from(leafMap.entries()).flatMap(
613
- ([input, pathAndCondiions]) => pathAndCondiions.map(([path2, ...conditions]) => ({
614
- input,
672
+ if (silent && [
673
+ "init",
674
+ "progress",
675
+ "watchChange"
676
+ ].includes(e.type))
677
+ return;
678
+ switch (e.type) {
679
+ case "init": {
680
+ const { leafMap, targetsLength } = e.data;
681
+ const leafs = Array.from(leafMap.entries()).flatMap(
682
+ ([input, pathAndCondiions]) => pathAndCondiions.map(([path2, ...conditions]) => ({
683
+ input,
684
+ path: path2,
685
+ conditions
686
+ }))
687
+ );
688
+ let initMessage = `Package '${manifest.name}' has ${targetsLength} targets to build`;
689
+ if (watch) {
690
+ initMessage += " and watching...";
691
+ }
692
+ console.log(initMessage);
693
+ leafs.forEach(({ input }) => {
694
+ inputMaxLen = Math.max(inputMaxLen, input.length);
695
+ });
696
+ leafs.forEach(({ input, path: path2 }) => {
697
+ const key = `${input}:${path2}`;
698
+ if (bars[key])
699
+ return;
700
+ bars[key] = multiBars.create(50, 0, {
701
+ pkgName: manifest.name,
702
+ input: input.padEnd(inputMaxLen + 5),
703
+ status: "waiting".padEnd(10)
704
+ }, {
705
+ barsize: 20,
706
+ linewrap: true
707
+ });
708
+ });
709
+ break;
710
+ }
711
+ case "progress": {
712
+ const {
615
713
  path: path2,
616
- conditions
617
- }))
618
- );
619
- let initMessage = `Package '${manifest.name}' has ${targetsLength} targets to build`;
620
- if (watch) {
621
- initMessage += " and watching...";
714
+ tags,
715
+ input,
716
+ event,
717
+ message
718
+ } = e.data;
719
+ const bar = bars[`${input}:${path2}`];
720
+ if (!bar)
721
+ return;
722
+ const time = times[`${input}:${path2}`];
723
+ bar.update(
724
+ {
725
+ start: 0,
726
+ resolve: 20,
727
+ end: 50
728
+ }[event ?? "start"] ?? 0,
729
+ {
730
+ input: (time ? `${input}(x${time.toString().padStart(2, "0")})` : input).padEnd(inputMaxLen + 5),
731
+ status: event?.padEnd(10),
732
+ message: `${tags?.join(", ")}: ${message}`
733
+ }
734
+ );
735
+ break;
622
736
  }
623
- console.log(initMessage);
624
- leafs.forEach(({ input }) => {
625
- inputMaxLen = Math.max(inputMaxLen, input.length);
626
- });
627
- leafs.forEach(({ input, path: path2 }) => {
737
+ case "watchChange": {
738
+ const {
739
+ path: path2,
740
+ input
741
+ } = e.data;
628
742
  const key = `${input}:${path2}`;
629
- if (bars[key])
743
+ const bar = bars[key];
744
+ if (!bar)
630
745
  return;
631
- bars[key] = multiBars.create(50, 0, {
632
- pkgName: manifest.name,
633
- input: input.padEnd(inputMaxLen + 5),
634
- status: "waiting".padEnd(10)
635
- }, {
636
- barsize: 20,
637
- linewrap: true
638
- });
639
- });
640
- }
641
- if (e.type === "progress") {
642
- const {
643
- path: path2,
644
- tags,
645
- input,
646
- event,
647
- message
648
- } = e.data;
649
- const bar = bars[`${input}:${path2}`];
650
- if (!bar)
651
- return;
652
- const time = times[`${input}:${path2}`];
653
- bar.update(
654
- {
655
- start: 0,
656
- resolve: 20,
657
- end: 50
658
- }[event ?? "start"] ?? 0,
659
- {
660
- input: (time ? `${input}(x${time.toString().padStart(2, "0")})` : input).padEnd(inputMaxLen + 5),
661
- status: event?.padEnd(10),
662
- message: `${tags?.join(", ")}: ${message}`
746
+ let time = times[key] ?? 1;
747
+ if (!locks[key]) {
748
+ time += 1;
749
+ times[key] = time;
750
+ setTimeout(() => {
751
+ locks[key] = false;
752
+ }, 100);
753
+ bar.update(0, {
754
+ input: `${input}(x${time.toString().padStart(2, "0")})`.padEnd(inputMaxLen + 5),
755
+ status: "watching".padEnd(10),
756
+ message: "watching..."
757
+ });
663
758
  }
664
- );
665
- }
666
- if (e.type === "watchChange") {
667
- const {
668
- path: path2,
669
- input
670
- } = e.data;
671
- const key = `${input}:${path2}`;
672
- const bar = bars[key];
673
- if (!bar)
674
- return;
675
- let time = times[key] ?? 1;
676
- if (!locks[key]) {
677
- time += 1;
678
- times[key] = time;
679
- setTimeout(() => {
680
- locks[key] = false;
681
- }, 100);
682
- bar.update(0, {
683
- input: `${input}(x${time.toString().padStart(2, "0")})`.padEnd(inputMaxLen + 5),
684
- status: "watching".padEnd(10),
685
- message: "watching..."
759
+ locks[key] = true;
760
+ break;
761
+ }
762
+ case "modulesAnalyze": {
763
+ const {
764
+ data: {
765
+ type: type2,
766
+ path: path2,
767
+ modules: pkgModules
768
+ }
769
+ } = e;
770
+ pkgModules.forEach((m) => {
771
+ const newM = {
772
+ ...m,
773
+ filename: `${manifest.name}/${m.filename}`,
774
+ label: `${manifest.name}/${m.label}`
775
+ };
776
+ const pushOrReplace = (arr) => {
777
+ const index = arr.findIndex(({ filename }) => filename === newM.filename);
778
+ if (index === -1) {
779
+ arr.push(newM);
780
+ } else {
781
+ arr[index] = newM;
782
+ }
783
+ };
784
+ pushOrReplace(modules);
785
+ if (type2 === "esm") {
786
+ pushOrReplace(esmModules);
787
+ }
788
+ if (type2 === "cjs") {
789
+ pushOrReplace(cjsModules);
790
+ }
686
791
  });
792
+ void refreshAnalyzer();
793
+ void refreshAnalyzer(
794
+ `/${type2}`,
795
+ {
796
+ cjs: cjsModules,
797
+ esm: esmModules
798
+ }[type2]
799
+ );
800
+ void refreshAnalyzer(`/${type2}/${manifest.name}/${path2.slice(2)}`, pkgModules);
801
+ break;
802
+ }
803
+ case "debug": {
804
+ console.log(...Array.isArray(e.data) ? e.data : [e.data]);
805
+ break;
687
806
  }
688
- locks[key] = true;
689
807
  }
690
808
  });
691
809
  await new Promise((resolve, reject) => {
@@ -716,6 +834,17 @@ ${errorStr}`)));
716
834
  }
717
835
  } finally {
718
836
  multiBars.stop();
837
+ let message = "The build is complete";
838
+ if (analyzer) {
839
+ message += ` and the analyzer is running at http://localhost:${options.port}/ana in ${analyzer.mode} mode.
840
+ `;
841
+ message += analyzer.open ? " The browser will open automatically.\n" : "";
842
+ if (anaPaths.size > 0) {
843
+ message += `The analyzer has ${anaPaths.size} pages:
844
+ ${Array.from(anaPaths).map((p) => `http://localhost:${options.port}${p}`).join("\n")}`;
845
+ }
846
+ }
847
+ !silent && console.log(message);
719
848
  }
720
849
  });
721
850