(opts: { verbose?: boolean })
| 2162 | // ============================================================================ |
| 2163 | |
| 2164 | function createHmrWebSocketPlugin(opts: { verbose?: boolean }): Plugin { |
| 2165 | const verbose = !!opts.verbose; |
| 2166 | let wss: WebSocketServer | null = null; |
| 2167 | const sfcFileMap = new Map<string, string>(); |
| 2168 | const depFileMap = new Map<string, string>(); |
| 2169 | // Generic module manifest (spec -> emitted relative .mjs path) |
| 2170 | // Populated lazily on-demand via ns:fetch-module responses and broadcast to clients. |
| 2171 | // Enables clients to short-circuit fetches for already-known modules and aids |
| 2172 | // consistent path normalization across reconnects. |
| 2173 | const moduleManifest = new Map<string, string>(); |
| 2174 | let registrySent = false; |
| 2175 | let vendorBootstrapDone = false; |
| 2176 | // Graph state |
| 2177 | interface GraphModule { |
| 2178 | id: string; |
| 2179 | deps: string[]; |
| 2180 | hash: string; |
| 2181 | } |
| 2182 | let pluginRoot: string | undefined; |
| 2183 | let graphVersion = 0; |
| 2184 | // Transactional HMR batches: map graphVersion -> ordered list of changed ids for that version |
| 2185 | const txnBatches: Map<number, string[]> = new Map(); |
| 2186 | const graph = new Map<string, GraphModule>(); |
| 2187 | // Compute a dependency-closed, topologically sorted list of modules for a given set of changed ids. |
| 2188 | // Only include application modules we can serve (e.g., under /src and known .vue/.ts/.js entries in the graph). |
| 2189 | function computeTxnOrderForChanged(changedIds: string[]): string[] { |
| 2190 | const includeExt = (id: string) => ACTIVE_STRATEGY.matchesFile(id) || /\.(ts|js|mjs|tsx|jsx)$/i.test(id); |
| 2191 | const isApp = (id: string) => id.startsWith(APP_VIRTUAL_WITH_SLASH); |
| 2192 | const roots = changedIds.map(normalizeGraphId).filter((id) => graph.has(id) && (isApp(id) || ACTIVE_STRATEGY.matchesFile(id)) && includeExt(id)); |
| 2193 | const toVisit = new Set<string>(); |
| 2194 | // Collect dependency closure (downstream deps from roots) |
| 2195 | const stack: string[] = [...roots]; |
| 2196 | while (stack.length) { |
| 2197 | const id = stack.pop()!; |
| 2198 | if (toVisit.has(id)) continue; |
| 2199 | toVisit.add(id); |
| 2200 | const m = graph.get(id); |
| 2201 | if (m) { |
| 2202 | for (const d of m.deps) { |
| 2203 | if (!graph.has(d)) continue; |
| 2204 | if ((isApp(d) || ACTIVE_STRATEGY.matchesFile(d)) && includeExt(d) && !toVisit.has(d)) { |
| 2205 | stack.push(d); |
| 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | // Topological order: deps first (post-order DFS) |
| 2211 | const ordered: string[] = []; |
| 2212 | const temp = new Set<string>(); |
| 2213 | const perm = new Set<string>(); |
| 2214 | const visit = (id: string) => { |
| 2215 | if (perm.has(id)) return; |
| 2216 | if (temp.has(id)) { |
| 2217 | perm.add(id); |
| 2218 | return; |
| 2219 | } // cycle: bail out |
| 2220 | temp.add(id); |
| 2221 | const m = graph.get(id); |
no outgoing calls
no test coverage detected