@vizejs/vite-plugin 0.310.0 → 0.312.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.
package/README.md CHANGED
@@ -181,8 +181,9 @@ interface VizeNativeOptions {
181
181
  ssr?: boolean;
182
182
 
183
183
  /**
184
- * Enable source map generation
185
- * @default true in development, false in production
184
+ * Enable source map generation. The emitted map's `sources` names the
185
+ * authored `.vue` file, not the virtual `.vue.ts` module.
186
+ * @default true in development, false in production unless `build.sourcemap` is set
186
187
  */
187
188
  sourceMap?: boolean;
188
189
 
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as VizeVueVersion, c as ResolvedVizeConfig, i as VizeOptions, l as UserConfigExport, n as MacroArtifact, o as ConfigEnv, r as VizeCompatibilityOptions, s as LoadConfigOptions, t as CompiledModule, u as VizeConfig } from "./types-x-lq08Y8.mjs";
1
+ import { a as VizeVueVersion, c as ResolvedVizeConfig, i as VizeOptions, l as UserConfigExport, n as MacroArtifact, o as ConfigEnv, r as VizeCompatibilityOptions, s as LoadConfigOptions, t as CompiledModule, u as VizeConfig } from "./types-Cm3dJq25.mjs";
2
2
  import { Plugin } from "vite";
3
3
 
4
4
  //#region src/virtual.d.ts
package/dist/index.mjs CHANGED
@@ -230,6 +230,89 @@ function insertBeforeSfcMainDefaultExport(code, insertion, options = {}) {
230
230
  return `${code.slice(0, exportStart)}${insertion}\n${code.slice(exportStart)}`;
231
231
  }
232
232
  //#endregion
233
+ //#region src/utils/source-map.ts
234
+ function isSourceMapV3(value) {
235
+ if (value === null || typeof value !== "object") return false;
236
+ const map = value;
237
+ return map.version === 3 && Array.isArray(map.sources) && Array.isArray(map.names) && typeof map.mappings === "string";
238
+ }
239
+ /**
240
+ * Parse a compiler-produced map, or `null` when there is nothing usable.
241
+ *
242
+ * A malformed map is worse than no map — Vite would chain garbage into the
243
+ * bundle's map — so anything that is not a v3 document is dropped.
244
+ */
245
+ function parseSourceMap(json) {
246
+ if (!json) return null;
247
+ let parsed;
248
+ try {
249
+ parsed = JSON.parse(json);
250
+ } catch {
251
+ return null;
252
+ }
253
+ return isSourceMapV3(parsed) ? parsed : null;
254
+ }
255
+ function countNewlines(text) {
256
+ let total = 0;
257
+ for (let index = text.indexOf("\n"); index !== -1; index = text.indexOf("\n", index + 1)) total++;
258
+ return total;
259
+ }
260
+ /**
261
+ * Insert `count` unmapped generated lines at generated line `atLine`.
262
+ *
263
+ * Returns the map unchanged when the insertion lands past the last mapped line,
264
+ * because nothing after it needs moving.
265
+ */
266
+ function shiftMappedLines(map, atLine, count) {
267
+ if (count <= 0) return map;
268
+ const groups = map.mappings.split(";");
269
+ if (atLine >= groups.length) return map;
270
+ const shifted = [
271
+ ...groups.slice(0, atLine),
272
+ ...Array(count).fill(""),
273
+ ...groups.slice(atLine)
274
+ ];
275
+ return {
276
+ ...map,
277
+ mappings: shifted.join(";")
278
+ };
279
+ }
280
+ /**
281
+ * A module's code and the map that describes it, edited together.
282
+ *
283
+ * Every write goes through {@link edit}, so the map is corrected in the same
284
+ * step that changes the code and the two cannot drift.
285
+ */
286
+ var MappedModule = class {
287
+ code;
288
+ map;
289
+ constructor(code, map) {
290
+ this.code = code;
291
+ this.map = map;
292
+ }
293
+ /** Replace the module with `next`, realigning the map to the new line layout. */
294
+ edit(next) {
295
+ const previous = this.code;
296
+ this.code = next;
297
+ if (this.map === null || next === previous) return;
298
+ const limit = Math.min(previous.length, next.length);
299
+ let prefix = 0;
300
+ while (prefix < limit && previous.charCodeAt(prefix) === next.charCodeAt(prefix)) prefix++;
301
+ let suffix = 0;
302
+ while (suffix < limit - prefix && previous.charCodeAt(previous.length - 1 - suffix) === next.charCodeAt(next.length - 1 - suffix)) suffix++;
303
+ const removed = previous.slice(prefix, previous.length - suffix);
304
+ const addedLines = countNewlines(next.slice(prefix, next.length - suffix)) - countNewlines(removed);
305
+ if (addedLines === 0) return;
306
+ if (addedLines < 0) {
307
+ this.map = null;
308
+ return;
309
+ }
310
+ const editLine = countNewlines(previous.slice(0, prefix));
311
+ const startsAtLineStart = prefix === 0 || previous.charCodeAt(prefix - 1) === 10;
312
+ this.map = shiftMappedLines(this.map, startsAtLineStart ? editLine : editLine + 1, addedLines);
313
+ }
314
+ };
315
+ //#endregion
233
316
  //#region src/utils/css.ts
234
317
  function scopeCssForPipeline(css, scopeId) {
235
318
  return scopeViteCssForPipeline(css, scopeId);
@@ -433,29 +516,40 @@ function embedsInlineCss(compiled, options) {
433
516
  return !usesStyleImports(compiled, options) && !options.ssr && !!compiled.css && !(options.isProduction && !!options.extractCss);
434
517
  }
435
518
  function generateOutput(compiled, options) {
519
+ return generateOutputWithMap(compiled, options).code;
520
+ }
521
+ /**
522
+ * `generateOutput` plus the source map that describes the module it returns.
523
+ *
524
+ * The compiler's map (`compiled.map`) describes `compiled.code`; every rewrite
525
+ * below goes through {@link MappedModule}, which realigns the map to the lines
526
+ * it inserts, so the returned map describes the returned code (#3399). `map` is
527
+ * `null` when the compiler produced none.
528
+ */
529
+ function generateOutputWithMap(compiled, options) {
436
530
  const { isProduction, isDev, ssr, hmrUpdateType, extractCss, filePath } = options;
437
- let output = compiled.code;
438
- const moduleInfo = analyzeModuleOutput(output);
531
+ const emitted = new MappedModule(compiled.code, parseSourceMap(compiled.map));
532
+ const moduleInfo = compiled.moduleShape ?? analyzeModuleOutput(emitted.code);
439
533
  const hasExportDefault = moduleInfo.hasDefaultExport;
440
534
  const hasNamedRenderExport = moduleInfo.hasNamedRenderExport;
441
535
  const hasNamedSsrRenderExport = moduleInfo.hasNamedSsrRenderExport;
442
536
  const hasSfcMainDefined = moduleInfo.hasSfcMainDefined;
443
537
  if (hasExportDefault && !hasSfcMainDefined) {
444
- output = rewriteDefaultExportToSfcMain(output, moduleInfo);
445
- if (compiled.hasScoped && compiled.scopeId) output += `\n_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`;
446
- output += "\nexport default _sfc_main;";
538
+ emitted.edit(rewriteDefaultExportToSfcMain(emitted.code, moduleInfo));
539
+ if (compiled.hasScoped && compiled.scopeId) emitted.edit(`${emitted.code}\n_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`);
540
+ emitted.edit(`${emitted.code}\nexport default _sfc_main;`);
447
541
  } else if (hasExportDefault && hasSfcMainDefined) {
448
- if (compiled.hasScoped && compiled.scopeId) output = insertBeforeSfcMainDefaultExport(output, `_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`, { moduleInfo });
542
+ if (compiled.hasScoped && compiled.scopeId) emitted.edit(insertBeforeSfcMainDefaultExport(emitted.code, `_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`, { moduleInfo }));
449
543
  } else if (!hasExportDefault && !hasSfcMainDefined && hasNamedRenderExport) {
450
- output += "\nconst _sfc_main = {};";
451
- if (compiled.hasScoped && compiled.scopeId) output += `\n_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`;
452
- output += "\n_sfc_main.render = render;";
453
- output += "\nexport default _sfc_main;";
544
+ emitted.edit(`${emitted.code}\nconst _sfc_main = {};`);
545
+ if (compiled.hasScoped && compiled.scopeId) emitted.edit(`${emitted.code}\n_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`);
546
+ emitted.edit(`${emitted.code}\n_sfc_main.render = render;`);
547
+ emitted.edit(`${emitted.code}\nexport default _sfc_main;`);
454
548
  } else if (!hasExportDefault && !hasSfcMainDefined && hasNamedSsrRenderExport) {
455
- output += "\nconst _sfc_main = {};";
456
- if (compiled.hasScoped && compiled.scopeId) output += `\n_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`;
457
- output += "\n_sfc_main.ssrRender = ssrRender;";
458
- output += "\nexport default _sfc_main;";
549
+ emitted.edit(`${emitted.code}\nconst _sfc_main = {};`);
550
+ if (compiled.hasScoped && compiled.scopeId) emitted.edit(`${emitted.code}\n_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`);
551
+ emitted.edit(`${emitted.code}\n_sfc_main.ssrRender = ssrRender;`);
552
+ emitted.edit(`${emitted.code}\nexport default _sfc_main;`);
459
553
  }
460
554
  if (usesStyleImports(compiled, options)) {
461
555
  const styleImports = [];
@@ -479,7 +573,7 @@ function generateOutput(compiled, options) {
479
573
  }
480
574
  }
481
575
  const allImports = [...styleImports, ...cssModuleImports].join("\n");
482
- if (allImports) output = insertAfterStaticImports(output, allImports);
576
+ if (allImports) emitted.edit(insertAfterStaticImports(emitted.code, allImports));
483
577
  if (cssModuleImports.length > 0) {
484
578
  const moduleBindings = [];
485
579
  for (const block of compiled.styles) if (isCssModule(block)) {
@@ -490,14 +584,17 @@ function generateOutput(compiled, options) {
490
584
  });
491
585
  }
492
586
  const cssModuleSetup = moduleBindings.map((m) => `_sfc_main.__cssModules = _sfc_main.__cssModules || {};\n_sfc_main.__cssModules[${JSON.stringify(m.name)}] = ${m.bindingName};`).join("\n");
493
- output = insertBeforeSfcMainDefaultExport(output, cssModuleSetup, { normalizeSemicolon: true });
587
+ emitted.edit(insertBeforeSfcMainDefaultExport(emitted.code, cssModuleSetup, { normalizeSemicolon: true }));
494
588
  }
495
- } else if (!ssr && compiled.css && !(isProduction && extractCss)) output = prependInlineStyleInjection(output, compiled.css, compiled.scopeId);
589
+ } else if (!ssr && compiled.css && !(isProduction && extractCss)) emitted.edit(prependInlineStyleInjection(emitted.code, compiled.css, compiled.scopeId));
496
590
  if (!isProduction && isDev && hasExportDefault) {
497
- const effectiveHmrUpdateType = hmrUpdateType === "template-only" && !supportsTemplateOnlyHmr(output) ? "full-reload" : hmrUpdateType ?? "full-reload";
498
- output += generateHmrCode(compiled.scopeId, effectiveHmrUpdateType);
591
+ const effectiveHmrUpdateType = hmrUpdateType === "template-only" && !supportsTemplateOnlyHmr(emitted.code) ? "full-reload" : hmrUpdateType ?? "full-reload";
592
+ emitted.edit(`${emitted.code}${generateHmrCode(compiled.scopeId, effectiveHmrUpdateType)}`);
499
593
  }
500
- return output;
594
+ return {
595
+ code: emitted.code,
596
+ map: emitted.map
597
+ };
501
598
  }
502
599
  const RESOLVED_CSS_MODULE = "\0vize:all-styles.css";
503
600
  /** Create a virtual module ID from a real .vue file path */
@@ -651,7 +748,7 @@ function getEnvironmentCache(state, ssr) {
651
748
  }
652
749
  function getCompileOptionsForRequest(state, ssr) {
653
750
  const options = {
654
- sourceMap: state.mergedOptions?.sourceMap ?? !state.isProduction,
751
+ sourceMap: state.mergedOptions?.sourceMap ?? (!state.isProduction || !!state.viteBuildSourcemap),
655
752
  ssr,
656
753
  vapor: !ssr && (state.mergedOptions?.vapor ?? false),
657
754
  customRenderer: state.mergedOptions?.customRenderer ?? false,
@@ -795,6 +892,7 @@ function buildCompileBatchOptions(options) {
795
892
  includeStyles: true,
796
893
  includeMacroArtifacts: true,
797
894
  includeHashes: true,
895
+ includeSourceMap: options.sourceMap,
798
896
  ...options.mode === void 0 ? {} : { mode: options.mode },
799
897
  ...options.templateSyntax === void 0 ? {} : { templateSyntax: options.templateSyntax },
800
898
  ...options.runtimeModuleName === void 0 ? {} : { runtimeModuleName: options.runtimeModuleName },
@@ -901,6 +999,7 @@ function compileFile(filePath, cache, options, source, diagnostics) {
901
999
  });
902
1000
  const compiled = {
903
1001
  code: result.code,
1002
+ ...result.map ? { map: result.map } : {},
904
1003
  css: result.css,
905
1004
  scopeId,
906
1005
  hasScoped: result.hasScoped,
@@ -964,6 +1063,7 @@ function compileBatch(files, cache, options) {
964
1063
  for (const fileResult of result.results) {
965
1064
  if (fileResult.errors.length === 0) cache.set(fileResult.path, {
966
1065
  code: fileResult.code,
1066
+ ...fileResult.map ? { map: fileResult.map } : {},
967
1067
  css: fileResult.css,
968
1068
  scopeId: fileResult.scopeId,
969
1069
  hasScoped: fileResult.hasScoped,
@@ -1039,7 +1139,7 @@ function resolveCompilerIdentity() {
1039
1139
  */
1040
1140
  function computePrecompileCacheKey(compileOptions) {
1041
1141
  const material = stableStringify({
1042
- format: 2,
1142
+ format: 3,
1043
1143
  compiler: resolveCompilerIdentity(),
1044
1144
  options: compileOptions
1045
1145
  });
@@ -1158,7 +1258,7 @@ function encodePrecompileManifest(options) {
1158
1258
  const indexBody = compressBody(Buffer.from(JSON.stringify(index), "utf8"));
1159
1259
  const payloadBody = compressBody(Buffer.concat(records));
1160
1260
  const header = Buffer.from(JSON.stringify({
1161
- format: 2,
1261
+ format: 3,
1162
1262
  key,
1163
1263
  codec: hasZstd ? "zstd" : "gzip",
1164
1264
  index: indexBody.length,
@@ -1234,7 +1334,7 @@ function decodePrecompileManifest(bytes, options) {
1234
1334
  return reject("unparsable header");
1235
1335
  }
1236
1336
  if (typeof header !== "object" || header === null || Array.isArray(header)) return reject("unrecognized header");
1237
- if (header.format !== 2 || header.key !== key) return reject("foreign format or key");
1337
+ if (header.format !== 3 || header.key !== key) return reject("foreign format or key");
1238
1338
  if (!isByteLength(header.index) || !isByteLength(header.payload)) return reject("unrecognized body lengths");
1239
1339
  const indexStart = headerEnd + 1;
1240
1340
  const payloadStart = indexStart + header.index;
@@ -1450,6 +1550,7 @@ function writeManifest(file, container, onDiagnostic) {
1450
1550
  */
1451
1551
  function resolvePrecompileBatchOptions(state) {
1452
1552
  return {
1553
+ sourceMap: getCompileOptionsForRequest(state, false).sourceMap,
1453
1554
  ssr: false,
1454
1555
  vapor: state.mergedOptions.vapor ?? false,
1455
1556
  mode: state.mergedOptions.mode,
@@ -2464,12 +2565,15 @@ function loadCompiledSfcModule(state, realPath, isSsr, currentBase, loadOptions)
2464
2565
  ...compiled,
2465
2566
  css: resolveCssImports(compiled.css, realPath, state.cssAliasRules, state.server !== null, currentBase)
2466
2567
  };
2467
- const generatedOutput = generateOutput(compiled, outputOptions);
2468
- const normalizedOutput = rewriteImportMetaGlobBase(rewriteStaticAssetUrls(rewriteDynamicTemplateImports(isSsr ? normalizeVueServerRendererImport(generatedOutput) : generatedOutput, state.dynamicImportAliasRules), state.dynamicImportAliasRules), realPath, state.root);
2568
+ const emitted = generateOutputWithMap(compiled, outputOptions);
2569
+ const rewritten = new MappedModule(isSsr ? normalizeVueServerRendererImport(emitted.code) : emitted.code, emitted.map);
2570
+ rewritten.edit(rewriteDynamicTemplateImports(rewritten.code, state.dynamicImportAliasRules));
2571
+ rewritten.edit(rewriteStaticAssetUrls(rewritten.code, state.dynamicImportAliasRules));
2572
+ rewritten.edit(rewriteImportMetaGlobBase(rewritten.code, realPath, state.root));
2469
2573
  if (!loadOptions?.ssr) state.pendingHmrUpdateTypes.delete(realPath);
2470
2574
  return {
2471
- code: normalizedOutput,
2472
- map: null
2575
+ code: rewritten.code,
2576
+ map: rewritten.map
2473
2577
  };
2474
2578
  }
2475
2579
  function loadDefinePageArtifact(state, realPath, ssr) {
@@ -3343,6 +3447,7 @@ function vize(options = {}) {
3343
3447
  pendingHmrUpdateTypes: /* @__PURE__ */ new Map(),
3344
3448
  viteResolveCache: /* @__PURE__ */ new Map(),
3345
3449
  isProduction: false,
3450
+ viteBuildSourcemap: false,
3346
3451
  root: "",
3347
3452
  clientViteBase: "/",
3348
3453
  serverViteBase: "/",
@@ -3381,6 +3486,7 @@ function vize(options = {}) {
3381
3486
  async configResolved(resolvedConfig) {
3382
3487
  state.root = options.root ?? resolvedConfig.root;
3383
3488
  state.isProduction = options.isProduction ?? resolvedConfig.isProduction;
3489
+ state.viteBuildSourcemap = !!resolvedConfig.build?.sourcemap;
3384
3490
  const isSsrBuild = !!resolvedConfig.build?.ssr;
3385
3491
  const currentBase = resolvedConfig.command === "serve" ? options.devUrlBase ?? resolvedConfig.base ?? "/" : resolvedConfig.base ?? "/";
3386
3492
  if (isSsrBuild) state.serverViteBase = currentBase;
@@ -1,4 +1,4 @@
1
- import { c as ResolvedVizeConfig } from "../types-x-lq08Y8.mjs";
1
+ import { c as ResolvedVizeConfig } from "../types-Cm3dJq25.mjs";
2
2
  import { ResolvedConfig } from "vite";
3
3
 
4
4
  //#region src/internal/config-bridge.d.ts
@@ -660,6 +660,28 @@ interface ExperimentalPluginOptions extends ExperimentalCompileFlags {
660
660
  experimentals?: ExperimentalOptions;
661
661
  }
662
662
  //#endregion
663
+ //#region src/utils/module-output.d.ts
664
+ type ModuleOutputInfo = {
665
+ hasDefaultExport: boolean;
666
+ hasSfcMainDefined: boolean;
667
+ hasNamedRenderExport: boolean;
668
+ hasNamedSsrRenderExport: boolean;
669
+ defaultExportKeywordEnd: number | null;
670
+ defaultExportStart: number | null;
671
+ /**
672
+ * End offset of the whole `export default ...` statement, or `null`.
673
+ *
674
+ * Recorded so {@link insertBeforeSfcMainDefaultExport} can reuse the caller's
675
+ * analysis instead of parsing the module a second time (#3425).
676
+ */
677
+ defaultExportEnd: number | null;
678
+ /**
679
+ * Whether the default export's declaration is exactly the `_sfc_main`
680
+ * identifier -- the only shape {@link insertBeforeSfcMainDefaultExport} acts on.
681
+ */
682
+ defaultExportIsSfcMain: boolean;
683
+ };
684
+ //#endregion
663
685
  //#region src/types.d.ts
664
686
  interface MacroArtifact {
665
687
  kind: string;
@@ -755,7 +777,10 @@ interface VizeOptions extends ExperimentalPluginOptions {
755
777
  */
756
778
  isProduction?: boolean;
757
779
  ssr?: boolean;
758
- /** Enable source map generation */
780
+ /**
781
+ * Enable source map generation.
782
+ * @default development on; production off unless Vite's `build.sourcemap` is set
783
+ */
759
784
  sourceMap?: boolean;
760
785
  /**
761
786
  * Enable Vapor mode compilation
@@ -845,6 +870,13 @@ interface StyleBlockInfo {
845
870
  }
846
871
  interface CompiledModule {
847
872
  code: string;
873
+ /**
874
+ * Source Map v3 document (JSON) describing `code`, when the compiler was
875
+ * asked for one (#3399). Absent when source maps are off, when the SFC has no
876
+ * script block, and for the rspack and unplugin builders, which do not request
877
+ * maps. Persisted with the rest of the module in the pre-compile cache.
878
+ */
879
+ map?: string;
848
880
  css?: string;
849
881
  scopeId: string;
850
882
  hasScoped: boolean;
@@ -857,6 +889,13 @@ interface CompiledModule {
857
889
  styles?: StyleBlockInfo[];
858
890
  /** Files loaded through SFC `src` imports */
859
891
  dependencies?: string[];
892
+ /**
893
+ * Module shape reported by the native compiler, so `generateOutput` need not
894
+ * re-parse the emitted module (#3425). Absent for a cache entry written before
895
+ * the field existed, and for the rspack and unplugin builders, which never set
896
+ * it — both fall back to parsing.
897
+ */
898
+ moduleShape?: ModuleOutputInfo;
860
899
  }
861
900
  //#endregion
862
901
  export { VizeVueVersion as a, ResolvedVizeConfig as c, VizeOptions as i, UserConfigExport as l, MacroArtifact as n, ConfigEnv as o, VizeCompatibilityOptions as r, LoadConfigOptions as s, CompiledModule as t, VizeConfig as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizejs/vite-plugin",
3
- "version": "0.310.0",
3
+ "version": "0.312.0",
4
4
  "description": "High-performance native Vite plugin for Vue SFC compilation powered by Vize",
5
5
  "keywords": [
6
6
  "compiler",
@@ -45,10 +45,10 @@
45
45
  "access": "public"
46
46
  },
47
47
  "dependencies": {
48
- "@vizejs/native": "0.310.0",
48
+ "@vizejs/native": "0.312.0",
49
49
  "oxc-parser": "0.133.0",
50
50
  "tinyglobby": "0.2.16",
51
- "vize": "0.310.0"
51
+ "vize": "0.312.0"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@types/node": "25.9.2",