@shijiu/jsview 2.1.200 → 2.1.340-test.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.
@@ -1,13 +1,16 @@
1
1
  import fs from 'node:fs';
2
2
  import { isCSSRequest, normalizePath as normalizePath$1, transformWithEsbuild, formatPostcssSourceMap, createFilter } from 'vite';
3
+ import { shallowRef, computed } from 'vue';
3
4
  import { createRequire } from 'node:module';
4
5
  import path from 'node:path';
5
6
  import { createHash } from 'node:crypto';
6
7
  import require$$0 from 'tty';
7
8
  import require$$1 from 'util';
8
9
 
10
+ const version = "5.0.3";
11
+
9
12
  function resolveCompiler(root) {
10
- const compiler = tryRequire("vue/compiler-sfc", root) || tryRequire("vue/compiler-sfc");
13
+ const compiler = tryResolveCompiler(root) || tryResolveCompiler();
11
14
  if (!compiler) {
12
15
  throw new Error(
13
16
  `Failed to resolve vue/compiler-sfc.
@@ -16,6 +19,12 @@ function resolveCompiler(root) {
16
19
  }
17
20
  return compiler;
18
21
  }
22
+ function tryResolveCompiler(root) {
23
+ const vueMeta = tryRequire("vue/package.json", root);
24
+ if (vueMeta && vueMeta.version.split(".")[0] >= 3) {
25
+ return tryRequire("vue/compiler-sfc", root);
26
+ }
27
+ }
19
28
  const _require = createRequire(import.meta.url);
20
29
  function tryRequire(id, from) {
21
30
  try {
@@ -49,7 +58,7 @@ function parseVueRequest(id) {
49
58
  }
50
59
 
51
60
  function slash(path) {
52
- const isExtendedLengthPath = /^\\\\\?\\/.test(path);
61
+ const isExtendedLengthPath = path.startsWith('\\\\?\\');
53
62
 
54
63
  if (isExtendedLengthPath) {
55
64
  return path;
@@ -59,34 +68,43 @@ function slash(path) {
59
68
  }
60
69
 
61
70
  const cache = /* @__PURE__ */ new Map();
71
+ const hmrCache = /* @__PURE__ */ new Map();
62
72
  const prevCache = /* @__PURE__ */ new Map();
63
- function createDescriptor(filename, source, { root, isProduction, sourceMap, compiler }) {
73
+ function createDescriptor(filename, source, { root, isProduction, sourceMap, compiler, template }, hmr = false) {
64
74
  const { descriptor, errors } = compiler.parse(source, {
65
75
  filename,
66
- sourceMap
76
+ sourceMap,
77
+ templateParseOptions: template?.compilerOptions
67
78
  });
68
79
  const normalizedPath = slash(path.normalize(path.relative(root, filename)));
69
80
  descriptor.id = getHash(normalizedPath + (isProduction ? source : ""));
70
- cache.set(filename, descriptor);
81
+ (hmr ? hmrCache : cache).set(filename, descriptor);
71
82
  return { descriptor, errors };
72
83
  }
73
84
  function getPrevDescriptor(filename) {
74
85
  return prevCache.get(filename);
75
86
  }
76
- function setPrevDescriptor(filename, entry) {
77
- prevCache.set(filename, entry);
87
+ function invalidateDescriptor(filename, hmr = false) {
88
+ const _cache = hmr ? hmrCache : cache;
89
+ const prev = _cache.get(filename);
90
+ _cache.delete(filename);
91
+ if (prev) {
92
+ prevCache.set(filename, prev);
93
+ }
78
94
  }
79
- function getDescriptor(filename, options, createIfNotFound = true) {
80
- if (cache.has(filename)) {
81
- return cache.get(filename);
95
+ function getDescriptor(filename, options, createIfNotFound = true, hmr = false, code) {
96
+ const _cache = hmr ? hmrCache : cache;
97
+ if (_cache.has(filename)) {
98
+ return _cache.get(filename);
82
99
  }
83
100
  if (createIfNotFound) {
84
101
  const { descriptor, errors } = createDescriptor(
85
102
  filename,
86
- fs.readFileSync(filename, "utf-8"),
87
- options
103
+ code ?? fs.readFileSync(filename, "utf-8"),
104
+ options,
105
+ hmr
88
106
  );
89
- if (errors.length) {
107
+ if (errors.length && !hmr) {
90
108
  throw errors[0];
91
109
  }
92
110
  return descriptor;
@@ -98,6 +116,20 @@ function getSrcDescriptor(filename, query) {
98
116
  }
99
117
  return cache.get(filename);
100
118
  }
119
+ function getTempSrcDescriptor(filename, query) {
120
+ return {
121
+ filename,
122
+ id: query.id || "",
123
+ styles: [
124
+ {
125
+ scoped: query.scoped,
126
+ loc: {
127
+ start: { line: 0, column: 0 }
128
+ }
129
+ }
130
+ ]
131
+ };
132
+ }
101
133
  function setSrcDescriptor(filename, entry, scoped) {
102
134
  if (scoped) {
103
135
  cache.set(`${filename}?src=${entry.id}`, entry);
@@ -128,8 +160,15 @@ function createRollupError(id, error) {
128
160
  return rollupError;
129
161
  }
130
162
 
131
- async function transformTemplateAsModule(code, descriptor, options, pluginContext, ssr) {
132
- const result = compile(code, descriptor, options, pluginContext, ssr);
163
+ async function transformTemplateAsModule(code, descriptor, options, pluginContext, ssr, customElement) {
164
+ const result = compile(
165
+ code,
166
+ descriptor,
167
+ options,
168
+ pluginContext,
169
+ ssr,
170
+ customElement
171
+ );
133
172
  let returnCode = result.code;
134
173
  if (options.devServer && options.devServer.config.server.hmr !== false && !ssr && !options.isProduction) {
135
174
  returnCode += `
@@ -142,8 +181,15 @@ import.meta.hot.accept(({ render }) => {
142
181
  map: result.map
143
182
  };
144
183
  }
145
- function transformTemplateInMain(code, descriptor, options, pluginContext, ssr) {
146
- const result = compile(code, descriptor, options, pluginContext, ssr);
184
+ function transformTemplateInMain(code, descriptor, options, pluginContext, ssr, customElement) {
185
+ const result = compile(
186
+ code,
187
+ descriptor,
188
+ options,
189
+ pluginContext,
190
+ ssr,
191
+ customElement
192
+ );
147
193
  return {
148
194
  ...result,
149
195
  code: result.code.replace(
@@ -152,8 +198,9 @@ function transformTemplateInMain(code, descriptor, options, pluginContext, ssr)
152
198
  )
153
199
  };
154
200
  }
155
- function compile(code, descriptor, options, pluginContext, ssr) {
201
+ function compile(code, descriptor, options, pluginContext, ssr, customElement) {
156
202
  const filename = descriptor.filename;
203
+ resolveScript(descriptor, options, ssr, customElement);
157
204
  const result = options.compiler.compileTemplate({
158
205
  ...resolveTemplateCompilerOptions(descriptor, options, ssr),
159
206
  source: code
@@ -189,7 +236,8 @@ function resolveTemplateCompilerOptions(descriptor, options, ssr) {
189
236
  if (filename.startsWith(options.root)) {
190
237
  const devBase = options.devServer.config.base;
191
238
  assetUrlOptions = {
192
- base: (options.devServer.config.server?.origin ?? "") + devBase + slash(path.relative(options.root, path.dirname(filename)))
239
+ base: (options.devServer.config.server?.origin ?? "") + devBase + slash(path.relative(options.root, path.dirname(filename))),
240
+ includeAbsolute: !!devBase
193
241
  };
194
242
  }
195
243
  } else if (transformAssetUrls !== false) {
@@ -224,6 +272,7 @@ function resolveTemplateCompilerOptions(descriptor, options, ssr) {
224
272
  return {
225
273
  ...options.template,
226
274
  id,
275
+ ast: canReuseAST(options.compiler.version) ? descriptor.template?.ast : void 0,
227
276
  filename,
228
277
  scoped: hasScoped,
229
278
  slotted: descriptor.slotted,
@@ -232,7 +281,7 @@ function resolveTemplateCompilerOptions(descriptor, options, ssr) {
232
281
  ssr,
233
282
  ssrCssVars: cssVars,
234
283
  transformAssetUrls,
235
- preprocessLang: block.lang,
284
+ preprocessLang: block.lang === "html" ? void 0 : block.lang,
236
285
  preprocessOptions,
237
286
  compilerOptions: {
238
287
  ...options.template?.compilerOptions,
@@ -243,9 +292,26 @@ function resolveTemplateCompilerOptions(descriptor, options, ssr) {
243
292
  }
244
293
  };
245
294
  }
295
+ function canReuseAST(version) {
296
+ if (version) {
297
+ const [_, minor, patch] = version.split(".").map(Number);
298
+ if (minor >= 4 && patch >= 3) {
299
+ return true;
300
+ }
301
+ }
302
+ return false;
303
+ }
246
304
 
247
305
  const clientCache = /* @__PURE__ */ new WeakMap();
248
306
  const ssrCache = /* @__PURE__ */ new WeakMap();
307
+ const typeDepToSFCMap = /* @__PURE__ */ new Map();
308
+ function invalidateScript(filename) {
309
+ const desc = cache.get(filename);
310
+ if (desc) {
311
+ clientCache.delete(desc);
312
+ ssrCache.delete(desc);
313
+ }
314
+ }
249
315
  function getResolvedScript(descriptor, ssr) {
250
316
  return (ssr ? ssrCache : clientCache).get(descriptor);
251
317
  }
@@ -255,16 +321,16 @@ function setResolvedScript(descriptor, script, ssr) {
255
321
  function isUseInlineTemplate(descriptor, isProd) {
256
322
  return isProd && !!descriptor.scriptSetup && !descriptor.template?.src;
257
323
  }
258
- function resolveScript(descriptor, options, ssr) {
324
+ const scriptIdentifier = `_sfc_main`;
325
+ function resolveScript(descriptor, options, ssr, customElement) {
259
326
  if (!descriptor.script && !descriptor.scriptSetup) {
260
327
  return null;
261
328
  }
262
- const cacheToUse = ssr ? ssrCache : clientCache;
263
329
  // JsView Added >>>
264
330
  // 1. 解决style内容发生变化,不使用cache,在templete class和style同时发生变化没有被更新问题。
265
331
  // 2. 解决@import的css文件没有被更新问题。
266
- // const cached = cacheToUse.get(descriptor);
267
- let cached = cacheToUse.get(descriptor);
332
+ // const cached = getResolvedScript(descriptor, ssr);
333
+ let cached = getResolvedScript(descriptor, ssr);
268
334
  if (cached && descriptor.filename?.endsWith('.vue') && descriptor.styles) {
269
335
  for (const style of descriptor.styles) {
270
336
  let useCache = false;
@@ -296,13 +362,42 @@ function resolveScript(descriptor, options, ssr) {
296
362
  id: descriptor.id,
297
363
  isProd: options.isProduction,
298
364
  inlineTemplate: isUseInlineTemplate(descriptor, !options.devServer),
299
- reactivityTransform: options.reactivityTransform !== false,
300
365
  templateOptions: resolveTemplateCompilerOptions(descriptor, options, ssr),
301
- sourceMap: options.sourceMap
366
+ sourceMap: options.sourceMap,
367
+ genDefaultAs: canInlineMain(descriptor, options) ? scriptIdentifier : void 0,
368
+ customElement
302
369
  });
303
- cacheToUse.set(descriptor, resolved);
370
+ if (!options.isProduction && resolved?.deps) {
371
+ for (const [key, sfcs] of typeDepToSFCMap) {
372
+ if (sfcs.has(descriptor.filename) && !resolved.deps.includes(key)) {
373
+ sfcs.delete(descriptor.filename);
374
+ }
375
+ }
376
+ for (const dep of resolved.deps) {
377
+ const existingSet = typeDepToSFCMap.get(dep);
378
+ if (!existingSet) {
379
+ typeDepToSFCMap.set(dep, /* @__PURE__ */ new Set([descriptor.filename]));
380
+ } else {
381
+ existingSet.add(descriptor.filename);
382
+ }
383
+ }
384
+ }
385
+ setResolvedScript(descriptor, resolved, ssr);
304
386
  return resolved;
305
387
  }
388
+ function canInlineMain(descriptor, options) {
389
+ if (descriptor.script?.src || descriptor.scriptSetup?.src) {
390
+ return false;
391
+ }
392
+ const lang = descriptor.script?.lang || descriptor.scriptSetup?.lang;
393
+ if (!lang || lang === "js") {
394
+ return true;
395
+ }
396
+ if (lang === "ts" && options.devServer) {
397
+ return true;
398
+ }
399
+ return false;
400
+ }
306
401
 
307
402
  const comma = ','.charCodeAt(0);
308
403
  const semicolon = ';'.charCodeAt(0);
@@ -787,7 +882,7 @@ class TraceMap {
787
882
  const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
788
883
  this.version = version;
789
884
  this.file = file;
790
- this.names = names;
885
+ this.names = names || [];
791
886
  this.sourceRoot = sourceRoot;
792
887
  this.sources = sources;
793
888
  this.sourcesContent = sourcesContent;
@@ -1777,8 +1872,8 @@ function requireBrowser () {
1777
1872
  } catch (error) {
1778
1873
  return '[UnexpectedJSONParseError]: ' + error.message;
1779
1874
  }
1780
- };
1781
- } (browser, browser.exports));
1875
+ };
1876
+ } (browser, browser.exports));
1782
1877
  return browser.exports;
1783
1878
  }
1784
1879
 
@@ -2052,8 +2147,8 @@ function requireNode () {
2052
2147
  formatters.O = function (v) {
2053
2148
  this.inspectOpts.colors = this.useColors;
2054
2149
  return util.inspect(v, this.inspectOpts);
2055
- };
2056
- } (node, node.exports));
2150
+ };
2151
+ } (node, node.exports));
2057
2152
  return node.exports;
2058
2153
  }
2059
2154
 
@@ -2062,42 +2157,32 @@ function requireNode () {
2062
2157
  * treat as a browser.
2063
2158
  */
2064
2159
 
2065
- (function (module) {
2066
- if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
2067
- module.exports = requireBrowser();
2068
- } else {
2069
- module.exports = requireNode();
2070
- }
2071
- } (src));
2160
+ if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
2161
+ src.exports = requireBrowser();
2162
+ } else {
2163
+ src.exports = requireNode();
2164
+ }
2072
2165
 
2073
- const _debug = /*@__PURE__*/getDefaultExportFromCjs(src.exports);
2166
+ var srcExports = src.exports;
2167
+ const _debug = /*@__PURE__*/getDefaultExportFromCjs(srcExports);
2074
2168
 
2075
2169
  const debug = _debug("vite:hmr");
2076
2170
  const directRequestRE = /(?:\?|&)direct\b/;
2077
- async function handleHotUpdate({ file, modules, read, server }, options) {
2078
- const prevDescriptor = getDescriptor(file, options, false);
2171
+ async function handleHotUpdate({ file, modules, read }, options, customElement) {
2172
+ const prevDescriptor = getDescriptor(file, options, false, true);
2079
2173
  if (!prevDescriptor) {
2080
2174
  return;
2081
2175
  }
2082
- setPrevDescriptor(file, prevDescriptor);
2083
2176
  const content = await read();
2084
- const { descriptor } = createDescriptor(file, content, options);
2177
+ const { descriptor } = createDescriptor(file, content, options, true);
2085
2178
  let needRerender = false;
2086
2179
  const affectedModules = /* @__PURE__ */ new Set();
2087
- const mainModule = modules.filter((m) => !/type=/.test(m.url) || /type=script/.test(m.url)).sort((m1, m2) => {
2088
- return m1.url.length - m2.url.length;
2089
- })[0];
2180
+ const mainModule = getMainModule(modules);
2090
2181
  const templateModule = modules.find((m) => /type=template/.test(m.url));
2182
+ resolveScript(descriptor, options, false, customElement);
2091
2183
  const scriptChanged = hasScriptChanged(prevDescriptor, descriptor);
2092
2184
  if (scriptChanged) {
2093
- let scriptModule;
2094
- if (descriptor.scriptSetup?.lang && !descriptor.scriptSetup.src || descriptor.script?.lang && !descriptor.script.src) {
2095
- const scriptModuleRE = new RegExp(
2096
- `type=script.*&lang.${descriptor.scriptSetup?.lang || descriptor.script?.lang}$`
2097
- );
2098
- scriptModule = modules.find((m) => scriptModuleRE.test(m.url));
2099
- }
2100
- affectedModules.add(scriptModule || mainModule);
2185
+ affectedModules.add(getScriptModule(modules) || mainModule);
2101
2186
  }
2102
2187
  if (!isEqualBlock(descriptor.template, prevDescriptor.template)) {
2103
2188
  if (!scriptChanged) {
@@ -2141,11 +2226,6 @@ async function handleHotUpdate({ file, modules, read, server }, options) {
2141
2226
  if (prevStyles.length > nextStyles.length) {
2142
2227
  affectedModules.add(mainModule);
2143
2228
  }
2144
- // JsView Added >>>
2145
- // 当Style发生变化时,更新Script,同步CSS to JS
2146
- // 貌似没有作用,已经被上面修改的一起处理了。
2147
- // affectedModules.add(mainModule);
2148
- // JsView Added <<<
2149
2229
  const prevCustoms = prevDescriptor.customBlocks || [];
2150
2230
  const nextCustoms = descriptor.customBlocks || [];
2151
2231
  if (prevCustoms.length !== nextCustoms.length) {
@@ -2182,6 +2262,11 @@ async function handleHotUpdate({ file, modules, read, server }, options) {
2182
2262
  updateType.push(`style`);
2183
2263
  }
2184
2264
  if (updateType.length) {
2265
+ if (file.endsWith(".vue")) {
2266
+ invalidateDescriptor(file);
2267
+ } else {
2268
+ cache.set(file, descriptor);
2269
+ }
2185
2270
  debug(`[vue:update(${updateType.join("&")})] ${file}`);
2186
2271
  }
2187
2272
  return [...affectedModules].filter(Boolean);
@@ -2205,11 +2290,62 @@ function isEqualBlock(a, b) {
2205
2290
  function isOnlyTemplateChanged(prev, next) {
2206
2291
  return !hasScriptChanged(prev, next) && prev.styles.length === next.styles.length && prev.styles.every((s, i) => isEqualBlock(s, next.styles[i])) && prev.customBlocks.length === next.customBlocks.length && prev.customBlocks.every((s, i) => isEqualBlock(s, next.customBlocks[i]));
2207
2292
  }
2293
+ function deepEqual(obj1, obj2, excludeProps = [], deepParentsOfObj1 = []) {
2294
+ if (typeof obj1 !== typeof obj2) {
2295
+ return false;
2296
+ }
2297
+ if (obj1 == null || obj2 == null || typeof obj1 !== "object" || deepParentsOfObj1.includes(obj1)) {
2298
+ return obj1 === obj2;
2299
+ }
2300
+ const keys1 = Object.keys(obj1);
2301
+ const keys2 = Object.keys(obj2);
2302
+ if (keys1.length !== keys2.length) {
2303
+ return false;
2304
+ }
2305
+ for (const key of keys1) {
2306
+ if (excludeProps.includes(key)) {
2307
+ continue;
2308
+ }
2309
+ if (!deepEqual(obj1[key], obj2[key], excludeProps, [
2310
+ ...deepParentsOfObj1,
2311
+ obj1
2312
+ ])) {
2313
+ return false;
2314
+ }
2315
+ }
2316
+ return true;
2317
+ }
2318
+ function isEqualAst(prev, next) {
2319
+ if (typeof prev === "undefined" || typeof next === "undefined") {
2320
+ return prev === next;
2321
+ }
2322
+ if (prev.length !== next.length) {
2323
+ return false;
2324
+ }
2325
+ for (let i = 0; i < prev.length; i++) {
2326
+ const prevNode = prev[i];
2327
+ const nextNode = next[i];
2328
+ if (!deepEqual(prevNode, nextNode, [
2329
+ "start",
2330
+ "end",
2331
+ "loc",
2332
+ "range",
2333
+ "leadingComments",
2334
+ "trailingComments",
2335
+ "innerComments"
2336
+ ])) {
2337
+ return false;
2338
+ }
2339
+ }
2340
+ return true;
2341
+ }
2208
2342
  function hasScriptChanged(prev, next) {
2209
- if (!isEqualBlock(prev.script, next.script)) {
2343
+ const prevScript = getResolvedScript(prev, false);
2344
+ const nextScript = getResolvedScript(next, false);
2345
+ if (!isEqualBlock(prev.script, next.script) && !isEqualAst(prevScript?.scriptAst, nextScript?.scriptAst)) {
2210
2346
  return true;
2211
2347
  }
2212
- if (!isEqualBlock(prev.scriptSetup, next.scriptSetup)) {
2348
+ if (!isEqualBlock(prev.scriptSetup, next.scriptSetup) && !isEqualAst(prevScript?.scriptSetupAst, nextScript?.scriptSetupAst)) {
2213
2349
  return true;
2214
2350
  }
2215
2351
  const prevResolvedScript = getResolvedScript(prev, false);
@@ -2219,6 +2355,26 @@ function hasScriptChanged(prev, next) {
2219
2355
  }
2220
2356
  return false;
2221
2357
  }
2358
+ function getMainModule(modules) {
2359
+ return modules.filter((m) => !/type=/.test(m.url) || /type=script/.test(m.url)).sort((m1, m2) => {
2360
+ return m1.url.length - m2.url.length;
2361
+ })[0];
2362
+ }
2363
+ function getScriptModule(modules) {
2364
+ return modules.find((m) => /type=script.*&lang\.\w+$/.test(m.url));
2365
+ }
2366
+ function handleTypeDepChange(affectedComponents, { modules, server: { moduleGraph } }) {
2367
+ const affected = /* @__PURE__ */ new Set();
2368
+ for (const file of affectedComponents) {
2369
+ invalidateScript(file);
2370
+ const mods = moduleGraph.getModulesByFile(file);
2371
+ if (mods) {
2372
+ const arr = [...mods];
2373
+ affected.add(getScriptModule(arr) || getMainModule(arr));
2374
+ }
2375
+ }
2376
+ return [...modules, ...affected];
2377
+ }
2222
2378
 
2223
2379
  const EXPORT_HELPER_ID = "\0plugin-vue:export-helper";
2224
2380
  const helperCode = `
@@ -2231,10 +2387,25 @@ export default (sfc, props) => {
2231
2387
  }
2232
2388
  `;
2233
2389
 
2234
- async function transformMain(code, filename, options, pluginContext, ssr, asCustomElement) {
2390
+ async function transformMain(code, filename, options, pluginContext, ssr, customElement) {
2235
2391
  const { devServer, isProduction, devToolsEnabled } = options;
2236
2392
  const prevDescriptor = getPrevDescriptor(filename);
2237
2393
  const { descriptor, errors } = createDescriptor(filename, code, options);
2394
+ if (fs.existsSync(filename)) {
2395
+ getDescriptor(
2396
+ filename,
2397
+ options,
2398
+ true,
2399
+ true,
2400
+ // for vue files, create descriptor from fs read to be consistent with
2401
+ // logic in handleHotUpdate()
2402
+ // for non vue files, e.g. md files in vitepress, we assume
2403
+ // `hmrContext.read` is overwriten so handleHotUpdate() is dealing with
2404
+ // post-transform code, so we populate the descriptor with post-transform
2405
+ // code here as well.
2406
+ filename.endsWith(".vue") ? void 0 : code
2407
+ );
2408
+ }
2238
2409
  if (errors.length) {
2239
2410
  errors.forEach(
2240
2411
  (error) => pluginContext.error(createRollupError(filename, error))
@@ -2247,7 +2418,8 @@ async function transformMain(code, filename, options, pluginContext, ssr, asCust
2247
2418
  descriptor,
2248
2419
  options,
2249
2420
  pluginContext,
2250
- ssr
2421
+ ssr,
2422
+ customElement
2251
2423
  );
2252
2424
  const hasTemplateImport = descriptor.template && !isUseInlineTemplate(descriptor, !devServer);
2253
2425
  let templateCode = "";
@@ -2257,7 +2429,8 @@ async function transformMain(code, filename, options, pluginContext, ssr, asCust
2257
2429
  descriptor,
2258
2430
  options,
2259
2431
  pluginContext,
2260
- ssr
2432
+ ssr,
2433
+ customElement
2261
2434
  ));
2262
2435
  }
2263
2436
  if (hasTemplateImport) {
@@ -2272,7 +2445,7 @@ async function transformMain(code, filename, options, pluginContext, ssr, asCust
2272
2445
  const stylesCode = await genStyleCode(
2273
2446
  descriptor,
2274
2447
  pluginContext,
2275
- asCustomElement,
2448
+ customElement,
2276
2449
  attachedProps
2277
2450
  );
2278
2451
  const customBlocksCode = await genCustomBlockCode(descriptor, pluginContext);
@@ -2331,9 +2504,12 @@ async function transformMain(code, filename, options, pluginContext, ssr, asCust
2331
2504
  if (options.sourceMap) {
2332
2505
  if (scriptMap && templateMap) {
2333
2506
  const gen = fromMap(
2507
+ // version property of result.map is declared as string
2508
+ // but actually it is `3`
2334
2509
  scriptMap
2335
2510
  );
2336
2511
  const tracer = new TraceMap(
2512
+ // same above
2337
2513
  templateMap
2338
2514
  );
2339
2515
  const offset = (scriptCode.match(/\r?\n/g)?.length ?? 0) + 1;
@@ -2391,16 +2567,17 @@ async function transformMain(code, filename, options, pluginContext, ssr, asCust
2391
2567
  }
2392
2568
  };
2393
2569
  }
2394
- async function genTemplateCode(descriptor, options, pluginContext, ssr) {
2570
+ async function genTemplateCode(descriptor, options, pluginContext, ssr, customElement) {
2395
2571
  const template = descriptor.template;
2396
2572
  const hasScoped = descriptor.styles.some((style) => style.scoped);
2397
- if (!template.lang && !template.src) {
2573
+ if ((!template.lang || template.lang === "html") && !template.src) {
2398
2574
  return transformTemplateInMain(
2399
2575
  template.content,
2400
2576
  descriptor,
2401
2577
  options,
2402
2578
  pluginContext,
2403
- ssr
2579
+ ssr,
2580
+ customElement
2404
2581
  );
2405
2582
  } else {
2406
2583
  if (template.src) {
@@ -2424,19 +2601,23 @@ async function genTemplateCode(descriptor, options, pluginContext, ssr) {
2424
2601
  };
2425
2602
  }
2426
2603
  }
2427
- async function genScriptCode(descriptor, options, pluginContext, ssr) {
2428
- let scriptCode = `const _sfc_main = {}`;
2604
+ async function genScriptCode(descriptor, options, pluginContext, ssr, customElement) {
2605
+ let scriptCode = `const ${scriptIdentifier} = {}`;
2429
2606
  let map;
2430
- const script = resolveScript(descriptor, options, ssr);
2607
+ const script = resolveScript(descriptor, options, ssr, customElement);
2431
2608
  if (script) {
2432
- if ((!script.lang || script.lang === "ts" && options.devServer) && !script.src) {
2433
- const userPlugins = options.script?.babelParserPlugins || [];
2434
- const defaultPlugins = script.lang === "ts" ? userPlugins.includes("decorators") ? ["typescript"] : ["typescript", "decorators-legacy"] : [];
2435
- scriptCode = options.compiler.rewriteDefault(
2436
- script.content,
2437
- "_sfc_main",
2438
- [...defaultPlugins, ...userPlugins]
2439
- );
2609
+ if (canInlineMain(descriptor, options)) {
2610
+ if (!options.compiler.version) {
2611
+ const userPlugins = options.script?.babelParserPlugins || [];
2612
+ const defaultPlugins = script.lang === "ts" ? userPlugins.includes("decorators") ? ["typescript"] : ["typescript", "decorators-legacy"] : [];
2613
+ scriptCode = options.compiler.rewriteDefault(
2614
+ script.content,
2615
+ scriptIdentifier,
2616
+ [...defaultPlugins, ...userPlugins]
2617
+ );
2618
+ } else {
2619
+ scriptCode = script.content;
2620
+ }
2440
2621
  map = script.map;
2441
2622
  } else {
2442
2623
  if (script.src) {
@@ -2457,7 +2638,7 @@ export * from ${request}`;
2457
2638
  map
2458
2639
  };
2459
2640
  }
2460
- async function genStyleCode(descriptor, pluginContext, asCustomElement, attachedProps) {
2641
+ async function genStyleCode(descriptor, pluginContext, customElement, attachedProps) {
2461
2642
  let stylesCode = ``;
2462
2643
  let cssModulesMap;
2463
2644
  if (descriptor.styles.length) {
@@ -2474,12 +2655,12 @@ async function genStyleCode(descriptor, pluginContext, asCustomElement, attached
2474
2655
  const src = style.src || descriptor.filename;
2475
2656
  const attrsQuery = attrsToQuery(style.attrs, "css");
2476
2657
  const srcQuery = style.src ? style.scoped ? `&src=${descriptor.id}` : "&src=true" : "";
2477
- const directQuery = asCustomElement ? `&inline` : ``;
2658
+ const directQuery = customElement ? `&inline` : ``;
2478
2659
  const scopedQuery = style.scoped ? `&scoped=${descriptor.id}` : ``;
2479
2660
  const query = `?vue&type=style&index=${i}${srcQuery}${directQuery}${scopedQuery}`;
2480
2661
  const styleRequest = src + query + attrsQuery;
2481
2662
  if (style.module) {
2482
- if (asCustomElement) {
2663
+ if (customElement) {
2483
2664
  throw new Error(
2484
2665
  `<style module> is not supported in custom elements mode.`
2485
2666
  );
@@ -2492,7 +2673,7 @@ async function genStyleCode(descriptor, pluginContext, asCustomElement, attached
2492
2673
  stylesCode += importCode;
2493
2674
  Object.assign(cssModulesMap || (cssModulesMap = {}), nameMap);
2494
2675
  } else {
2495
- if (asCustomElement) {
2676
+ if (customElement) {
2496
2677
  stylesCode += `
2497
2678
  import _style_${i} from ${JSON.stringify(
2498
2679
  styleRequest
@@ -2503,7 +2684,7 @@ import ${JSON.stringify(styleRequest)}`;
2503
2684
  }
2504
2685
  }
2505
2686
  }
2506
- if (asCustomElement) {
2687
+ if (customElement) {
2507
2688
  attachedProps.push([
2508
2689
  `styles`,
2509
2690
  `[${descriptor.styles.map((_, i) => `_style_${i}`).join(",")}]`
@@ -2555,7 +2736,16 @@ async function linkSrcToDescriptor(src, descriptor, pluginContext, scoped) {
2555
2736
  const srcFile = (await pluginContext.resolve(src, descriptor.filename))?.id || src;
2556
2737
  setSrcDescriptor(srcFile.replace(/\?.*$/, ""), descriptor, scoped);
2557
2738
  }
2558
- const ignoreList = ["id", "index", "src", "type", "lang", "module", "scoped"];
2739
+ const ignoreList = [
2740
+ "id",
2741
+ "index",
2742
+ "src",
2743
+ "type",
2744
+ "lang",
2745
+ "module",
2746
+ "scoped",
2747
+ "generic"
2748
+ ];
2559
2749
  function attrsToQuery(attrs, langFallback, forceLangFallback = false) {
2560
2750
  let query = ``;
2561
2751
  for (const name in attrs) {
@@ -2603,6 +2793,8 @@ async function transformStyle(code, descriptor, index, options, pluginContext, f
2603
2793
  return null;
2604
2794
  }
2605
2795
  const map = result.map ? await formatPostcssSourceMap(
2796
+ // version property of result.map is declared as string
2797
+ // but actually it is a number
2606
2798
  result.map,
2607
2799
  filename
2608
2800
  ) : { mappings: "" };
@@ -2613,35 +2805,49 @@ async function transformStyle(code, descriptor, index, options, pluginContext, f
2613
2805
  }
2614
2806
 
2615
2807
  function vuePlugin(rawOptions = {}) {
2616
- const {
2617
- include = /\.vue$/,
2618
- exclude,
2619
- customElement = /\.ce\.vue$/,
2620
- reactivityTransform = false
2621
- } = rawOptions;
2622
- const filter = createFilter(include, exclude);
2623
- const customElementFilter = typeof customElement === "boolean" ? () => customElement : createFilter(customElement);
2624
- const refTransformFilter = reactivityTransform === false ? () => false : reactivityTransform === true ? createFilter(/\.(j|t)sx?$/, /node_modules/) : createFilter(reactivityTransform);
2625
- let options = {
2808
+ const options = shallowRef({
2626
2809
  isProduction: process.env.NODE_ENV === "production",
2627
2810
  compiler: null,
2811
+ // to be set in buildStart
2812
+ include: /\.vue$/,
2813
+ customElement: /\.ce\.vue$/,
2628
2814
  ...rawOptions,
2629
- include,
2630
- exclude,
2631
- customElement,
2632
- reactivityTransform,
2633
2815
  root: process.cwd(),
2634
2816
  sourceMap: true,
2635
2817
  cssDevSourcemap: false,
2636
2818
  devToolsEnabled: process.env.NODE_ENV !== "production"
2637
- };
2819
+ });
2820
+ const filter = computed(
2821
+ () => createFilter(options.value.include, options.value.exclude)
2822
+ );
2823
+ const customElementFilter = computed(
2824
+ () => typeof options.value.customElement === "boolean" ? () => options.value.customElement : createFilter(options.value.customElement)
2825
+ );
2638
2826
  return {
2639
2827
  name: "vite:vue",
2828
+ api: {
2829
+ get options() {
2830
+ return options.value;
2831
+ },
2832
+ set options(value) {
2833
+ options.value = value;
2834
+ },
2835
+ version
2836
+ },
2640
2837
  handleHotUpdate(ctx) {
2641
- if (!filter(ctx.file)) {
2642
- return;
2838
+ if (options.value.compiler.invalidateTypeCache) {
2839
+ options.value.compiler.invalidateTypeCache(ctx.file);
2840
+ }
2841
+ if (typeDepToSFCMap.has(ctx.file)) {
2842
+ return handleTypeDepChange(typeDepToSFCMap.get(ctx.file), ctx);
2843
+ }
2844
+ if (filter.value(ctx.file)) {
2845
+ return handleHotUpdate(
2846
+ ctx,
2847
+ options.value,
2848
+ customElementFilter.value(ctx.file)
2849
+ );
2643
2850
  }
2644
- return handleHotUpdate(ctx, options);
2645
2851
  },
2646
2852
  config(config) {
2647
2853
  return {
@@ -2650,16 +2856,18 @@ function vuePlugin(rawOptions = {}) {
2650
2856
  },
2651
2857
  define: {
2652
2858
  __VUE_OPTIONS_API__: config.define?.__VUE_OPTIONS_API__ ?? true,
2653
- __VUE_PROD_DEVTOOLS__: config.define?.__VUE_PROD_DEVTOOLS__ ?? false
2859
+ __VUE_PROD_DEVTOOLS__: config.define?.__VUE_PROD_DEVTOOLS__ ?? false,
2860
+ __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: config.define?.__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ ?? false
2654
2861
  },
2655
2862
  ssr: {
2863
+ // @ts-ignore -- config.legacy.buildSsrCjsExternalHeuristics will be removed in Vite 5
2656
2864
  external: config.legacy?.buildSsrCjsExternalHeuristics ? ["vue", "@vue/server-renderer"] : []
2657
2865
  }
2658
2866
  };
2659
2867
  },
2660
2868
  configResolved(config) {
2661
- options = {
2662
- ...options,
2869
+ options.value = {
2870
+ ...options.value,
2663
2871
  root: config.root,
2664
2872
  sourceMap: config.command === "build" ? !!config.build.sourcemap : true,
2665
2873
  cssDevSourcemap: config.css?.devSourcemap ?? false,
@@ -2668,10 +2876,15 @@ function vuePlugin(rawOptions = {}) {
2668
2876
  };
2669
2877
  },
2670
2878
  configureServer(server) {
2671
- options.devServer = server;
2879
+ options.value.devServer = server;
2672
2880
  },
2673
2881
  buildStart() {
2674
- options.compiler = options.compiler || resolveCompiler(options.root);
2882
+ const compiler = options.value.compiler = options.value.compiler || resolveCompiler(options.value.root);
2883
+ if (compiler.invalidateTypeCache) {
2884
+ options.value.devServer?.watcher.on("unlink", (file) => {
2885
+ compiler.invalidateTypeCache(file);
2886
+ });
2887
+ }
2675
2888
  },
2676
2889
  async resolveId(id) {
2677
2890
  if (id === EXPORT_HELPER_ID) {
@@ -2691,7 +2904,7 @@ function vuePlugin(rawOptions = {}) {
2691
2904
  if (query.src) {
2692
2905
  return fs.readFileSync(filename, "utf-8");
2693
2906
  }
2694
- const descriptor = getDescriptor(filename, options);
2907
+ const descriptor = getDescriptor(filename, options.value);
2695
2908
  let block;
2696
2909
  if (query.type === "script") {
2697
2910
  block = getResolvedScript(descriptor, ssr);
@@ -2716,34 +2929,35 @@ function vuePlugin(rawOptions = {}) {
2716
2929
  if (query.raw || query.url) {
2717
2930
  return;
2718
2931
  }
2719
- if (!filter(filename) && !query.vue) {
2720
- if (!query.vue && refTransformFilter(filename) && options.compiler.shouldTransformRef(code)) {
2721
- return options.compiler.transformRef(code, {
2722
- filename,
2723
- sourceMap: true
2724
- });
2725
- }
2932
+ if (!filter.value(filename) && !query.vue) {
2726
2933
  return;
2727
2934
  }
2728
2935
  if (!query.vue) {
2729
2936
  return transformMain(
2730
2937
  code,
2731
2938
  filename,
2732
- options,
2939
+ options.value,
2733
2940
  this,
2734
2941
  ssr,
2735
- customElementFilter(filename)
2942
+ customElementFilter.value(filename)
2736
2943
  );
2737
2944
  } else {
2738
- const descriptor = query.src ? getSrcDescriptor(filename, query) : getDescriptor(filename, options);
2945
+ const descriptor = query.src ? getSrcDescriptor(filename, query) || getTempSrcDescriptor(filename, query) : getDescriptor(filename, options.value);
2739
2946
  if (query.type === "template") {
2740
- return transformTemplateAsModule(code, descriptor, options, this, ssr);
2947
+ return transformTemplateAsModule(
2948
+ code,
2949
+ descriptor,
2950
+ options.value,
2951
+ this,
2952
+ ssr,
2953
+ customElementFilter.value(filename)
2954
+ );
2741
2955
  } else if (query.type === "style") {
2742
2956
  return transformStyle(
2743
2957
  code,
2744
2958
  descriptor,
2745
- Number(query.index),
2746
- options,
2959
+ Number(query.index || 0),
2960
+ options.value,
2747
2961
  this,
2748
2962
  filename
2749
2963
  );