@@ -227,6 +227,7 @@ async function genTemplateCode(
227
227
ssr : boolean
228
228
) {
229
229
const template = descriptor . template !
230
+ const hasScoped = descriptor . styles . some ( ( style ) => style . scoped )
230
231
231
232
// If the template is not using pre-processor AND is not using external src,
232
233
// compile and inline it directly in the main module. When served in vite this
@@ -241,12 +242,22 @@ async function genTemplateCode(
241
242
)
242
243
} else {
243
244
if ( template . src ) {
244
- await linkSrcToDescriptor ( template . src , descriptor , pluginContext )
245
+ await linkSrcToDescriptor (
246
+ template . src ,
247
+ descriptor ,
248
+ pluginContext ,
249
+ hasScoped
250
+ )
245
251
}
246
252
const src = template . src || descriptor . filename
247
- const srcQuery = template . src ? `&src=${ descriptor . id } ` : ``
253
+ const srcQuery = template . src
254
+ ? hasScoped
255
+ ? `&src=${ descriptor . id } `
256
+ : '&src=true'
257
+ : ''
258
+ const scopedQuery = hasScoped ? `&scoped=true` : ``
248
259
const attrsQuery = attrsToQuery ( template . attrs , 'js' , true )
249
- const query = `?vue&type=template${ srcQuery } ${ attrsQuery } `
260
+ const query = `?vue&type=template${ srcQuery } ${ scopedQuery } ${ attrsQuery } `
250
261
const request = JSON . stringify ( src + query )
251
262
const renderFnName = ssr ? 'ssrRender' : 'render'
252
263
return {
@@ -284,12 +295,12 @@ async function genScriptCode(
284
295
map = script . map
285
296
} else {
286
297
if ( script . src ) {
287
- await linkSrcToDescriptor ( script . src , descriptor , pluginContext )
298
+ await linkSrcToDescriptor ( script . src , descriptor , pluginContext , false )
288
299
}
289
300
const src = script . src || descriptor . filename
290
301
const langFallback = ( script . src && path . extname ( src ) . slice ( 1 ) ) || 'js'
291
302
const attrsQuery = attrsToQuery ( script . attrs , langFallback )
292
- const srcQuery = script . src ? `&src=${ descriptor . id } ` : ``
303
+ const srcQuery = script . src ? `&src=true ` : ``
293
304
const query = `?vue&type=script${ srcQuery } ${ attrsQuery } `
294
305
const request = JSON . stringify ( src + query )
295
306
scriptCode =
@@ -314,13 +325,22 @@ async function genStyleCode(
314
325
for ( let i = 0 ; i < descriptor . styles . length ; i ++ ) {
315
326
const style = descriptor . styles [ i ]
316
327
if ( style . src ) {
317
- await linkSrcToDescriptor ( style . src , descriptor , pluginContext )
328
+ await linkSrcToDescriptor (
329
+ style . src ,
330
+ descriptor ,
331
+ pluginContext ,
332
+ style . scoped
333
+ )
318
334
}
319
335
const src = style . src || descriptor . filename
320
336
// do not include module in default query, since we use it to indicate
321
337
// that the module needs to export the modules json
322
338
const attrsQuery = attrsToQuery ( style . attrs , 'css' )
323
- const srcQuery = style . src ? `&src=${ descriptor . id } ` : ``
339
+ const srcQuery = style . src
340
+ ? style . scoped
341
+ ? `&src=${ descriptor . id } `
342
+ : '&src=true'
343
+ : ''
324
344
const directQuery = asCustomElement ? `&inline` : ``
325
345
const query = `?vue&type=style&index=${ i } ${ srcQuery } ${ directQuery } `
326
346
const styleRequest = src + query + attrsQuery
@@ -390,11 +410,11 @@ async function genCustomBlockCode(
390
410
for ( let index = 0 ; index < descriptor . customBlocks . length ; index ++ ) {
391
411
const block = descriptor . customBlocks [ index ]
392
412
if ( block . src ) {
393
- await linkSrcToDescriptor ( block . src , descriptor , pluginContext )
413
+ await linkSrcToDescriptor ( block . src , descriptor , pluginContext , false )
394
414
}
395
415
const src = block . src || descriptor . filename
396
416
const attrsQuery = attrsToQuery ( block . attrs , block . type )
397
- const srcQuery = block . src ? `&src` : ``
417
+ const srcQuery = block . src ? `&src=true ` : ``
398
418
const query = `?vue&type=${ block . type } &index=${ index } ${ srcQuery } ${ attrsQuery } `
399
419
const request = JSON . stringify ( src + query )
400
420
code += `import block${ index } from ${ request } \n`
@@ -411,13 +431,14 @@ async function genCustomBlockCode(
411
431
async function linkSrcToDescriptor (
412
432
src : string ,
413
433
descriptor : SFCDescriptor ,
414
- pluginContext : PluginContext
434
+ pluginContext : PluginContext ,
435
+ scoped ?: boolean
415
436
) {
416
437
const srcFile =
417
438
( await pluginContext . resolve ( src , descriptor . filename ) ) ?. id || src
418
439
// #1812 if the src points to a dep file, the resolved id may contain a
419
440
// version query.
420
- setSrcDescriptor ( srcFile . replace ( / \? .* $ / , '' ) , descriptor )
441
+ setSrcDescriptor ( srcFile . replace ( / \? .* $ / , '' ) , descriptor , scoped )
421
442
}
422
443
423
444
// these are built-in query parameters so should be ignored
0 commit comments