@zeus-js/bundler-plugin 0.1.0-beta.0 → 0.1.0-beta.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/dist/vite.js CHANGED
@@ -1,21 +1,26 @@
1
1
  /**
2
- * bundler-plugin v0.1.0-beta.0
2
+ * bundler-plugin v0.1.0-beta.2
3
3
  * (c) 2026 baicie
4
4
  * Released under the MIT License.
5
5
  **/
6
6
  import { createRequire } from "node:module";
7
7
  import path from "node:path";
8
8
  import { mergeConfig } from "vite";
9
+ import fs from "node:fs";
9
10
  import { analyzeComponents } from "@zeus-js/component-analyzer";
10
11
  import fg from "fast-glob";
11
12
  import picomatch from "picomatch";
12
- import fs from "node:fs/promises";
13
+ import fs$1 from "node:fs/promises";
13
14
  import { transformAsync } from "@babel/core";
15
+ import presetTypeScript from "@babel/preset-typescript";
14
16
  import zeusCompiler from "@zeus-js/compiler";
15
17
  //#region packages/web-c/bundler-plugin/src/filter.ts
16
18
  function cleanUrl(id) {
17
19
  return id.replace(/[?#].*$/, "");
18
20
  }
21
+ function isTypeScriptLike(id) {
22
+ return /\.[cm]?tsx?$/.test(cleanUrl(id));
23
+ }
19
24
  //#endregion
20
25
  //#region packages/web-c/bundler-plugin/src/componentTransformFilter.ts
21
26
  function normalizePath$1(value) {
@@ -44,12 +49,28 @@ const DEFAULT_COMPONENT_EXCLUDE = [
44
49
  "node_modules/**",
45
50
  "dist/**"
46
51
  ];
52
+ const DEFAULT_TRANSFORM_INCLUDE = DEFAULT_COMPONENT_INCLUDE;
53
+ const DEFAULT_TRANSFORM_EXCLUDE = [
54
+ "**/*.test.*",
55
+ "**/*.spec.*",
56
+ "**/__tests__/**",
57
+ "**/*.d.ts",
58
+ "node_modules/**",
59
+ "dist/**"
60
+ ];
47
61
  function resolveComponentInclude(include) {
48
62
  return (include === null || include === void 0 ? void 0 : include.length) ? include : DEFAULT_COMPONENT_INCLUDE;
49
63
  }
50
64
  function resolveComponentExclude(exclude) {
51
65
  return (exclude === null || exclude === void 0 ? void 0 : exclude.length) ? exclude : DEFAULT_COMPONENT_EXCLUDE;
52
66
  }
67
+ function resolveTransformInclude(include, componentInclude) {
68
+ if (include === null || include === void 0 ? void 0 : include.length) return include;
69
+ return Array.from(new Set([...DEFAULT_TRANSFORM_INCLUDE, ...componentInclude]));
70
+ }
71
+ function resolveTransformExclude(exclude) {
72
+ return (exclude === null || exclude === void 0 ? void 0 : exclude.length) ? exclude : DEFAULT_TRANSFORM_EXCLUDE;
73
+ }
53
74
  //#endregion
54
75
  //#region packages/web-c/bundler-plugin/src/diagnostics.ts
55
76
  function formatDiagnostic(diagnostic) {
@@ -231,7 +252,7 @@ function readPackageJson(_x5) {
231
252
  function _readPackageJson() {
232
253
  _readPackageJson = _asyncToGenerator(function* (root) {
233
254
  try {
234
- return JSON.parse(yield fs.readFile(path.join(root, "package.json"), "utf-8"));
255
+ return JSON.parse(yield fs$1.readFile(path.join(root, "package.json"), "utf-8"));
235
256
  } catch (_unused) {
236
257
  return null;
237
258
  }
@@ -244,7 +265,7 @@ function fileExists(_x6) {
244
265
  function _fileExists() {
245
266
  _fileExists = _asyncToGenerator(function* (file) {
246
267
  try {
247
- yield fs.access(file);
268
+ yield fs$1.access(file);
248
269
  return true;
249
270
  } catch (_unused2) {
250
271
  return false;
@@ -311,17 +332,28 @@ function transformZeus(_x) {
311
332
  }
312
333
  function _transformZeus() {
313
334
  _transformZeus = _asyncToGenerator(function* (options) {
314
- var _compiler$moduleName;
315
- const { id, code, compiler, sourcemap = true } = options;
335
+ var _compilerOptions$modu;
336
+ const { id, code, compiler, sourcemap = true, transpile = false } = options;
337
+ const isTs = isTypeScriptLike(id);
338
+ const isTsx = /\.[cm]?tsx$/.test(id.replace(/[?#].*$/, ""));
339
+ const shouldRunCompiler = compiler !== false;
340
+ const shouldStripTs = transpile && isTs;
341
+ if (!shouldRunCompiler && !shouldStripTs) return null;
342
+ const compilerOptions = compiler === false ? {} : compiler === void 0 ? {} : compiler;
316
343
  const result = yield transformAsync(code, {
317
344
  filename: id,
318
345
  sourceMaps: sourcemap,
319
- plugins: [[zeusCompiler, _objectSpread2({
320
- moduleName: (_compiler$moduleName = compiler === null || compiler === void 0 ? void 0 : compiler.moduleName) !== null && _compiler$moduleName !== void 0 ? _compiler$moduleName : "@zeus-js/runtime-dom",
346
+ plugins: shouldRunCompiler ? [[zeusCompiler, _objectSpread2({
347
+ moduleName: (_compilerOptions$modu = compilerOptions.moduleName) !== null && _compilerOptions$modu !== void 0 ? _compilerOptions$modu : "@zeus-js/runtime-dom",
321
348
  generate: "dom",
322
349
  hydratable: false,
323
350
  delegateEvents: true
324
- }, compiler)]],
351
+ }, compilerOptions)]] : [],
352
+ presets: shouldStripTs ? [[presetTypeScript, {
353
+ allExtensions: true,
354
+ isTSX: isTsx,
355
+ allowDeclareFields: true
356
+ }]] : [],
325
357
  parserOpts: {
326
358
  sourceType: "module",
327
359
  plugins: ["typescript", "jsx"]
@@ -422,34 +454,38 @@ function normalizePath(value) {
422
454
  return value.replace(/\\/g, "/").replace(/^\.\//, "");
423
455
  }
424
456
  //#endregion
425
- //#region packages/web-c/bundler-plugin/src/rollup.ts
426
- function createZeusPlugin(options = {}) {
427
- let shouldTransform = (_id) => false;
428
- const virtualModules = new VirtualModuleRegistry();
457
+ //#region packages/web-c/bundler-plugin/src/core.ts
458
+ function createZeusBundlerPlugin(options = {}, createOptions) {
459
+ const target = createOptions.target;
460
+ let shouldCompileZeus = (_id) => false;
429
461
  let ctx;
462
+ let root = process.cwd();
463
+ const virtualModules = new VirtualModuleRegistry();
430
464
  return {
431
- name: "zeus-bundler-plugin",
465
+ name: resolvePluginName(target),
432
466
  buildStart() {
433
467
  var _this = this;
434
468
  return _asyncToGenerator(function* () {
435
- var _options$components, _options$components2, _options$plugins;
469
+ var _options$components, _options$components2, _options$transform, _options$transform2, _options$plugins;
436
470
  virtualModules.clear();
437
- const root = resolveRoot(options.root);
438
- const include = resolveComponentInclude((_options$components = options.components) === null || _options$components === void 0 ? void 0 : _options$components.include);
439
- const exclude = resolveComponentExclude((_options$components2 = options.components) === null || _options$components2 === void 0 ? void 0 : _options$components2.exclude);
440
- shouldTransform = createComponentTransformFilter({
471
+ root = resolveRoot(options.root);
472
+ const componentInclude = resolveComponentInclude((_options$components = options.components) === null || _options$components === void 0 ? void 0 : _options$components.include);
473
+ const componentExclude = resolveComponentExclude((_options$components2 = options.components) === null || _options$components2 === void 0 ? void 0 : _options$components2.exclude);
474
+ const transformInclude = resolveTransformInclude((_options$transform = options.transform) === null || _options$transform === void 0 ? void 0 : _options$transform.include, componentInclude);
475
+ const transformExclude = resolveTransformExclude((_options$transform2 = options.transform) === null || _options$transform2 === void 0 ? void 0 : _options$transform2.exclude);
476
+ shouldCompileZeus = createComponentTransformFilter({
441
477
  root,
442
- include,
443
- exclude
478
+ include: transformInclude,
479
+ exclude: transformExclude
444
480
  });
445
481
  const dts = yield resolveDts({
446
482
  root,
447
483
  mode: options.dts,
448
- include,
449
- exclude
484
+ include: componentInclude,
485
+ exclude: componentExclude
450
486
  });
451
- const manifestResult = yield createManifest(root, include, exclude);
452
- for (const file of yield collectWatchFiles(root, include, exclude)) _this.addWatchFile(file);
487
+ const manifestResult = yield createManifest(root, componentInclude, componentExclude);
488
+ for (const file of yield collectWatchFiles(root, componentInclude, componentExclude)) _this.addWatchFile(file);
453
489
  const diagnostics = manifestResult.diagnostics;
454
490
  for (const diagnostic of diagnostics) {
455
491
  const message = formatDiagnostic(diagnostic);
@@ -458,13 +494,12 @@ function createZeusPlugin(options = {}) {
458
494
  }
459
495
  if (hasErrorDiagnostics(diagnostics)) _this.error("[zeus] component analyzer failed.");
460
496
  if (options.diagnostics === "verbose") _this.warn(`[zeus] dts ${dts.enabled ? "enabled" : "disabled"}: ${dts.reason.join(", ") || "no signal"}`);
461
- const outputs = createOutputRegistry();
462
497
  ctx = {
463
498
  root,
464
499
  manifest: manifestResult.manifest,
465
500
  diagnostics,
466
501
  dts,
467
- outputs,
502
+ outputs: createOutputRegistry(),
468
503
  emitFile: _this.emitFile.bind(_this),
469
504
  warn: _this.warn.bind(_this),
470
505
  error: _this.error.bind(_this),
@@ -490,11 +525,15 @@ function createZeusPlugin(options = {}) {
490
525
  })();
491
526
  },
492
527
  resolveId(id, importer) {
493
- const resolved = virtualModules.resolve(id, importer);
494
- if (resolved) return {
495
- id: resolved,
528
+ const resolvedVirtual = virtualModules.resolve(id, importer);
529
+ if (resolvedVirtual) return {
530
+ id: resolvedVirtual,
496
531
  moduleSideEffects: "no-treeshake"
497
532
  };
533
+ if (target === "rollup") {
534
+ const resolvedTs = resolveTsLikeImport(id, importer, { extensions: options.resolveExtensions });
535
+ if (resolvedTs) return resolvedTs;
536
+ }
498
537
  return null;
499
538
  },
500
539
  load(id) {
@@ -502,15 +541,16 @@ function createZeusPlugin(options = {}) {
502
541
  },
503
542
  transform(code, id) {
504
543
  return _asyncToGenerator(function* () {
505
- if (!shouldTransform(id)) return null;
506
- const result = yield transformZeus({
544
+ const shouldRunZeus = shouldCompileZeus(id);
545
+ const shouldStripTs = resolveTranspile(options.transpile, target) && isTypeScriptLike(id);
546
+ if (!shouldRunZeus && !shouldStripTs) return null;
547
+ return yield transformZeus({
507
548
  id,
508
549
  code,
509
- compiler: options.compiler,
510
- sourcemap: true
550
+ compiler: shouldRunZeus ? options.compiler : false,
551
+ sourcemap: true,
552
+ transpile: shouldStripTs
511
553
  });
512
- if (!result) return null;
513
- return result;
514
554
  })();
515
555
  },
516
556
  generateBundle(_, bundle) {
@@ -529,6 +569,19 @@ function createZeusPlugin(options = {}) {
529
569
  }
530
570
  };
531
571
  }
572
+ function resolvePluginName(target) {
573
+ if (target === "vite") return "vite-plugin-zeus";
574
+ if (target === "rolldown") return "rolldown-plugin-zeus";
575
+ return "rollup-plugin-zeus";
576
+ }
577
+ function resolveTranspile(value, target) {
578
+ if (typeof value === "boolean") return value;
579
+ return target === "rollup";
580
+ }
581
+ function resolveRoot(root) {
582
+ if (typeof root === "function") return path.resolve(root());
583
+ return path.resolve(root !== null && root !== void 0 ? root : process.cwd());
584
+ }
532
585
  function createManifest(_x, _x2, _x3) {
533
586
  return _createManifest.apply(this, arguments);
534
587
  }
@@ -563,9 +616,37 @@ function _collectWatchFiles() {
563
616
  });
564
617
  return _collectWatchFiles.apply(this, arguments);
565
618
  }
566
- function resolveRoot(root) {
567
- if (typeof root === "function") return path.resolve(root());
568
- return path.resolve(root !== null && root !== void 0 ? root : process.cwd());
619
+ function resolveTsLikeImport(id, importer, options) {
620
+ var _options$extensions;
621
+ if (!importer) return null;
622
+ if (cleanUrl(id) !== id) return null;
623
+ const source = cleanUrl(id);
624
+ const from = cleanUrl(importer);
625
+ if (source.startsWith("\0") || from.startsWith("\0")) return null;
626
+ if (!source.startsWith(".") && !isAbsoluteImportPath(source)) return null;
627
+ if (path.extname(source)) return null;
628
+ if (options.extensions === false) return null;
629
+ const extensions = (_options$extensions = options.extensions) !== null && _options$extensions !== void 0 ? _options$extensions : [
630
+ ".ts",
631
+ ".tsx",
632
+ ".mts",
633
+ ".cts",
634
+ ".js",
635
+ ".jsx",
636
+ ".mjs",
637
+ ".cjs"
638
+ ];
639
+ const base = isAbsoluteImportPath(source) ? source : path.resolve(path.dirname(from), source);
640
+ const candidates = [
641
+ base,
642
+ ...extensions.map((ext) => `${base}${ext}`),
643
+ ...extensions.map((ext) => path.join(base, `index${ext}`))
644
+ ];
645
+ for (const file of candidates) if (fs.existsSync(file) && fs.statSync(file).isFile()) return file;
646
+ return null;
647
+ }
648
+ function isAbsoluteImportPath(id) {
649
+ return path.isAbsolute(id) || /^[a-zA-Z]:[\\/]/.test(id);
569
650
  }
570
651
  function emitOutputFile(pluginContext, file) {
571
652
  if (file.type === "asset") {
@@ -594,18 +675,40 @@ function emitVirtualEntries(modules, pluginContext) {
594
675
  }
595
676
  }
596
677
  //#endregion
678
+ //#region packages/web-c/bundler-plugin/src/external.ts
679
+ function collectPluginExternals(options) {
680
+ var _options$plugins;
681
+ const set = /* @__PURE__ */ new Set();
682
+ for (const plugin of (_options$plugins = options.plugins) !== null && _options$plugins !== void 0 ? _options$plugins : []) {
683
+ var _plugin$external;
684
+ for (const dep of (_plugin$external = plugin.external) !== null && _plugin$external !== void 0 ? _plugin$external : []) set.add(dep);
685
+ }
686
+ return Array.from(set);
687
+ }
688
+ function mergeExternal(userExternal, pluginExternal) {
689
+ if (!userExternal) return pluginExternal;
690
+ if (typeof userExternal === "function") return (source, importer, isResolved) => {
691
+ return pluginExternal.includes(source) || userExternal(source, importer, isResolved);
692
+ };
693
+ return [...Array.isArray(userExternal) ? userExternal : [userExternal], ...pluginExternal];
694
+ }
695
+ //#endregion
597
696
  //#region packages/web-c/bundler-plugin/src/vite.ts
598
697
  function createZeusVitePlugin(options = {}) {
599
- var _options$root;
698
+ var _options$root, _options$transpile;
600
699
  let resolvedConfig;
601
- return _objectSpread2(_objectSpread2({}, createZeusPlugin(_objectSpread2(_objectSpread2({}, options), {}, { root: (_options$root = options.root) !== null && _options$root !== void 0 ? _options$root : (() => {
602
- var _resolvedConfig$root;
603
- return (_resolvedConfig$root = resolvedConfig === null || resolvedConfig === void 0 ? void 0 : resolvedConfig.root) !== null && _resolvedConfig$root !== void 0 ? _resolvedConfig$root : process.cwd();
604
- }) }))), {}, {
700
+ return _objectSpread2(_objectSpread2({}, createZeusBundlerPlugin(_objectSpread2(_objectSpread2({}, options), {}, {
701
+ root: (_options$root = options.root) !== null && _options$root !== void 0 ? _options$root : (() => {
702
+ var _resolvedConfig$root;
703
+ return (_resolvedConfig$root = resolvedConfig === null || resolvedConfig === void 0 ? void 0 : resolvedConfig.root) !== null && _resolvedConfig$root !== void 0 ? _resolvedConfig$root : process.cwd();
704
+ }),
705
+ transpile: (_options$transpile = options.transpile) !== null && _options$transpile !== void 0 ? _options$transpile : false
706
+ }), { target: "vite" })), {}, {
605
707
  name: "vite-plugin-zeus",
606
708
  enforce: "pre",
607
709
  config(userConfig) {
608
710
  return _asyncToGenerator(function* () {
711
+ var _userConfig$build;
609
712
  const runtimeDomEntry = resolveRuntimeDOMEntry(userConfig.root);
610
713
  const pluginExternals = collectPluginExternals(options);
611
714
  const pluginConfig = _objectSpread2(_objectSpread2({}, (yield isRolldownVite()) ? { oxc: { jsx: "preserve" } } : { esbuild: { jsx: "preserve" } }), {}, { resolve: {
@@ -617,11 +720,8 @@ function createZeusVitePlugin(options = {}) {
617
720
  "@zeus-js/component-dts"
618
721
  ]
619
722
  } });
620
- if (pluginExternals.length) {
621
- var _userConfig$build;
622
- return mergeConfig(pluginConfig, { build: { rollupOptions: { external: mergeExternal((_userConfig$build = userConfig.build) === null || _userConfig$build === void 0 || (_userConfig$build = _userConfig$build.rollupOptions) === null || _userConfig$build === void 0 ? void 0 : _userConfig$build.external, pluginExternals) } } });
623
- }
624
- return pluginConfig;
723
+ if (!pluginExternals.length) return pluginConfig;
724
+ return mergeConfig(pluginConfig, { build: { rollupOptions: { external: mergeExternal((_userConfig$build = userConfig.build) === null || _userConfig$build === void 0 || (_userConfig$build = _userConfig$build.rollupOptions) === null || _userConfig$build === void 0 ? void 0 : _userConfig$build.external, pluginExternals) } } });
625
725
  })();
626
726
  },
627
727
  configResolved(config) {
@@ -629,22 +729,6 @@ function createZeusVitePlugin(options = {}) {
629
729
  }
630
730
  });
631
731
  }
632
- function collectPluginExternals(options) {
633
- var _options$plugins;
634
- const set = /* @__PURE__ */ new Set();
635
- for (const plugin of (_options$plugins = options.plugins) !== null && _options$plugins !== void 0 ? _options$plugins : []) {
636
- var _plugin$external;
637
- for (const dep of (_plugin$external = plugin.external) !== null && _plugin$external !== void 0 ? _plugin$external : []) set.add(dep);
638
- }
639
- return Array.from(set);
640
- }
641
- function mergeExternal(userExternal, pluginExternal) {
642
- if (!userExternal) return pluginExternal;
643
- if (typeof userExternal === "function") return (source, importer, isResolved) => {
644
- return pluginExternal.includes(source) || userExternal(source, importer, isResolved);
645
- };
646
- return [...Array.isArray(userExternal) ? userExternal : [userExternal], ...pluginExternal];
647
- }
648
732
  function isRolldownVite() {
649
733
  return _isRolldownVite.apply(this, arguments);
650
734
  }
@@ -672,4 +756,4 @@ function resolveRuntimeDOMEntry(root) {
672
756
  }
673
757
  }
674
758
  //#endregion
675
- export { createZeusVitePlugin, createZeusVitePlugin as default, createZeusVitePlugin as zeus, mergeExternal };
759
+ export { createZeusVitePlugin, createZeusVitePlugin as default, createZeusVitePlugin as zeus };