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

fix: generate compiler per component to pass information about scopeId #337

Merged
merged 2 commits into from May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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