Skip to content

Commit 41b5d76

Browse files
authored
fix(plugin-vue): multiple vue files using the same src file (fix #5925, #5447) (#5994)
1 parent dbc0261 commit 41b5d76

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

packages/plugin-vue/src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '@vue/compiler-sfc'
1010
import { compiler } from './compiler'
1111
import { parseVueRequest } from './utils/query'
12-
import { getDescriptor } from './utils/descriptorCache'
12+
import { getDescriptor, getSrcDescriptor } from './utils/descriptorCache'
1313
import { getResolvedScript } from './script'
1414
import { transformMain } from './main'
1515
import { handleHotUpdate } from './handleHotUpdate'
@@ -223,7 +223,10 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
223223
)
224224
} else {
225225
// sub block request
226-
const descriptor = getDescriptor(filename, options)!
226+
const descriptor = query.src
227+
? getSrcDescriptor(filename, query)!
228+
: getDescriptor(filename, options)!
229+
227230
if (query.type === 'template') {
228231
return transformTemplateAsModule(code, descriptor, options, this, ssr)
229232
} else if (query.type === 'style') {

packages/plugin-vue/src/main.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ResolvedOptions } from '.'
66
import {
77
createDescriptor,
88
getPrevDescriptor,
9-
setDescriptor
9+
setSrcDescriptor
1010
} from './utils/descriptorCache'
1111
import { PluginContext, SourceMap, TransformPluginContext } from 'rollup'
1212
import { normalizePath } from '@rollup/pluginutils'
@@ -237,7 +237,7 @@ async function genTemplateCode(
237237
await linkSrcToDescriptor(template.src, descriptor, pluginContext)
238238
}
239239
const src = template.src || descriptor.filename
240-
const srcQuery = template.src ? `&src` : ``
240+
const srcQuery = template.src ? `&src=${descriptor.id}` : ``
241241
const attrsQuery = attrsToQuery(template.attrs, 'js', true)
242242
const query = `?vue&type=template${srcQuery}${attrsQuery}`
243243
const request = JSON.stringify(src + query)
@@ -279,7 +279,7 @@ async function genScriptCode(
279279
const src = script.src || descriptor.filename
280280
const langFallback = (script.src && path.extname(src).slice(1)) || 'js'
281281
const attrsQuery = attrsToQuery(script.attrs, langFallback)
282-
const srcQuery = script.src ? `&src` : ``
282+
const srcQuery = script.src ? `&src=${descriptor.id}` : ``
283283
const query = `?vue&type=script${srcQuery}${attrsQuery}`
284284
const request = JSON.stringify(src + query)
285285
scriptCode =
@@ -310,7 +310,7 @@ async function genStyleCode(
310310
// do not include module in default query, since we use it to indicate
311311
// that the module needs to export the modules json
312312
const attrsQuery = attrsToQuery(style.attrs, 'css')
313-
const srcQuery = style.src ? `&src` : ``
313+
const srcQuery = style.src ? `&src=${descriptor.id}` : ``
314314
const directQuery = asCustomElement ? `&inline` : ``
315315
const query = `?vue&type=style&index=${i}${srcQuery}${directQuery}`
316316
const styleRequest = src + query + attrsQuery
@@ -397,7 +397,7 @@ async function linkSrcToDescriptor(
397397
(await pluginContext.resolve(src, descriptor.filename))?.id || src
398398
// #1812 if the src points to a dep file, the resolved id may contain a
399399
// version query.
400-
setDescriptor(srcFile.replace(/\?.*$/, ''), descriptor)
400+
setSrcDescriptor(srcFile.replace(/\?.*$/, ''), descriptor)
401401
}
402402

403403
// these are built-in query parameters so should be ignored

packages/plugin-vue/src/utils/descriptorCache.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path'
33
import slash from 'slash'
44
import hash from 'hash-sum'
55
import { CompilerError, SFCDescriptor } from '@vue/compiler-sfc'
6-
import { ResolvedOptions } from '..'
6+
import { ResolvedOptions, VueQuery } from '..'
77
import { compiler } from '../compiler'
88

99
// node_modules/@vue/compiler-sfc/dist/compiler-sfc.d.ts SFCParseResult should be exported so it can be re-used
@@ -66,6 +66,15 @@ export function getDescriptor(
6666
}
6767
}
6868

69-
export function setDescriptor(filename: string, entry: SFCDescriptor): void {
70-
cache.set(filename, entry)
69+
export function getSrcDescriptor(
70+
filename: string,
71+
query: VueQuery
72+
): SFCDescriptor {
73+
return cache.get(`${filename}?src=${query.src}`)!
74+
}
75+
76+
export function setSrcDescriptor(filename: string, entry: SFCDescriptor): void {
77+
// if multiple Vue files use the same src file, they will be overwritten
78+
// should use other key
79+
cache.set(`${filename}?src=${entry.id}`, entry)
7180
}

packages/plugin-vue/src/utils/query.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import qs from 'querystring'
22

33
export interface VueQuery {
44
vue?: boolean
5-
src?: boolean
5+
src?: string
66
type?: 'script' | 'template' | 'style' | 'custom'
77
index?: number
88
lang?: string
@@ -18,9 +18,6 @@ export function parseVueRequest(id: string): {
1818
if (query.vue != null) {
1919
query.vue = true
2020
}
21-
if (query.src != null) {
22-
query.src = true
23-
}
2421
if (query.index != null) {
2522
query.index = Number(query.index)
2623
}

0 commit comments

Comments
 (0)