@@ -22,6 +22,7 @@ import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils'
2222import debug from 'debug'
2323import {
2424 VueTemplateCompiler ,
25+ VueTemplateCompilerOptions ,
2526 VueTemplateCompilerParseOptions
2627} from '@vue/component-compiler-utils/dist/types'
2728
@@ -159,6 +160,20 @@ export interface VuePluginOptions {
159160
160161 beforeAssemble ?( descriptor : DescriptorCompileResult ) : DescriptorCompileResult
161162}
163+
164+ // Official VueTemplateCompilerOptions does not expose scopeId as a part of public API
165+ // ScopeId is required to correctly compile Vue template with SSR optimization.
166+ interface TemplateOptionsRollup extends TemplateOptions {
167+ compilerOptions : VueTemplateCompilerOptions & {
168+ scopeId ?: string
169+ }
170+ }
171+
172+ interface VueCompilerOptions {
173+ script ?: ScriptOptions | undefined ;
174+ style ?: StyleOptions | undefined ;
175+ template ?: TemplateOptionsRollup | undefined ;
176+ }
162177/**
163178 * Rollup plugin for handling .vue files.
164179 */
@@ -242,11 +257,25 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
242257 opts . template . isProduction = isProduction
243258 }
244259
245- const compiler = createDefaultCompiler ( opts )
246260 const descriptors = new Map < string , SFCDescriptor > ( )
247261
248262 if ( opts . css === false ) d ( 'Running in CSS extract mode' )
249263
264+ const getCompiler = ( { scopeId } : { scopeId ?: string } ) => {
265+ const options : VueCompilerOptions = { ...opts }
266+
267+ options . template = {
268+ ...options . template ! ,
269+ compilerOptions : {
270+ ...( options . template ! . compilerOptions
271+ ? options . template ! . compilerOptions
272+ : { } ) ,
273+ scopeId : scopeId
274+ }
275+ }
276+
277+ return createDefaultCompiler ( options )
278+ }
250279 function prependStyle (
251280 id : string ,
252281 lang : string ,
@@ -344,6 +373,11 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
344373 ? hash ( path . basename ( filename ) + source )
345374 : hash ( filename + source ) )
346375
376+ const hasScopedStyles = descriptor . styles . some ( style => ! ! style . scoped )
377+ const compiler = getCompiler ( {
378+ scopeId : hasScopedStyles ? scopeId : undefined
379+ } )
380+
347381 const styles = await Promise . all (
348382 descriptor . styles . map ( async style => {
349383 if ( style . content ) {
0 commit comments