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

Commit

Permalink
fix: provide scopeId to template compiler when the component has scop…
Browse files Browse the repository at this point in the history
…ed styles (#337)

Co-authored-by: Rahul Kadyan <hi@znck.me>
  • Loading branch information
przemkow and znck committed May 11, 2020
1 parent cd4af0d commit 1fec2c5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cookbook/ssr/package.json
Expand Up @@ -5,7 +5,7 @@
},
"main": "./dist/MyComponent.js",
"devDependencies": {
"rollup": "^0.59.4",
"rollup": "^1.1.0",
"rollup-plugin-vue": "link:../.."
}
}
8 changes: 7 additions & 1 deletion cookbook/ssr/src/MyComponent.vue
@@ -1,5 +1,7 @@
<template>
<h1>Hello {{ name }}</h1>
<div class="component-root-node">
<h1>Hello {{ name }}</h1>
</div>
</template>

<script>
Expand All @@ -11,6 +13,10 @@ export default {
</script>

<style scoped>
.component-root-node {
background: blue;
}
h1 {
color: red;
}
Expand Down
36 changes: 35 additions & 1 deletion src/index.ts
Expand Up @@ -22,6 +22,7 @@ import { parse, SFCDescriptor, SFCBlock } from '@vue/component-compiler-utils'
import debug from 'debug'
import {
VueTemplateCompiler,
VueTemplateCompilerOptions,
VueTemplateCompilerParseOptions
} from '@vue/component-compiler-utils/dist/types'

Expand Down Expand Up @@ -159,6 +160,20 @@ export interface VuePluginOptions {

beforeAssemble?(descriptor: DescriptorCompileResult): DescriptorCompileResult
}

// Official VueTemplateCompilerOptions does not expose scopeId as a part of public API
// ScopeId is required to correctly compile Vue template with SSR optimization.
interface TemplateOptionsRollup extends TemplateOptions {
compilerOptions: VueTemplateCompilerOptions & {
scopeId?: string
}
}

interface VueCompilerOptions {
script?: ScriptOptions | undefined;
style?: StyleOptions | undefined;
template?: TemplateOptionsRollup | undefined;
}
/**
* Rollup plugin for handling .vue files.
*/
Expand Down Expand Up @@ -242,11 +257,25 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
opts.template.isProduction = isProduction
}

const compiler = createDefaultCompiler(opts)
const descriptors = new Map<string, SFCDescriptor>()

if (opts.css === false) d('Running in CSS extract mode')

const getCompiler = ({ scopeId }: { scopeId?: string}) => {
const options: VueCompilerOptions = { ...opts }

options.template = {
...options.template!,
compilerOptions: {
...(options.template!.compilerOptions
? options.template!.compilerOptions
: {}),
scopeId: scopeId
}
}

return createDefaultCompiler(options)
}
function prependStyle(
id: string,
lang: string,
Expand Down Expand Up @@ -344,6 +373,11 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
? hash(path.basename(filename) + source)
: hash(filename + source))

const hasScopedStyles = descriptor.styles.some(style => !!style.scoped)
const compiler = getCompiler({
scopeId: hasScopedStyles ? scopeId : undefined
})

const styles = await Promise.all(
descriptor.styles.map(async style => {
if (style.content) {
Expand Down

0 comments on commit 1fec2c5

Please sign in to comment.