@vizejs/nuxt 0.155.0 → 0.157.0

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.
Files changed (2) hide show
  1. package/dist/index.mjs +167 -3
  2. package/package.json +5 -5
package/dist/index.mjs CHANGED
@@ -304,6 +304,12 @@ function buildNuxtCompilerOptions(rootDir, baseURL = "/", buildAssetsDir = "/_nu
304
304
  function isVizeVirtualVueModuleId(id) {
305
305
  return id.startsWith("\0") && /\.vue\.ts(?:\?|$)/.test(id);
306
306
  }
307
+ function isVizeGeneratedVueModuleId(id) {
308
+ let normalized = id;
309
+ if (normalized.startsWith("/@id/__x00__")) normalized = normalized.slice(12);
310
+ else if (normalized.startsWith("__x00__")) normalized = normalized.slice(7);
311
+ return /\.vue\.ts(?:\?|$)/.test(normalized);
312
+ }
307
313
  function normalizeVizeVirtualVueModuleId(id) {
308
314
  return (id.startsWith("\0vize-ssr:") ? id.slice(10) : id.slice(1)).replace(/\.ts(?=\?|$)/, "");
309
315
  }
@@ -320,6 +326,80 @@ function normalizeNuxtInjectedKeysForVizeVirtualModule(code, id) {
320
326
  return `'$${buildStableNuxtKey(normalizedId, index)}' ${NUXT_INJECTED_MARKER}`;
321
327
  });
322
328
  }
329
+ const NAMED_IMPORT_RE = /^import\s*\{([\s\S]*?)\}\s*from\s*(['"])(vue|#imports|#entry)\2\s*;?/gm;
330
+ function parseNamedImportSpecifiers(specifierSource) {
331
+ return specifierSource.split(",").map((specifier) => specifier.trim()).filter(Boolean).flatMap((specifier) => {
332
+ const withoutType = specifier.replace(/^type\s+/, "").trim();
333
+ if (!withoutType) return [];
334
+ const match = withoutType.match(/^([A-Za-z_$][\w$]*)(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);
335
+ if (!match) return [];
336
+ const imported = match[1];
337
+ return [{
338
+ imported,
339
+ local: match[2] ?? imported,
340
+ raw: withoutType
341
+ }];
342
+ });
343
+ }
344
+ function collectNamedImports(code) {
345
+ const imports = [];
346
+ for (const match of code.matchAll(NAMED_IMPORT_RE)) imports.push({
347
+ start: match.index ?? 0,
348
+ end: (match.index ?? 0) + match[0].length,
349
+ quote: match[2],
350
+ source: match[3],
351
+ specifiers: parseNamedImportSpecifiers(match[1] ?? "")
352
+ });
353
+ return imports;
354
+ }
355
+ function renderNamedImport(specifiers, source, quote) {
356
+ return `import { ${specifiers.map((specifier) => specifier.raw).join(", ")} } from ${quote}${source}${quote};`;
357
+ }
358
+ function preserveExplicitVueImportsFromNuxtAutoImports(originalCode, injectedCode) {
359
+ const originalVueSpecifiers = /* @__PURE__ */ new Map();
360
+ for (const statement of collectNamedImports(originalCode)) {
361
+ if (statement.source !== "vue") continue;
362
+ for (const specifier of statement.specifiers) originalVueSpecifiers.set(specifier.local, specifier);
363
+ }
364
+ if (originalVueSpecifiers.size === 0) return injectedCode;
365
+ const restoredSpecifiers = /* @__PURE__ */ new Map();
366
+ const replacements = [];
367
+ for (const statement of collectNamedImports(injectedCode)) {
368
+ if (statement.source !== "#imports" && statement.source !== "#entry") continue;
369
+ const keep = [];
370
+ let changed = false;
371
+ for (const specifier of statement.specifiers) {
372
+ const original = originalVueSpecifiers.get(specifier.local);
373
+ if (original) {
374
+ restoredSpecifiers.set(specifier.local, original);
375
+ changed = true;
376
+ } else keep.push(specifier);
377
+ }
378
+ if (!changed) continue;
379
+ replacements.push({
380
+ start: statement.start,
381
+ end: statement.end,
382
+ text: keep.length > 0 ? renderNamedImport(keep, statement.source, statement.quote) : ""
383
+ });
384
+ }
385
+ if (replacements.length === 0) return injectedCode;
386
+ let output = injectedCode;
387
+ for (const replacement of replacements.reverse()) output = output.slice(0, replacement.start) + replacement.text + output.slice(replacement.end);
388
+ const currentVueLocals = /* @__PURE__ */ new Set();
389
+ for (const statement of collectNamedImports(output)) {
390
+ if (statement.source !== "vue") continue;
391
+ for (const specifier of statement.specifiers) currentVueLocals.add(specifier.local);
392
+ }
393
+ const missing = [...restoredSpecifiers.values()].filter((specifier) => !currentVueLocals.has(specifier.local));
394
+ if (missing.length > 0) output = `${renderNamedImport(missing, "vue", "\"")}\n${output}`;
395
+ return output.replace(/\n{3,}/g, "\n\n");
396
+ }
397
+ function preserveExplicitVueImportsFromVizeModuleSource(id, code) {
398
+ if (!isVizeVirtualVueModuleId(id) && !isVizeGeneratedVueModuleId(id)) return code;
399
+ const sourcePath = (isVizeVirtualVueModuleId(id) ? normalizeVizeVirtualVueModuleId(id) : id.replace(/^\/@id\/__x00__/, "").replace(/^__x00__/, "").replace(/\.ts(?=\?|$)/, "")).replace(/\?.*$/, "");
400
+ if (!sourcePath.endsWith(".vue") || !fs.existsSync(sourcePath)) return code;
401
+ return preserveExplicitVueImportsFromNuxtAutoImports(fs.readFileSync(sourcePath, "utf-8"), code);
402
+ }
323
403
  //#endregion
324
404
  //#region src/options.ts
325
405
  const DEFAULT_NUXT_BRIDGE_OPTIONS = {
@@ -407,6 +487,15 @@ function appendOriginalVueSourceForUnoCss(code, normalizedId, options = {}) {
407
487
  * - Linter: `vize lint` CLI command (via `vize` bin)
408
488
  * - Type Checker: `vize check` CLI command (via `vize` bin)
409
489
  */
490
+ const VIZE_NUXT_AUTO_IMPORT_PATCHED = "__vizeNuxtAutoImportPatched";
491
+ const VUE_RUNTIME_DEDUPE = [
492
+ "vue",
493
+ "@vue/reactivity",
494
+ "@vue/runtime-core",
495
+ "@vue/runtime-dom",
496
+ "@vue/shared"
497
+ ];
498
+ const VUE_CLIENT_RUNTIME_IMPORT = "vue/dist/vue.runtime.esm-bundler.js";
410
499
  function getDetectedNuxtMajor(nuxt) {
411
500
  try {
412
501
  const version = getNuxtVersion(nuxt);
@@ -435,6 +524,23 @@ function getNuxtBuildAssetsDir(nuxt) {
435
524
  function shouldUseVizeCompiler(compilerOptions) {
436
525
  return compilerOptions !== false && (compilerOptions.vueVersion ?? 3) === 3;
437
526
  }
527
+ function dedupeVueRuntimePackages(vite) {
528
+ vite.resolve ||= {};
529
+ const dedupe = new Set(vite.resolve.dedupe ?? []);
530
+ for (const packageName of VUE_RUNTIME_DEDUPE) dedupe.add(packageName);
531
+ vite.resolve.dedupe = [...dedupe];
532
+ }
533
+ function isViteSsrTransform(args) {
534
+ const options = args[0];
535
+ return typeof options === "object" && options !== null && "ssr" in options && options.ssr === true;
536
+ }
537
+ function rewriteBareVueImportsToClientRuntime(code) {
538
+ return code.replace(/(\bfrom\s*)(["'])vue\2/g, (_, prefix, quote) => {
539
+ return `${prefix}${quote}${VUE_CLIENT_RUNTIME_IMPORT}${quote}`;
540
+ }).replace(/(\bimport\s*)(["'])vue\2/g, (_, prefix, quote) => {
541
+ return `${prefix}${quote}${VUE_CLIENT_RUNTIME_IMPORT}${quote}`;
542
+ });
543
+ }
438
544
  function normalizeNuxtKeyedTransformResult(id, result) {
439
545
  if (!isVizeVirtualVueModuleId(id) || result == null) return result;
440
546
  if (typeof result === "string") return normalizeNuxtInjectedKeysForVizeVirtualModule(result, id);
@@ -460,6 +566,39 @@ function patchNuxtKeyedFunctionsPlugin(plugin) {
460
566
  return normalizeNuxtKeyedTransformResult(id, await original.call(this, code, id, ...args));
461
567
  };
462
568
  }
569
+ function normalizeNuxtAutoImportTransformResult(code, id, result, rewriteVueRuntimeImports) {
570
+ if (!isVizeGeneratedVueModuleId(id) || result == null) return result;
571
+ if (typeof result === "string") {
572
+ const restored = preserveExplicitVueImportsFromVizeModuleSource(id, preserveExplicitVueImportsFromNuxtAutoImports(code, result));
573
+ return rewriteVueRuntimeImports ? rewriteBareVueImportsToClientRuntime(restored) : restored;
574
+ }
575
+ if (typeof result.code !== "string") return result;
576
+ let normalized = preserveExplicitVueImportsFromVizeModuleSource(id, preserveExplicitVueImportsFromNuxtAutoImports(code, result.code));
577
+ if (rewriteVueRuntimeImports) normalized = rewriteBareVueImportsToClientRuntime(normalized);
578
+ return normalized === result.code ? result : {
579
+ ...result,
580
+ code: normalized
581
+ };
582
+ }
583
+ function patchNuxtAutoImportTransformPlugin(plugin, isBuild) {
584
+ if (!plugin) return;
585
+ if (plugin[VIZE_NUXT_AUTO_IMPORT_PATCHED]) return;
586
+ if (typeof plugin.transform === "function") {
587
+ const original = plugin.transform;
588
+ plugin.transform = async function(code, id, ...args) {
589
+ return normalizeNuxtAutoImportTransformResult(code, id, await original.call(this, code, id, ...args), isBuild && !isViteSsrTransform(args));
590
+ };
591
+ plugin[VIZE_NUXT_AUTO_IMPORT_PATCHED] = true;
592
+ return;
593
+ }
594
+ const transform = plugin.transform;
595
+ if (!transform || typeof transform.handler !== "function") return;
596
+ const original = transform.handler;
597
+ transform.handler = async function(code, id, ...args) {
598
+ return normalizeNuxtAutoImportTransformResult(code, id, await original.call(this, code, id, ...args), isBuild && !isViteSsrTransform(args));
599
+ };
600
+ plugin[VIZE_NUXT_AUTO_IMPORT_PATCHED] = true;
601
+ }
463
602
  var src_default = defineNuxtModule({
464
603
  meta: {
465
604
  name: "@vizejs/nuxt",
@@ -485,12 +624,20 @@ var src_default = defineNuxtModule({
485
624
  supportsViteCompiler,
486
625
  vueVersion
487
626
  });
627
+ const usesVizeCompiler = shouldUseVizeCompiler(compilerOptions);
488
628
  if (compilerOptions !== false) {
489
629
  nuxt.options.vite ||= {};
490
630
  nuxt.options.vite.plugins = nuxt.options.vite.plugins || [];
491
631
  nuxt.options.vite.plugins.push(vize(compilerOptions));
492
632
  }
493
- const usesVizeCompiler = shouldUseVizeCompiler(compilerOptions);
633
+ let isNuxtBuild = false;
634
+ let isViteBuild = false;
635
+ if (usesVizeCompiler) nuxt.hook("build:before", () => {
636
+ if (nuxt.options.dev !== false) return;
637
+ isNuxtBuild = true;
638
+ nuxt.options.vite ||= {};
639
+ dedupeVueRuntimePackages(nuxt.options.vite);
640
+ });
494
641
  if (usesVizeCompiler) {
495
642
  if (nuxt.options.dev && devOptions.stylesheetLinks) {
496
643
  const devAssetBase = compilerOptions.devUrlBase ?? buildNuxtDevAssetBase(appBaseURL, buildAssetsDir);
@@ -502,11 +649,13 @@ var src_default = defineNuxtModule({
502
649
  }
503
650
  }
504
651
  nuxt.hook("vite:configResolved", (config) => {
652
+ isViteBuild = config.command === "build" || isNuxtBuild || nuxt.options.dev === false;
505
653
  for (let i = config.plugins.length - 1; i >= 0; i--) {
506
654
  const p = config.plugins[i];
507
655
  const name = p && typeof p === "object" && "name" in p ? p.name : "";
508
656
  if (name === "vite:vue") config.plugins.splice(i, 1);
509
657
  else if (bridgeOptions.stableInjectedKeys && name === "nuxt:compiler:keyed-functions") patchNuxtKeyedFunctionsPlugin(p);
658
+ if (bridgeOptions.autoImports) patchNuxtAutoImportTransformPlugin(p, isViteBuild);
510
659
  }
511
660
  });
512
661
  }
@@ -525,7 +674,7 @@ var src_default = defineNuxtModule({
525
674
  if (usesVizeCompiler && (bridgeOptions.autoImports || bridgeOptions.components || bridgeOptions.i18n || bridgeOptions.stableInjectedKeys)) addVitePlugin({
526
675
  name: "vizejs:nuxt-transform-bridge",
527
676
  enforce: "post",
528
- async transform(code, id) {
677
+ async transform(code, id, ...args) {
529
678
  if (!isVizeVirtualVueModuleId(id)) return;
530
679
  let result = code;
531
680
  let changed = false;
@@ -546,12 +695,20 @@ var src_default = defineNuxtModule({
546
695
  }
547
696
  }
548
697
  if (unimportCtx) try {
698
+ const beforeUnimport = result;
549
699
  const injected = await unimportCtx.injectImports(result, id);
550
700
  if (injected.imports && injected.imports.length > 0) {
551
- result = injected.code;
701
+ result = preserveExplicitVueImportsFromNuxtAutoImports(beforeUnimport, injected.code);
552
702
  changed = true;
553
703
  }
554
704
  } catch {}
705
+ if (bridgeOptions.autoImports) {
706
+ const nextResult = preserveExplicitVueImportsFromVizeModuleSource(id, result);
707
+ if (nextResult !== result) {
708
+ result = nextResult;
709
+ changed = true;
710
+ }
711
+ }
555
712
  if (bridgeOptions.stableInjectedKeys) {
556
713
  const stableKeyResult = normalizeNuxtInjectedKeysForVizeVirtualModule(result, id);
557
714
  if (stableKeyResult !== result) {
@@ -559,6 +716,13 @@ var src_default = defineNuxtModule({
559
716
  changed = true;
560
717
  }
561
718
  }
719
+ if (isViteBuild && !isViteSsrTransform(args)) {
720
+ const clientRuntimeResult = rewriteBareVueImportsToClientRuntime(result);
721
+ if (clientRuntimeResult !== result) {
722
+ result = clientRuntimeResult;
723
+ changed = true;
724
+ }
725
+ }
562
726
  if (changed) return {
563
727
  code: result,
564
728
  map: null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizejs/nuxt",
3
- "version": "0.155.0",
3
+ "version": "0.157.0",
4
4
  "description": "Nuxt module for Vize - compiler, musea gallery, linter, and type checker",
5
5
  "keywords": [
6
6
  "compiler",
@@ -41,10 +41,10 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@nuxt/kit": "4.4.6",
44
- "@vizejs/musea-nuxt": "0.155.0",
45
- "@vizejs/vite-plugin": "0.155.0",
46
- "@vizejs/vite-plugin-musea": "0.155.0",
47
- "vize": "0.155.0"
44
+ "@vizejs/musea-nuxt": "0.157.0",
45
+ "@vizejs/vite-plugin": "0.157.0",
46
+ "@vizejs/vite-plugin-musea": "0.157.0",
47
+ "vize": "0.157.0"
48
48
  },
49
49
  "devDependencies": {
50
50
  "typescript": "6.0.3",