@@ -22,6 +22,7 @@ import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils'
22
22
import debug from 'debug'
23
23
import {
24
24
VueTemplateCompiler ,
25
+ VueTemplateCompilerOptions ,
25
26
VueTemplateCompilerParseOptions
26
27
} from '@vue/component-compiler-utils/dist/types'
27
28
@@ -159,6 +160,20 @@ export interface VuePluginOptions {
159
160
160
161
beforeAssemble ?( descriptor : DescriptorCompileResult ) : DescriptorCompileResult
161
162
}
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
+ }
162
177
/**
163
178
* Rollup plugin for handling .vue files.
164
179
*/
@@ -242,11 +257,25 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
242
257
opts . template . isProduction = isProduction
243
258
}
244
259
245
- const compiler = createDefaultCompiler ( opts )
246
260
const descriptors = new Map < string , SFCDescriptor > ( )
247
261
248
262
if ( opts . css === false ) d ( 'Running in CSS extract mode' )
249
263
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
+ }
250
279
function prependStyle (
251
280
id : string ,
252
281
lang : string ,
@@ -344,6 +373,11 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
344
373
? hash ( path . basename ( filename ) + source )
345
374
: hash ( filename + source ) )
346
375
376
+ const hasScopedStyles = descriptor . styles . some ( style => ! ! style . scoped )
377
+ const compiler = getCompiler ( {
378
+ scopeId : hasScopedStyles ? scopeId : undefined
379
+ } )
380
+
347
381
const styles = await Promise . all (
348
382
descriptor . styles . map ( async style => {
349
383
if ( style . content ) {
0 commit comments