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