@vizejs/vite-plugin 0.306.0 → 0.310.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 +92 -4
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -4,9 +4,9 @@ import crypto, { createHash } from "node:crypto";
4
4
  import * as native from "@vizejs/native";
5
5
  import { applyViteDefineReplacements, buildInspectorGraph, chunkVitePrecompileFiles, classifyVitePluginRequest, createViteBareImportBases, createViteBareImportCandidates, createViteVirtualId, detectViteHmrUpdateType, diffVitePrecompileFiles, generateViteHmrCode, hasViteHmrChanges, isViteBareSpecifier, normalizeViteCssModuleFilename, normalizeViteDevMiddlewareUrl, normalizeVitePrecompileBatchSize, normalizeViteRequireBase, normalizeViteResolvedVuePath, resolveViteAliasRequest, resolveViteCssImports, resolveViteRelativeImport, resolveViteVuePath, rewriteViteDynamicTemplateImports, rewriteViteImportMetaGlobBase, rewriteViteStaticAssetUrls, scopeViteCssForPipeline, shouldApplyViteDefineInVirtualModule, splitViteIdQuery, toViteBrowserImportPrefix, transformViteCssVarsForPipeline } from "@vizejs/native";
6
6
  import { parseSync } from "oxc-parser";
7
+ import path from "node:path";
7
8
  import fs from "node:fs";
8
9
  import { glob } from "tinyglobby";
9
- import path from "node:path";
10
10
  import zlib from "node:zlib";
11
11
  import { pathToFileURL } from "node:url";
12
12
  import * as vite from "vite";
@@ -687,6 +687,84 @@ function clearBuildCaches(state) {
687
687
  state.viteResolveCache?.clear();
688
688
  }
689
689
  //#endregion
690
+ //#region src/plugin/compiled-module-cache.ts
691
+ /**
692
+ * Compiled-module caches carrying a reverse index from a `src`-imported
693
+ * dependency to the SFCs that pulled it in.
694
+ *
695
+ * Every hot update has to answer "which SFCs own this changed file?" before it
696
+ * can do anything else, and that answer used to come from a full scan of both
697
+ * caches with a `path.resolve` per dependency per cached file. HMR latency then
698
+ * grew with the number of components in the project, on the interactive path,
699
+ * for every save — including saves of ordinary `.vue` files, because the scan
700
+ * runs before the `.vue` fast path.
701
+ *
702
+ * The index is maintained by overriding `set`/`delete`/`clear` rather than by a
703
+ * second structure updated at each call site. `state.cache` is handed to
704
+ * `compileFile` and `compileBatch`, and is also mutated directly from
705
+ * `precompile-run.ts`, `hmr.ts` and `state.ts`; a structure kept in sync by hand
706
+ * across those would drift the first time a new writer appeared. Subclassing
707
+ * puts the bookkeeping where the mutation is.
708
+ *
709
+ * The keys are `path.resolve`d exactly as the previous scan resolved them, so a
710
+ * dependency recorded as a relative path indexes and looks up identically.
711
+ */
712
+ var CompiledModuleCache = class extends Map {
713
+ #ownersByDependency = /* @__PURE__ */ new Map();
714
+ /**
715
+ * Deliberately takes no entries: `Map`'s constructor would call the
716
+ * overridden `set` from inside `super()`, before `#ownersByDependency` exists.
717
+ */
718
+ constructor() {
719
+ super();
720
+ }
721
+ set(file, compiled) {
722
+ this.#unindex(file);
723
+ super.set(file, compiled);
724
+ for (const dependency of compiled.dependencies ?? []) {
725
+ const key = path.resolve(dependency);
726
+ const owners = this.#ownersByDependency.get(key);
727
+ if (owners) owners.add(file);
728
+ else this.#ownersByDependency.set(key, new Set([file]));
729
+ }
730
+ return this;
731
+ }
732
+ delete(file) {
733
+ this.#unindex(file);
734
+ return super.delete(file);
735
+ }
736
+ clear() {
737
+ this.#ownersByDependency.clear();
738
+ super.clear();
739
+ }
740
+ /** The cached SFCs that `src`-import `resolvedDependency`. */
741
+ ownersOf(resolvedDependency) {
742
+ const owners = this.#ownersByDependency.get(resolvedDependency);
743
+ return owners ? [...owners] : [];
744
+ }
745
+ #unindex(file) {
746
+ for (const dependency of super.get(file)?.dependencies ?? []) {
747
+ const key = path.resolve(dependency);
748
+ const owners = this.#ownersByDependency.get(key);
749
+ if (!owners) continue;
750
+ owners.delete(file);
751
+ if (owners.size === 0) this.#ownersByDependency.delete(key);
752
+ }
753
+ }
754
+ };
755
+ /**
756
+ * Owners of `resolvedDependency` in one cache.
757
+ *
758
+ * Plain `Map`s — the hand-built states in unit tests — keep the original linear
759
+ * scan, so an unindexed cache answers exactly what the indexed one does.
760
+ */
761
+ function ownersOfDependency(cache, resolvedDependency) {
762
+ if (cache instanceof CompiledModuleCache) return cache.ownersOf(resolvedDependency);
763
+ const owners = [];
764
+ for (const [file, compiled] of cache) if (compiled.dependencies?.some((dependency) => path.resolve(dependency) === resolvedDependency)) owners.push(file);
765
+ return owners;
766
+ }
767
+ //#endregion
690
768
  //#region src/compile-options.ts
691
769
  function buildCompileFileOptions(filePath, options) {
692
770
  return {
@@ -2539,10 +2617,20 @@ async function transformHook(state, code, id, options) {
2539
2617
  //#region src/plugin/hmr.ts
2540
2618
  const VIZE_COMPONENTS_CSS_BASENAME = "vize-components.css";
2541
2619
  const VIZE_COMPONENTS_CSS_FILE = `assets/${VIZE_COMPONENTS_CSS_BASENAME}`;
2620
+ /**
2621
+ * The cached SFCs that pulled `dependencyFile` in through
2622
+ * `<script src>` / `<template src>` / `<style src>`.
2623
+ *
2624
+ * This runs as the first statement of every hot update, before the `.vue` fast
2625
+ * path, so editing an ordinary SFC pays for it too. It used to walk both caches
2626
+ * end to end with a `path.resolve` per dependency, which made HMR latency grow
2627
+ * with the number of components in the project; the caches now carry a reverse
2628
+ * index that answers it in constant time. See `compiled-module-cache.ts`.
2629
+ */
2542
2630
  function getVueFilesDependingOn(state, dependencyFile) {
2543
2631
  const normalizedDependency = path.resolve(dependencyFile);
2544
2632
  const owners = /* @__PURE__ */ new Set();
2545
- for (const cache of [state.cache, state.ssrCache]) for (const [vueFile, compiled] of cache) if (compiled.dependencies?.some((dependency) => path.resolve(dependency) === normalizedDependency)) owners.add(vueFile);
2633
+ for (const cache of [state.cache, state.ssrCache]) for (const vueFile of ownersOfDependency(cache, normalizedDependency)) owners.add(vueFile);
2546
2634
  return [...owners];
2547
2635
  }
2548
2636
  function unique(values) {
@@ -3248,8 +3336,8 @@ function resolveCompatibilityOptions(options, compilerConfig = {}) {
3248
3336
  function vize(options = {}) {
3249
3337
  if (isLegacyVueCompatibilityMode(options)) return [createLegacyVueCompatibilityPlugin(options)];
3250
3338
  const state = {
3251
- cache: /* @__PURE__ */ new Map(),
3252
- ssrCache: /* @__PURE__ */ new Map(),
3339
+ cache: new CompiledModuleCache(),
3340
+ ssrCache: new CompiledModuleCache(),
3253
3341
  collectedCss: /* @__PURE__ */ new Map(),
3254
3342
  precompileMetadata: /* @__PURE__ */ new Map(),
3255
3343
  pendingHmrUpdateTypes: /* @__PURE__ */ new Map(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizejs/vite-plugin",
3
- "version": "0.306.0",
3
+ "version": "0.310.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.306.0",
48
+ "@vizejs/native": "0.310.0",
49
49
  "oxc-parser": "0.133.0",
50
50
  "tinyglobby": "0.2.16",
51
- "vize": "0.306.0"
51
+ "vize": "0.310.0"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@types/node": "25.9.2",