@shijiu/jsview 2.1.340-test.0 → 2.1.362-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,16 +1,13 @@
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';
4
3
  import { createRequire } from 'node:module';
5
4
  import path from 'node:path';
6
5
  import { createHash } from 'node:crypto';
7
6
  import require$$0 from 'tty';
8
7
  import require$$1 from 'util';
9
8
 
10
- const version = "5.0.3";
11
-
12
9
  function resolveCompiler(root) {
13
- const compiler = tryResolveCompiler(root) || tryResolveCompiler();
10
+ const compiler = tryRequire("vue/compiler-sfc", root) || tryRequire("vue/compiler-sfc");
14
11
  if (!compiler) {
15
12
  throw new Error(
16
13
  `Failed to resolve vue/compiler-sfc.
@@ -19,12 +16,6 @@ function resolveCompiler(root) {
19
16
  }
20
17
  return compiler;
21
18
  }
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
- }
28
19
  const _require = createRequire(import.meta.url);
29
20
  function tryRequire(id, from) {
30
21
  try {
@@ -58,7 +49,7 @@ function parseVueRequest(id) {
58
49
  }
59
50
 
60
51
  function slash(path) {
61
- const isExtendedLengthPath = path.startsWith('\\\\?\\');
52
+ const isExtendedLengthPath = /^\\\\\?\\/.test(path);
62
53
 
63
54
  if (isExtendedLengthPath) {
64
55
  return path;
@@ -68,43 +59,34 @@ function slash(path) {
68
59
  }
69
60
 
70
61
  const cache = /* @__PURE__ */ new Map();
71
- const hmrCache = /* @__PURE__ */ new Map();
72
62
  const prevCache = /* @__PURE__ */ new Map();
73
- function createDescriptor(filename, source, { root, isProduction, sourceMap, compiler, template }, hmr = false) {
63
+ function createDescriptor(filename, source, { root, isProduction, sourceMap, compiler }) {
74
64
  const { descriptor, errors } = compiler.parse(source, {
75
65
  filename,
76
- sourceMap,
77
- templateParseOptions: template?.compilerOptions
66
+ sourceMap
78
67
  });
79
68
  const normalizedPath = slash(path.normalize(path.relative(root, filename)));
80
69
  descriptor.id = getHash(normalizedPath + (isProduction ? source : ""));
81
- (hmr ? hmrCache : cache).set(filename, descriptor);
70
+ cache.set(filename, descriptor);
82
71
  return { descriptor, errors };
83
72
  }
84
73
  function getPrevDescriptor(filename) {
85
74
  return prevCache.get(filename);
86
75
  }
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
- }
76
+ function setPrevDescriptor(filename, entry) {
77
+ prevCache.set(filename, entry);
94
78
  }
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);
79
+ function getDescriptor(filename, options, createIfNotFound = true) {
80
+ if (cache.has(filename)) {
81
+ return cache.get(filename);
99
82
  }
100
83
  if (createIfNotFound) {
101
84
  const { descriptor, errors } = createDescriptor(
102
85
  filename,
103
- code ?? fs.readFileSync(filename, "utf-8"),
104
- options,
105
- hmr
86
+ fs.readFileSync(filename, "utf-8"),
87
+ options
106
88
  );
107
- if (errors.length && !hmr) {
89
+ if (errors.length) {
108
90
  throw errors[0];
109
91
  }
110
92
  return descriptor;
@@ -116,20 +98,6 @@ function getSrcDescriptor(filename, query) {
116
98
  }
117
99
  return cache.get(filename);
118
100
  }
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
- }
133
101
  function setSrcDescriptor(filename, entry, scoped) {
134
102
  if (scoped) {
135
103
  cache.set(`${filename}?src=${entry.id}`, entry);
@@ -160,15 +128,8 @@ function createRollupError(id, error) {
160
128
  return rollupError;
161
129
  }
162
130
 
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
- );
131
+ async function transformTemplateAsModule(code, descriptor, options, pluginContext, ssr) {
132
+ const result = compile(code, descriptor, options, pluginContext, ssr);
172
133
  let returnCode = result.code;
173
134
  if (options.devServer && options.devServer.config.server.hmr !== false && !ssr && !options.isProduction) {
174
135
  returnCode += `
@@ -181,15 +142,8 @@ import.meta.hot.accept(({ render }) => {
181
142
  map: result.map
182
143
  };
183
144
  }
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
- );
145
+ function transformTemplateInMain(code, descriptor, options, pluginContext, ssr) {
146
+ const result = compile(code, descriptor, options, pluginContext, ssr);
193
147
  return {
194
148
  ...result,
195
149
  code: result.code.replace(
@@ -198,9 +152,8 @@ function transformTemplateInMain(code, descriptor, options, pluginContext, ssr,
198
152
  )
199
153
  };
200
154
  }
201
- function compile(code, descriptor, options, pluginContext, ssr, customElement) {
155
+ function compile(code, descriptor, options, pluginContext, ssr) {
202
156
  const filename = descriptor.filename;
203
- resolveScript(descriptor, options, ssr, customElement);
204
157
  const result = options.compiler.compileTemplate({
205
158
  ...resolveTemplateCompilerOptions(descriptor, options, ssr),
206
159
  source: code
@@ -236,8 +189,7 @@ function resolveTemplateCompilerOptions(descriptor, options, ssr) {
236
189
  if (filename.startsWith(options.root)) {
237
190
  const devBase = options.devServer.config.base;
238
191
  assetUrlOptions = {
239
- base: (options.devServer.config.server?.origin ?? "") + devBase + slash(path.relative(options.root, path.dirname(filename))),
240
- includeAbsolute: !!devBase
192
+ base: (options.devServer.config.server?.origin ?? "") + devBase + slash(path.relative(options.root, path.dirname(filename)))
241
193
  };
242
194
  }
243
195
  } else if (transformAssetUrls !== false) {
@@ -272,7 +224,6 @@ function resolveTemplateCompilerOptions(descriptor, options, ssr) {
272
224
  return {
273
225
  ...options.template,
274
226
  id,
275
- ast: canReuseAST(options.compiler.version) ? descriptor.template?.ast : void 0,
276
227
  filename,
277
228
  scoped: hasScoped,
278
229
  slotted: descriptor.slotted,
@@ -281,7 +232,7 @@ function resolveTemplateCompilerOptions(descriptor, options, ssr) {
281
232
  ssr,
282
233
  ssrCssVars: cssVars,
283
234
  transformAssetUrls,
284
- preprocessLang: block.lang === "html" ? void 0 : block.lang,
235
+ preprocessLang: block.lang,
285
236
  preprocessOptions,
286
237
  compilerOptions: {
287
238
  ...options.template?.compilerOptions,
@@ -292,26 +243,9 @@ function resolveTemplateCompilerOptions(descriptor, options, ssr) {
292
243
  }
293
244
  };
294
245
  }
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
- }
304
246
 
305
247
  const clientCache = /* @__PURE__ */ new WeakMap();
306
248
  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
- }
315
249
  function getResolvedScript(descriptor, ssr) {
316
250
  return (ssr ? ssrCache : clientCache).get(descriptor);
317
251
  }
@@ -321,16 +255,16 @@ function setResolvedScript(descriptor, script, ssr) {
321
255
  function isUseInlineTemplate(descriptor, isProd) {
322
256
  return isProd && !!descriptor.scriptSetup && !descriptor.template?.src;
323
257
  }
324
- const scriptIdentifier = `_sfc_main`;
325
- function resolveScript(descriptor, options, ssr, customElement) {
258
+ function resolveScript(descriptor, options, ssr) {
326
259
  if (!descriptor.script && !descriptor.scriptSetup) {
327
260
  return null;
328
261
  }
262
+ const cacheToUse = ssr ? ssrCache : clientCache;
329
263
  // JsView Added >>>
330
264
  // 1. 解决style内容发生变化,不使用cache,在templete class和style同时发生变化没有被更新问题。
331
265
  // 2. 解决@import的css文件没有被更新问题。
332
- // const cached = getResolvedScript(descriptor, ssr);
333
- let cached = getResolvedScript(descriptor, ssr);
266
+ // const cached = cacheToUse.get(descriptor);
267
+ let cached = cacheToUse.get(descriptor);
334
268
  if (cached && descriptor.filename?.endsWith('.vue') && descriptor.styles) {
335
269
  for (const style of descriptor.styles) {
336
270
  let useCache = false;
@@ -362,42 +296,13 @@ function resolveScript(descriptor, options, ssr, customElement) {
362
296
  id: descriptor.id,
363
297
  isProd: options.isProduction,
364
298
  inlineTemplate: isUseInlineTemplate(descriptor, !options.devServer),
299
+ reactivityTransform: options.reactivityTransform !== false,
365
300
  templateOptions: resolveTemplateCompilerOptions(descriptor, options, ssr),
366
- sourceMap: options.sourceMap,
367
- genDefaultAs: canInlineMain(descriptor, options) ? scriptIdentifier : void 0,
368
- customElement
301
+ sourceMap: options.sourceMap
369
302
  });
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);
303
+ cacheToUse.set(descriptor, resolved);
386
304
  return resolved;
387
305
  }
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
- }
401
306
 
402
307
  const comma = ','.charCodeAt(0);
403
308
  const semicolon = ';'.charCodeAt(0);
@@ -882,7 +787,7 @@ class TraceMap {
882
787
  const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
883
788
  this.version = version;
884
789
  this.file = file;
885
- this.names = names || [];
790
+ this.names = names;
886
791
  this.sourceRoot = sourceRoot;
887
792
  this.sources = sources;
888
793
  this.sourcesContent = sourcesContent;
@@ -1872,8 +1777,8 @@ function requireBrowser () {
1872
1777
  } catch (error) {
1873
1778
  return '[UnexpectedJSONParseError]: ' + error.message;
1874
1779
  }
1875
- };
1876
- } (browser, browser.exports));
1780
+ };
1781
+ } (browser, browser.exports));
1877
1782
  return browser.exports;
1878
1783
  }
1879
1784
 
@@ -2147,8 +2052,8 @@ function requireNode () {
2147
2052
  formatters.O = function (v) {
2148
2053
  this.inspectOpts.colors = this.useColors;
2149
2054
  return util.inspect(v, this.inspectOpts);
2150
- };
2151
- } (node, node.exports));
2055
+ };
2056
+ } (node, node.exports));
2152
2057
  return node.exports;
2153
2058
  }
2154
2059
 
@@ -2157,32 +2062,42 @@ function requireNode () {
2157
2062
  * treat as a browser.
2158
2063
  */
2159
2064
 
2160
- if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
2161
- src.exports = requireBrowser();
2162
- } else {
2163
- src.exports = requireNode();
2164
- }
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));
2165
2072
 
2166
- var srcExports = src.exports;
2167
- const _debug = /*@__PURE__*/getDefaultExportFromCjs(srcExports);
2073
+ const _debug = /*@__PURE__*/getDefaultExportFromCjs(src.exports);
2168
2074
 
2169
2075
  const debug = _debug("vite:hmr");
2170
2076
  const directRequestRE = /(?:\?|&)direct\b/;
2171
- async function handleHotUpdate({ file, modules, read }, options, customElement) {
2172
- const prevDescriptor = getDescriptor(file, options, false, true);
2077
+ async function handleHotUpdate({ file, modules, read, server }, options) {
2078
+ const prevDescriptor = getDescriptor(file, options, false);
2173
2079
  if (!prevDescriptor) {
2174
2080
  return;
2175
2081
  }
2082
+ setPrevDescriptor(file, prevDescriptor);
2176
2083
  const content = await read();
2177
- const { descriptor } = createDescriptor(file, content, options, true);
2084
+ const { descriptor } = createDescriptor(file, content, options);
2178
2085
  let needRerender = false;
2179
2086
  const affectedModules = /* @__PURE__ */ new Set();
2180
- const mainModule = getMainModule(modules);
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];
2181
2090
  const templateModule = modules.find((m) => /type=template/.test(m.url));
2182
- resolveScript(descriptor, options, false, customElement);
2183
2091
  const scriptChanged = hasScriptChanged(prevDescriptor, descriptor);
2184
2092
  if (scriptChanged) {
2185
- affectedModules.add(getScriptModule(modules) || mainModule);
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);
2186
2101
  }
2187
2102
  if (!isEqualBlock(descriptor.template, prevDescriptor.template)) {
2188
2103
  if (!scriptChanged) {
@@ -2226,6 +2141,11 @@ async function handleHotUpdate({ file, modules, read }, options, customElement)
2226
2141
  if (prevStyles.length > nextStyles.length) {
2227
2142
  affectedModules.add(mainModule);
2228
2143
  }
2144
+ // JsView Added >>>
2145
+ // 当Style发生变化时,更新Script,同步CSS to JS
2146
+ // 貌似没有作用,已经被上面修改的一起处理了。
2147
+ // affectedModules.add(mainModule);
2148
+ // JsView Added <<<
2229
2149
  const prevCustoms = prevDescriptor.customBlocks || [];
2230
2150
  const nextCustoms = descriptor.customBlocks || [];
2231
2151
  if (prevCustoms.length !== nextCustoms.length) {
@@ -2262,11 +2182,6 @@ async function handleHotUpdate({ file, modules, read }, options, customElement)
2262
2182
  updateType.push(`style`);
2263
2183
  }
2264
2184
  if (updateType.length) {
2265
- if (file.endsWith(".vue")) {
2266
- invalidateDescriptor(file);
2267
- } else {
2268
- cache.set(file, descriptor);
2269
- }
2270
2185
  debug(`[vue:update(${updateType.join("&")})] ${file}`);
2271
2186
  }
2272
2187
  return [...affectedModules].filter(Boolean);
@@ -2290,62 +2205,11 @@ function isEqualBlock(a, b) {
2290
2205
  function isOnlyTemplateChanged(prev, next) {
2291
2206
  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]));
2292
2207
  }
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
- }
2342
2208
  function hasScriptChanged(prev, next) {
2343
- const prevScript = getResolvedScript(prev, false);
2344
- const nextScript = getResolvedScript(next, false);
2345
- if (!isEqualBlock(prev.script, next.script) && !isEqualAst(prevScript?.scriptAst, nextScript?.scriptAst)) {
2209
+ if (!isEqualBlock(prev.script, next.script)) {
2346
2210
  return true;
2347
2211
  }
2348
- if (!isEqualBlock(prev.scriptSetup, next.scriptSetup) && !isEqualAst(prevScript?.scriptSetupAst, nextScript?.scriptSetupAst)) {
2212
+ if (!isEqualBlock(prev.scriptSetup, next.scriptSetup)) {
2349
2213
  return true;
2350
2214
  }
2351
2215
  const prevResolvedScript = getResolvedScript(prev, false);
@@ -2355,26 +2219,6 @@ function hasScriptChanged(prev, next) {
2355
2219
  }
2356
2220
  return false;
2357
2221
  }
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
- }
2378
2222
 
2379
2223
  const EXPORT_HELPER_ID = "\0plugin-vue:export-helper";
2380
2224
  const helperCode = `
@@ -2387,25 +2231,10 @@ export default (sfc, props) => {
2387
2231
  }
2388
2232
  `;
2389
2233
 
2390
- async function transformMain(code, filename, options, pluginContext, ssr, customElement) {
2234
+ async function transformMain(code, filename, options, pluginContext, ssr, asCustomElement) {
2391
2235
  const { devServer, isProduction, devToolsEnabled } = options;
2392
2236
  const prevDescriptor = getPrevDescriptor(filename);
2393
2237
  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
- }
2409
2238
  if (errors.length) {
2410
2239
  errors.forEach(
2411
2240
  (error) => pluginContext.error(createRollupError(filename, error))
@@ -2418,8 +2247,7 @@ async function transformMain(code, filename, options, pluginContext, ssr, custom
2418
2247
  descriptor,
2419
2248
  options,
2420
2249
  pluginContext,
2421
- ssr,
2422
- customElement
2250
+ ssr
2423
2251
  );
2424
2252
  const hasTemplateImport = descriptor.template && !isUseInlineTemplate(descriptor, !devServer);
2425
2253
  let templateCode = "";
@@ -2429,8 +2257,7 @@ async function transformMain(code, filename, options, pluginContext, ssr, custom
2429
2257
  descriptor,
2430
2258
  options,
2431
2259
  pluginContext,
2432
- ssr,
2433
- customElement
2260
+ ssr
2434
2261
  ));
2435
2262
  }
2436
2263
  if (hasTemplateImport) {
@@ -2445,7 +2272,7 @@ async function transformMain(code, filename, options, pluginContext, ssr, custom
2445
2272
  const stylesCode = await genStyleCode(
2446
2273
  descriptor,
2447
2274
  pluginContext,
2448
- customElement,
2275
+ asCustomElement,
2449
2276
  attachedProps
2450
2277
  );
2451
2278
  const customBlocksCode = await genCustomBlockCode(descriptor, pluginContext);
@@ -2504,12 +2331,9 @@ async function transformMain(code, filename, options, pluginContext, ssr, custom
2504
2331
  if (options.sourceMap) {
2505
2332
  if (scriptMap && templateMap) {
2506
2333
  const gen = fromMap(
2507
- // version property of result.map is declared as string
2508
- // but actually it is `3`
2509
2334
  scriptMap
2510
2335
  );
2511
2336
  const tracer = new TraceMap(
2512
- // same above
2513
2337
  templateMap
2514
2338
  );
2515
2339
  const offset = (scriptCode.match(/\r?\n/g)?.length ?? 0) + 1;
@@ -2567,17 +2391,16 @@ async function transformMain(code, filename, options, pluginContext, ssr, custom
2567
2391
  }
2568
2392
  };
2569
2393
  }
2570
- async function genTemplateCode(descriptor, options, pluginContext, ssr, customElement) {
2394
+ async function genTemplateCode(descriptor, options, pluginContext, ssr) {
2571
2395
  const template = descriptor.template;
2572
2396
  const hasScoped = descriptor.styles.some((style) => style.scoped);
2573
- if ((!template.lang || template.lang === "html") && !template.src) {
2397
+ if (!template.lang && !template.src) {
2574
2398
  return transformTemplateInMain(
2575
2399
  template.content,
2576
2400
  descriptor,
2577
2401
  options,
2578
2402
  pluginContext,
2579
- ssr,
2580
- customElement
2403
+ ssr
2581
2404
  );
2582
2405
  } else {
2583
2406
  if (template.src) {
@@ -2601,23 +2424,19 @@ async function genTemplateCode(descriptor, options, pluginContext, ssr, customEl
2601
2424
  };
2602
2425
  }
2603
2426
  }
2604
- async function genScriptCode(descriptor, options, pluginContext, ssr, customElement) {
2605
- let scriptCode = `const ${scriptIdentifier} = {}`;
2427
+ async function genScriptCode(descriptor, options, pluginContext, ssr) {
2428
+ let scriptCode = `const _sfc_main = {}`;
2606
2429
  let map;
2607
- const script = resolveScript(descriptor, options, ssr, customElement);
2430
+ const script = resolveScript(descriptor, options, ssr);
2608
2431
  if (script) {
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
- }
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
+ );
2621
2440
  map = script.map;
2622
2441
  } else {
2623
2442
  if (script.src) {
@@ -2638,7 +2457,7 @@ export * from ${request}`;
2638
2457
  map
2639
2458
  };
2640
2459
  }
2641
- async function genStyleCode(descriptor, pluginContext, customElement, attachedProps) {
2460
+ async function genStyleCode(descriptor, pluginContext, asCustomElement, attachedProps) {
2642
2461
  let stylesCode = ``;
2643
2462
  let cssModulesMap;
2644
2463
  if (descriptor.styles.length) {
@@ -2655,12 +2474,12 @@ async function genStyleCode(descriptor, pluginContext, customElement, attachedPr
2655
2474
  const src = style.src || descriptor.filename;
2656
2475
  const attrsQuery = attrsToQuery(style.attrs, "css");
2657
2476
  const srcQuery = style.src ? style.scoped ? `&src=${descriptor.id}` : "&src=true" : "";
2658
- const directQuery = customElement ? `&inline` : ``;
2477
+ const directQuery = asCustomElement ? `&inline` : ``;
2659
2478
  const scopedQuery = style.scoped ? `&scoped=${descriptor.id}` : ``;
2660
2479
  const query = `?vue&type=style&index=${i}${srcQuery}${directQuery}${scopedQuery}`;
2661
2480
  const styleRequest = src + query + attrsQuery;
2662
2481
  if (style.module) {
2663
- if (customElement) {
2482
+ if (asCustomElement) {
2664
2483
  throw new Error(
2665
2484
  `<style module> is not supported in custom elements mode.`
2666
2485
  );
@@ -2673,7 +2492,7 @@ async function genStyleCode(descriptor, pluginContext, customElement, attachedPr
2673
2492
  stylesCode += importCode;
2674
2493
  Object.assign(cssModulesMap || (cssModulesMap = {}), nameMap);
2675
2494
  } else {
2676
- if (customElement) {
2495
+ if (asCustomElement) {
2677
2496
  stylesCode += `
2678
2497
  import _style_${i} from ${JSON.stringify(
2679
2498
  styleRequest
@@ -2684,7 +2503,7 @@ import ${JSON.stringify(styleRequest)}`;
2684
2503
  }
2685
2504
  }
2686
2505
  }
2687
- if (customElement) {
2506
+ if (asCustomElement) {
2688
2507
  attachedProps.push([
2689
2508
  `styles`,
2690
2509
  `[${descriptor.styles.map((_, i) => `_style_${i}`).join(",")}]`
@@ -2736,16 +2555,7 @@ async function linkSrcToDescriptor(src, descriptor, pluginContext, scoped) {
2736
2555
  const srcFile = (await pluginContext.resolve(src, descriptor.filename))?.id || src;
2737
2556
  setSrcDescriptor(srcFile.replace(/\?.*$/, ""), descriptor, scoped);
2738
2557
  }
2739
- const ignoreList = [
2740
- "id",
2741
- "index",
2742
- "src",
2743
- "type",
2744
- "lang",
2745
- "module",
2746
- "scoped",
2747
- "generic"
2748
- ];
2558
+ const ignoreList = ["id", "index", "src", "type", "lang", "module", "scoped"];
2749
2559
  function attrsToQuery(attrs, langFallback, forceLangFallback = false) {
2750
2560
  let query = ``;
2751
2561
  for (const name in attrs) {
@@ -2793,8 +2603,6 @@ async function transformStyle(code, descriptor, index, options, pluginContext, f
2793
2603
  return null;
2794
2604
  }
2795
2605
  const map = result.map ? await formatPostcssSourceMap(
2796
- // version property of result.map is declared as string
2797
- // but actually it is a number
2798
2606
  result.map,
2799
2607
  filename
2800
2608
  ) : { mappings: "" };
@@ -2805,49 +2613,35 @@ async function transformStyle(code, descriptor, index, options, pluginContext, f
2805
2613
  }
2806
2614
 
2807
2615
  function vuePlugin(rawOptions = {}) {
2808
- const options = shallowRef({
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 = {
2809
2626
  isProduction: process.env.NODE_ENV === "production",
2810
2627
  compiler: null,
2811
- // to be set in buildStart
2812
- include: /\.vue$/,
2813
- customElement: /\.ce\.vue$/,
2814
2628
  ...rawOptions,
2629
+ include,
2630
+ exclude,
2631
+ customElement,
2632
+ reactivityTransform,
2815
2633
  root: process.cwd(),
2816
2634
  sourceMap: true,
2817
2635
  cssDevSourcemap: false,
2818
2636
  devToolsEnabled: process.env.NODE_ENV !== "production"
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
- );
2637
+ };
2826
2638
  return {
2827
2639
  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
- },
2837
2640
  handleHotUpdate(ctx) {
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
- );
2641
+ if (!filter(ctx.file)) {
2642
+ return;
2850
2643
  }
2644
+ return handleHotUpdate(ctx, options);
2851
2645
  },
2852
2646
  config(config) {
2853
2647
  return {
@@ -2856,18 +2650,16 @@ function vuePlugin(rawOptions = {}) {
2856
2650
  },
2857
2651
  define: {
2858
2652
  __VUE_OPTIONS_API__: config.define?.__VUE_OPTIONS_API__ ?? true,
2859
- __VUE_PROD_DEVTOOLS__: config.define?.__VUE_PROD_DEVTOOLS__ ?? false,
2860
- __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: config.define?.__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ ?? false
2653
+ __VUE_PROD_DEVTOOLS__: config.define?.__VUE_PROD_DEVTOOLS__ ?? false
2861
2654
  },
2862
2655
  ssr: {
2863
- // @ts-ignore -- config.legacy.buildSsrCjsExternalHeuristics will be removed in Vite 5
2864
2656
  external: config.legacy?.buildSsrCjsExternalHeuristics ? ["vue", "@vue/server-renderer"] : []
2865
2657
  }
2866
2658
  };
2867
2659
  },
2868
2660
  configResolved(config) {
2869
- options.value = {
2870
- ...options.value,
2661
+ options = {
2662
+ ...options,
2871
2663
  root: config.root,
2872
2664
  sourceMap: config.command === "build" ? !!config.build.sourcemap : true,
2873
2665
  cssDevSourcemap: config.css?.devSourcemap ?? false,
@@ -2876,15 +2668,10 @@ function vuePlugin(rawOptions = {}) {
2876
2668
  };
2877
2669
  },
2878
2670
  configureServer(server) {
2879
- options.value.devServer = server;
2671
+ options.devServer = server;
2880
2672
  },
2881
2673
  buildStart() {
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
- }
2674
+ options.compiler = options.compiler || resolveCompiler(options.root);
2888
2675
  },
2889
2676
  async resolveId(id) {
2890
2677
  if (id === EXPORT_HELPER_ID) {
@@ -2904,7 +2691,7 @@ function vuePlugin(rawOptions = {}) {
2904
2691
  if (query.src) {
2905
2692
  return fs.readFileSync(filename, "utf-8");
2906
2693
  }
2907
- const descriptor = getDescriptor(filename, options.value);
2694
+ const descriptor = getDescriptor(filename, options);
2908
2695
  let block;
2909
2696
  if (query.type === "script") {
2910
2697
  block = getResolvedScript(descriptor, ssr);
@@ -2929,35 +2716,34 @@ function vuePlugin(rawOptions = {}) {
2929
2716
  if (query.raw || query.url) {
2930
2717
  return;
2931
2718
  }
2932
- if (!filter.value(filename) && !query.vue) {
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
+ }
2933
2726
  return;
2934
2727
  }
2935
2728
  if (!query.vue) {
2936
2729
  return transformMain(
2937
2730
  code,
2938
2731
  filename,
2939
- options.value,
2732
+ options,
2940
2733
  this,
2941
2734
  ssr,
2942
- customElementFilter.value(filename)
2735
+ customElementFilter(filename)
2943
2736
  );
2944
2737
  } else {
2945
- const descriptor = query.src ? getSrcDescriptor(filename, query) || getTempSrcDescriptor(filename, query) : getDescriptor(filename, options.value);
2738
+ const descriptor = query.src ? getSrcDescriptor(filename, query) : getDescriptor(filename, options);
2946
2739
  if (query.type === "template") {
2947
- return transformTemplateAsModule(
2948
- code,
2949
- descriptor,
2950
- options.value,
2951
- this,
2952
- ssr,
2953
- customElementFilter.value(filename)
2954
- );
2740
+ return transformTemplateAsModule(code, descriptor, options, this, ssr);
2955
2741
  } else if (query.type === "style") {
2956
2742
  return transformStyle(
2957
2743
  code,
2958
2744
  descriptor,
2959
- Number(query.index || 0),
2960
- options.value,
2745
+ Number(query.index),
2746
+ options,
2961
2747
  this,
2962
2748
  filename
2963
2749
  );