Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 1fec2c5

Browse files
przemkowznck
andauthored
fix: provide scopeId to template compiler when the component has scoped styles (#337)
Co-authored-by: Rahul Kadyan <hi@znck.me>
1 parent cd4af0d commit 1fec2c5

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

cookbook/ssr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"main": "./dist/MyComponent.js",
77
"devDependencies": {
8-
"rollup": "^0.59.4",
8+
"rollup": "^1.1.0",
99
"rollup-plugin-vue": "link:../.."
1010
}
1111
}

cookbook/ssr/src/MyComponent.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<template>
2-
<h1>Hello {{ name }}</h1>
2+
<div class="component-root-node">
3+
<h1>Hello {{ name }}</h1>
4+
</div>
35
</template>
46

57
<script>
@@ -11,6 +13,10 @@ export default {
1113
</script>
1214

1315
<style scoped>
16+
.component-root-node {
17+
background: blue;
18+
}
19+
1420
h1 {
1521
color: red;
1622
}

src/index.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils'
2222
import debug from 'debug'
2323
import {
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

Comments
 (0)