@zeus-js/bundler-plugin 0.1.0-beta.1 → 0.1.0-beta.3

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.cjs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * bundler-plugin v0.1.0-beta.1
2
+ * bundler-plugin v0.1.0-beta.3
3
3
  * (c) 2026 baicie
4
4
  * Released under the MIT License.
5
5
  **/
@@ -33,6 +33,8 @@ let node_module = require("node:module");
33
33
  let node_path = require("node:path");
34
34
  node_path = __toESM(node_path, 1);
35
35
  let vite = require("vite");
36
+ let node_fs = require("node:fs");
37
+ node_fs = __toESM(node_fs, 1);
36
38
  let _zeus_js_component_analyzer = require("@zeus-js/component-analyzer");
37
39
  let fast_glob = require("fast-glob");
38
40
  fast_glob = __toESM(fast_glob, 1);
@@ -41,12 +43,17 @@ picomatch = __toESM(picomatch, 1);
41
43
  let node_fs_promises = require("node:fs/promises");
42
44
  node_fs_promises = __toESM(node_fs_promises, 1);
43
45
  let _babel_core = require("@babel/core");
46
+ let _babel_preset_typescript = require("@babel/preset-typescript");
47
+ _babel_preset_typescript = __toESM(_babel_preset_typescript, 1);
44
48
  let _zeus_js_compiler = require("@zeus-js/compiler");
45
49
  _zeus_js_compiler = __toESM(_zeus_js_compiler, 1);
46
50
  //#region packages/web-c/bundler-plugin/src/filter.ts
47
51
  function cleanUrl(id) {
48
52
  return id.replace(/[?#].*$/, "");
49
53
  }
54
+ function isTypeScriptLike(id) {
55
+ return /\.[cm]?tsx?$/.test(cleanUrl(id));
56
+ }
50
57
  //#endregion
51
58
  //#region packages/web-c/bundler-plugin/src/componentTransformFilter.ts
52
59
  function normalizePath$1(value) {
@@ -75,12 +82,28 @@ const DEFAULT_COMPONENT_EXCLUDE = [
75
82
  "node_modules/**",
76
83
  "dist/**"
77
84
  ];
85
+ const DEFAULT_TRANSFORM_INCLUDE = DEFAULT_COMPONENT_INCLUDE;
86
+ const DEFAULT_TRANSFORM_EXCLUDE = [
87
+ "**/*.test.*",
88
+ "**/*.spec.*",
89
+ "**/__tests__/**",
90
+ "**/*.d.ts",
91
+ "node_modules/**",
92
+ "dist/**"
93
+ ];
78
94
  function resolveComponentInclude(include) {
79
95
  return (include === null || include === void 0 ? void 0 : include.length) ? include : DEFAULT_COMPONENT_INCLUDE;
80
96
  }
81
97
  function resolveComponentExclude(exclude) {
82
98
  return (exclude === null || exclude === void 0 ? void 0 : exclude.length) ? exclude : DEFAULT_COMPONENT_EXCLUDE;
83
99
  }
100
+ function resolveTransformInclude(include, componentInclude) {
101
+ if (include === null || include === void 0 ? void 0 : include.length) return include;
102
+ return Array.from(new Set([...DEFAULT_TRANSFORM_INCLUDE, ...componentInclude]));
103
+ }
104
+ function resolveTransformExclude(exclude) {
105
+ return (exclude === null || exclude === void 0 ? void 0 : exclude.length) ? exclude : DEFAULT_TRANSFORM_EXCLUDE;
106
+ }
84
107
  //#endregion
85
108
  //#region packages/web-c/bundler-plugin/src/diagnostics.ts
86
109
  function formatDiagnostic(diagnostic) {
@@ -166,6 +189,42 @@ async function fileExists(file) {
166
189
  }
167
190
  }
168
191
  //#endregion
192
+ //#region packages/web-c/bundler-plugin/src/external.ts
193
+ const ZEUS_LIBRARY_EXTERNALS = [/^@zeus-js\//];
194
+ function collectPluginExternals(options, collectOptions = {}) {
195
+ var _options$plugins;
196
+ const entries = [];
197
+ if (collectOptions.includeZeusLibraryExternals) entries.push(...ZEUS_LIBRARY_EXTERNALS);
198
+ for (const plugin of (_options$plugins = options.plugins) !== null && _options$plugins !== void 0 ? _options$plugins : []) {
199
+ var _plugin$external;
200
+ for (const dep of (_plugin$external = plugin.external) !== null && _plugin$external !== void 0 ? _plugin$external : []) entries.push(dep);
201
+ }
202
+ return uniqueExternalEntries(entries);
203
+ }
204
+ function mergeExternal(userExternal, pluginExternal) {
205
+ if (!userExternal) return pluginExternal;
206
+ if (typeof userExternal === "function") return (source, importer, isResolved) => {
207
+ return matchesExternal(source, pluginExternal) || userExternal(source, importer, isResolved);
208
+ };
209
+ return uniqueExternalEntries([...Array.isArray(userExternal) ? userExternal : [userExternal], ...pluginExternal]);
210
+ }
211
+ function matchesExternal(source, entries) {
212
+ return entries.some((entry) => {
213
+ return typeof entry === "string" ? entry === source : entry.test(source);
214
+ });
215
+ }
216
+ function uniqueExternalEntries(entries) {
217
+ const seen = /* @__PURE__ */ new Set();
218
+ const result = [];
219
+ for (const entry of entries) {
220
+ const key = typeof entry === "string" ? `s:${entry}` : `r:${entry.source}/${entry.flags}`;
221
+ if (seen.has(key)) continue;
222
+ seen.add(key);
223
+ result.push(entry);
224
+ }
225
+ return result;
226
+ }
227
+ //#endregion
169
228
  //#region packages/web-c/bundler-plugin/src/outputRegistry.ts
170
229
  function createOutputRegistry() {
171
230
  const map = /* @__PURE__ */ new Map();
@@ -220,18 +279,29 @@ function normalizeTagName(tag, stripPrefix) {
220
279
  //#endregion
221
280
  //#region packages/web-c/bundler-plugin/src/transform.ts
222
281
  async function transformZeus(options) {
223
- var _compiler$moduleName;
224
- const { id, code, compiler, sourcemap = true } = options;
282
+ var _compilerOptions$modu;
283
+ const { id, code, compiler, sourcemap = true, transpile = false } = options;
284
+ const isTs = isTypeScriptLike(id);
285
+ const isTsx = /\.[cm]?tsx$/.test(id.replace(/[?#].*$/, ""));
286
+ const shouldRunCompiler = compiler !== false;
287
+ const shouldStripTs = transpile && isTs;
288
+ if (!shouldRunCompiler && !shouldStripTs) return null;
289
+ const compilerOptions = compiler === false ? {} : compiler === void 0 ? {} : compiler;
225
290
  const result = await (0, _babel_core.transformAsync)(code, {
226
291
  filename: id,
227
292
  sourceMaps: sourcemap,
228
- plugins: [[_zeus_js_compiler.default, {
229
- moduleName: (_compiler$moduleName = compiler === null || compiler === void 0 ? void 0 : compiler.moduleName) !== null && _compiler$moduleName !== void 0 ? _compiler$moduleName : "@zeus-js/runtime-dom",
293
+ plugins: shouldRunCompiler ? [[_zeus_js_compiler.default, {
294
+ moduleName: (_compilerOptions$modu = compilerOptions.moduleName) !== null && _compilerOptions$modu !== void 0 ? _compilerOptions$modu : "@zeus-js/runtime-dom",
230
295
  generate: "dom",
231
296
  hydratable: false,
232
297
  delegateEvents: true,
233
- ...compiler
234
- }]],
298
+ ...compilerOptions
299
+ }]] : [],
300
+ presets: shouldStripTs ? [[_babel_preset_typescript.default, {
301
+ allExtensions: true,
302
+ isTSX: isTsx,
303
+ allowDeclareFields: true
304
+ }]] : [],
235
305
  parserOpts: {
236
306
  sourceType: "module",
237
307
  plugins: ["typescript", "jsx"]
@@ -330,32 +400,59 @@ function normalizePath(value) {
330
400
  return value.replace(/\\/g, "/").replace(/^\.\//, "");
331
401
  }
332
402
  //#endregion
333
- //#region packages/web-c/bundler-plugin/src/rollup.ts
334
- function createZeusPlugin(options = {}) {
335
- let shouldTransform = (_id) => false;
336
- const virtualModules = new VirtualModuleRegistry();
403
+ //#region packages/web-c/bundler-plugin/src/core.ts
404
+ function createZeusBundlerPlugin(options = {}, createOptions) {
405
+ const target = createOptions.target;
406
+ let shouldCompileZeus = (_id) => false;
337
407
  let ctx;
408
+ let root = process.cwd();
409
+ const cleanedOutputDirs = /* @__PURE__ */ new Set();
410
+ const virtualModules = new VirtualModuleRegistry();
338
411
  return {
339
- name: "zeus-bundler-plugin",
412
+ name: resolvePluginName(target),
413
+ options(inputOptions) {
414
+ if (target === "vite") return null;
415
+ const pluginExternals = collectPluginExternals(options, { includeZeusLibraryExternals: true });
416
+ if (!pluginExternals.length) return null;
417
+ return {
418
+ ...inputOptions,
419
+ external: mergeExternal(inputOptions.external, pluginExternals)
420
+ };
421
+ },
422
+ outputOptions(outputOptions) {
423
+ if (target === "vite") return null;
424
+ cleanOutputDir(outputOptions, {
425
+ root: resolveRoot(options.root),
426
+ enabled: options.clean !== false,
427
+ cleanedOutputDirs
428
+ });
429
+ if (outputOptions.chunkFileNames) return null;
430
+ return {
431
+ ...outputOptions,
432
+ chunkFileNames: "chunks/[name]-[hash].js"
433
+ };
434
+ },
340
435
  async buildStart() {
341
- var _options$components, _options$components2, _options$plugins;
436
+ var _options$components, _options$components2, _options$transform, _options$transform2, _options$plugins;
342
437
  virtualModules.clear();
343
- const root = resolveRoot(options.root);
344
- const include = resolveComponentInclude((_options$components = options.components) === null || _options$components === void 0 ? void 0 : _options$components.include);
345
- const exclude = resolveComponentExclude((_options$components2 = options.components) === null || _options$components2 === void 0 ? void 0 : _options$components2.exclude);
346
- shouldTransform = createComponentTransformFilter({
438
+ root = resolveRoot(options.root);
439
+ const componentInclude = resolveComponentInclude((_options$components = options.components) === null || _options$components === void 0 ? void 0 : _options$components.include);
440
+ const componentExclude = resolveComponentExclude((_options$components2 = options.components) === null || _options$components2 === void 0 ? void 0 : _options$components2.exclude);
441
+ const transformInclude = resolveTransformInclude((_options$transform = options.transform) === null || _options$transform === void 0 ? void 0 : _options$transform.include, componentInclude);
442
+ const transformExclude = resolveTransformExclude((_options$transform2 = options.transform) === null || _options$transform2 === void 0 ? void 0 : _options$transform2.exclude);
443
+ shouldCompileZeus = createComponentTransformFilter({
347
444
  root,
348
- include,
349
- exclude
445
+ include: transformInclude,
446
+ exclude: transformExclude
350
447
  });
351
448
  const dts = await resolveDts({
352
449
  root,
353
450
  mode: options.dts,
354
- include,
355
- exclude
451
+ include: componentInclude,
452
+ exclude: componentExclude
356
453
  });
357
- const manifestResult = await createManifest(root, include, exclude);
358
- for (const file of await collectWatchFiles(root, include, exclude)) this.addWatchFile(file);
454
+ const manifestResult = await createManifest(root, componentInclude, componentExclude);
455
+ for (const file of await collectWatchFiles(root, componentInclude, componentExclude)) this.addWatchFile(file);
359
456
  const diagnostics = manifestResult.diagnostics;
360
457
  for (const diagnostic of diagnostics) {
361
458
  const message = formatDiagnostic(diagnostic);
@@ -364,13 +461,12 @@ function createZeusPlugin(options = {}) {
364
461
  }
365
462
  if (hasErrorDiagnostics(diagnostics)) this.error("[zeus] component analyzer failed.");
366
463
  if (options.diagnostics === "verbose") this.warn(`[zeus] dts ${dts.enabled ? "enabled" : "disabled"}: ${dts.reason.join(", ") || "no signal"}`);
367
- const outputs = createOutputRegistry();
368
464
  ctx = {
369
465
  root,
370
466
  manifest: manifestResult.manifest,
371
467
  diagnostics,
372
468
  dts,
373
- outputs,
469
+ outputs: createOutputRegistry(),
374
470
  emitFile: this.emitFile.bind(this),
375
471
  warn: this.warn.bind(this),
376
472
  error: this.error.bind(this),
@@ -395,26 +491,39 @@ function createZeusPlugin(options = {}) {
395
491
  }
396
492
  },
397
493
  resolveId(id, importer) {
398
- const resolved = virtualModules.resolve(id, importer);
399
- if (resolved) return {
400
- id: resolved,
494
+ const resolvedVirtual = virtualModules.resolve(id, importer);
495
+ if (resolvedVirtual) return {
496
+ id: resolvedVirtual,
401
497
  moduleSideEffects: "no-treeshake"
402
498
  };
499
+ const cleanImporter = importer === null || importer === void 0 ? void 0 : importer.replace(/^\x00/, "");
500
+ if ((cleanImporter === null || cleanImporter === void 0 ? void 0 : cleanImporter.startsWith("zeus:")) && (id.startsWith("./") || id.startsWith("../"))) {
501
+ const virtualEntryId = resolveVirtualEntryImport(id, cleanImporter);
502
+ if (virtualEntryId && virtualModules.has(virtualEntryId)) return {
503
+ id: "\0" + virtualEntryId,
504
+ moduleSideEffects: "no-treeshake"
505
+ };
506
+ }
507
+ if (target === "rollup") {
508
+ const resolvedTs = resolveTsLikeImport(id, importer, { extensions: options.resolveExtensions });
509
+ if (resolvedTs) return resolvedTs;
510
+ }
403
511
  return null;
404
512
  },
405
513
  load(id) {
406
514
  return virtualModules.load(id);
407
515
  },
408
516
  async transform(code, id) {
409
- if (!shouldTransform(id)) return null;
410
- const result = await transformZeus({
517
+ const shouldRunZeus = shouldCompileZeus(id);
518
+ const shouldStripTs = resolveTranspile(options.transpile, target) && isTypeScriptLike(id);
519
+ if (!shouldRunZeus && !shouldStripTs) return null;
520
+ return await transformZeus({
411
521
  id,
412
522
  code,
413
- compiler: options.compiler,
414
- sourcemap: true
523
+ compiler: shouldRunZeus ? options.compiler : false,
524
+ sourcemap: true,
525
+ transpile: shouldStripTs
415
526
  });
416
- if (!result) return null;
417
- return result;
418
527
  },
419
528
  async generateBundle(_, bundle) {
420
529
  var _options$plugins2;
@@ -429,6 +538,44 @@ function createZeusPlugin(options = {}) {
429
538
  }
430
539
  };
431
540
  }
541
+ function resolvePluginName(target) {
542
+ if (target === "vite") return "vite-plugin-zeus";
543
+ if (target === "rolldown") return "rolldown-plugin-zeus";
544
+ return "rollup-plugin-zeus";
545
+ }
546
+ function resolveTranspile(value, target) {
547
+ if (typeof value === "boolean") return value;
548
+ return target === "rollup";
549
+ }
550
+ function resolveRoot(root) {
551
+ if (typeof root === "function") return node_path.default.resolve(root());
552
+ return node_path.default.resolve(root !== null && root !== void 0 ? root : process.cwd());
553
+ }
554
+ function cleanOutputDir(outputOptions, options) {
555
+ if (!options.enabled) return;
556
+ const outputDir = resolveOutputDir(outputOptions, options.root);
557
+ if (!outputDir || !isSafeCleanTarget(outputDir, options.root)) return;
558
+ if (options.cleanedOutputDirs.has(outputDir)) return;
559
+ options.cleanedOutputDirs.add(outputDir);
560
+ node_fs.default.rmSync(outputDir, {
561
+ recursive: true,
562
+ force: true
563
+ });
564
+ }
565
+ function resolveOutputDir(outputOptions, root) {
566
+ const dir = outputOptions.dir;
567
+ if (typeof dir === "string" && dir.length > 0) return node_path.default.resolve(root, dir);
568
+ const file = outputOptions.file;
569
+ if (typeof file === "string" && file.length > 0) return node_path.default.dirname(node_path.default.resolve(root, file));
570
+ return node_path.default.resolve(root, "dist");
571
+ }
572
+ function isSafeCleanTarget(outputDir, root) {
573
+ const resolvedRoot = node_path.default.resolve(root);
574
+ const resolvedOutputDir = node_path.default.resolve(outputDir);
575
+ if (resolvedOutputDir === resolvedRoot) return false;
576
+ const relative = node_path.default.relative(resolvedRoot, resolvedOutputDir);
577
+ return Boolean(relative) && !relative.startsWith("..") && !node_path.default.isAbsolute(relative);
578
+ }
432
579
  async function createManifest(root, include, exclude) {
433
580
  if (!include.length) return {
434
581
  manifest: {
@@ -451,9 +598,37 @@ async function collectWatchFiles(root, include, exclude) {
451
598
  ignore: exclude
452
599
  });
453
600
  }
454
- function resolveRoot(root) {
455
- if (typeof root === "function") return node_path.default.resolve(root());
456
- return node_path.default.resolve(root !== null && root !== void 0 ? root : process.cwd());
601
+ function resolveTsLikeImport(id, importer, options) {
602
+ var _options$extensions;
603
+ if (!importer) return null;
604
+ if (cleanUrl(id) !== id) return null;
605
+ const source = cleanUrl(id);
606
+ const from = cleanUrl(importer);
607
+ if (source.startsWith("\0") || from.startsWith("\0")) return null;
608
+ if (!source.startsWith(".") && !isAbsoluteImportPath(source)) return null;
609
+ if (node_path.default.extname(source)) return null;
610
+ if (options.extensions === false) return null;
611
+ const extensions = (_options$extensions = options.extensions) !== null && _options$extensions !== void 0 ? _options$extensions : [
612
+ ".ts",
613
+ ".tsx",
614
+ ".mts",
615
+ ".cts",
616
+ ".js",
617
+ ".jsx",
618
+ ".mjs",
619
+ ".cjs"
620
+ ];
621
+ const base = isAbsoluteImportPath(source) ? source : node_path.default.resolve(node_path.default.dirname(from), source);
622
+ const candidates = [
623
+ base,
624
+ ...extensions.map((ext) => `${base}${ext}`),
625
+ ...extensions.map((ext) => node_path.default.join(base, `index${ext}`))
626
+ ];
627
+ for (const file of candidates) if (node_fs.default.existsSync(file) && node_fs.default.statSync(file).isFile()) return file;
628
+ return null;
629
+ }
630
+ function isAbsoluteImportPath(id) {
631
+ return node_path.default.isAbsolute(id) || /^[a-zA-Z]:[\\/]/.test(id);
457
632
  }
458
633
  function emitOutputFile(pluginContext, file) {
459
634
  if (file.type === "asset") {
@@ -481,22 +656,30 @@ function emitVirtualEntries(modules, pluginContext) {
481
656
  });
482
657
  }
483
658
  }
659
+ function resolveVirtualEntryImport(id, importer) {
660
+ const tagName = id.replace(/\.js$/, "").replace(/^\.\//, "").replace(/\.entry$/, "");
661
+ const lastColon = importer.lastIndexOf(":");
662
+ if (lastColon <= 0) return null;
663
+ return `${importer.slice(0, lastColon + 1)}entry:${tagName}`;
664
+ }
484
665
  //#endregion
485
666
  //#region packages/web-c/bundler-plugin/src/vite.ts
486
667
  function createZeusVitePlugin(options = {}) {
487
- var _options$root;
668
+ var _options$root, _options$transpile;
488
669
  let resolvedConfig;
489
670
  return {
490
- ...createZeusPlugin({
671
+ ...createZeusBundlerPlugin({
491
672
  ...options,
492
673
  root: (_options$root = options.root) !== null && _options$root !== void 0 ? _options$root : (() => {
493
674
  var _resolvedConfig$root;
494
675
  return (_resolvedConfig$root = resolvedConfig === null || resolvedConfig === void 0 ? void 0 : resolvedConfig.root) !== null && _resolvedConfig$root !== void 0 ? _resolvedConfig$root : process.cwd();
495
- })
496
- }),
676
+ }),
677
+ transpile: (_options$transpile = options.transpile) !== null && _options$transpile !== void 0 ? _options$transpile : false
678
+ }, { target: "vite" }),
497
679
  name: "vite-plugin-zeus",
498
680
  enforce: "pre",
499
681
  async config(userConfig) {
682
+ var _userConfig$build;
500
683
  const runtimeDomEntry = resolveRuntimeDOMEntry(userConfig.root);
501
684
  const pluginExternals = collectPluginExternals(options);
502
685
  const pluginConfig = {
@@ -511,33 +694,14 @@ function createZeusVitePlugin(options = {}) {
511
694
  ]
512
695
  }
513
696
  };
514
- if (pluginExternals.length) {
515
- var _userConfig$build;
516
- return (0, vite.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) } } });
517
- }
518
- return pluginConfig;
697
+ if (!pluginExternals.length) return pluginConfig;
698
+ return (0, vite.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) } } });
519
699
  },
520
700
  configResolved(config) {
521
701
  resolvedConfig = config;
522
702
  }
523
703
  };
524
704
  }
525
- function collectPluginExternals(options) {
526
- var _options$plugins;
527
- const set = /* @__PURE__ */ new Set();
528
- for (const plugin of (_options$plugins = options.plugins) !== null && _options$plugins !== void 0 ? _options$plugins : []) {
529
- var _plugin$external;
530
- for (const dep of (_plugin$external = plugin.external) !== null && _plugin$external !== void 0 ? _plugin$external : []) set.add(dep);
531
- }
532
- return Array.from(set);
533
- }
534
- function mergeExternal(userExternal, pluginExternal) {
535
- if (!userExternal) return pluginExternal;
536
- if (typeof userExternal === "function") return (source, importer, isResolved) => {
537
- return pluginExternal.includes(source) || userExternal(source, importer, isResolved);
538
- };
539
- return [...Array.isArray(userExternal) ? userExternal : [userExternal], ...pluginExternal];
540
- }
541
705
  async function isRolldownVite() {
542
706
  try {
543
707
  const vite$1 = await import("vite");
@@ -562,4 +726,3 @@ function resolveRuntimeDOMEntry(root) {
562
726
  exports.createZeusVitePlugin = createZeusVitePlugin;
563
727
  exports.default = createZeusVitePlugin;
564
728
  exports.zeus = createZeusVitePlugin;
565
- exports.mergeExternal = mergeExternal;