wip(ssr): element scopeId

This commit is contained in:
Evan You 2020-02-06 16:51:26 -05:00
parent 4cc39e14a2
commit 797cc18967
5 changed files with 49 additions and 21 deletions

View File

@ -44,7 +44,8 @@ import {
CREATE_TEXT, CREATE_TEXT,
PUSH_SCOPE_ID, PUSH_SCOPE_ID,
POP_SCOPE_ID, POP_SCOPE_ID,
WITH_SCOPE_ID WITH_SCOPE_ID,
CREATE_BLOCK
} from './runtimeHelpers' } from './runtimeHelpers'
import { ImportItem } from './transform' import { ImportItem } from './transform'
@ -333,14 +334,28 @@ function genModulePreamble(
context: CodegenContext, context: CodegenContext,
genScopeId: boolean genScopeId: boolean
) { ) {
const { push, helper, newline, scopeId, runtimeModuleName } = context const { push, helper, newline, scopeId, runtimeModuleName, ssr } = context
// generate import statements for helpers
if (genScopeId) { if (!__BROWSER__) {
ast.helpers.push(WITH_SCOPE_ID) // in ssr mode, `withId` helper is only needed if the template contains
if (ast.hoists.length) { // de-optimized component slots (which uses the createVNode helper)
ast.helpers.push(PUSH_SCOPE_ID, POP_SCOPE_ID) if (
ssr &&
!(
ast.helpers.includes(CREATE_VNODE) || ast.helpers.includes(CREATE_BLOCK)
)
) {
genScopeId = false
}
if (genScopeId) {
ast.helpers.push(WITH_SCOPE_ID)
if (ast.hoists.length) {
ast.helpers.push(PUSH_SCOPE_ID, POP_SCOPE_ID)
}
} }
} }
// generate import statements for helpers
if (ast.helpers.length) { if (ast.helpers.length) {
push( push(
`import { ${ast.helpers.map(helper).join(', ')} } from ${JSON.stringify( `import { ${ast.helpers.map(helper).join(', ')} } from ${JSON.stringify(
@ -348,21 +363,25 @@ function genModulePreamble(
)}\n` )}\n`
) )
} }
if (!__BROWSER__ && ast.ssrHelpers && ast.ssrHelpers.length) {
push( if (!__BROWSER__) {
`import { ${ast.ssrHelpers if (ast.ssrHelpers && ast.ssrHelpers.length) {
.map(helper) push(
.join(', ')} } from "@vue/server-renderer"\n` `import { ${ast.ssrHelpers
) .map(helper)
} .join(', ')} } from "@vue/server-renderer"\n`
if (ast.imports.length) { )
genImports(ast.imports, context) }
newline() if (ast.imports.length) {
} genImports(ast.imports, context)
if (genScopeId) { newline()
push(`const withId = ${helper(WITH_SCOPE_ID)}("${scopeId}")`) }
newline() if (genScopeId) {
push(`const withId = ${helper(WITH_SCOPE_ID)}("${scopeId}")`)
newline()
}
} }
genHoists(ast.hoists, context) genHoists(ast.hoists, context)
newline() newline()
push(`export `) push(`export `)

View File

@ -49,6 +49,8 @@ export interface TransformOptions {
// analysis to determine if a handler is safe to cache. // analysis to determine if a handler is safe to cache.
// - Default: false // - Default: false
cacheHandlers?: boolean cacheHandlers?: boolean
// SFC scoped styles ID
scopeId?: string | null
ssr?: boolean ssr?: boolean
onError?: (error: CompilerError) => void onError?: (error: CompilerError) => void
} }

View File

@ -118,6 +118,7 @@ function createTransformContext(
nodeTransforms = [], nodeTransforms = [],
directiveTransforms = {}, directiveTransforms = {},
isBuiltInComponent = NOOP, isBuiltInComponent = NOOP,
scopeId = null,
ssr = false, ssr = false,
onError = defaultOnError onError = defaultOnError
}: TransformOptions }: TransformOptions
@ -130,6 +131,7 @@ function createTransformContext(
nodeTransforms, nodeTransforms,
directiveTransforms, directiveTransforms,
isBuiltInComponent, isBuiltInComponent,
scopeId,
ssr, ssr,
onError, onError,

View File

@ -31,6 +31,7 @@ export function compile(
// apply DOM-specific parsing options // apply DOM-specific parsing options
...parserOptions, ...parserOptions,
ssr: true, ssr: true,
scopeId: options.mode === 'function' ? null : options.scopeId,
// always prefix since compiler-ssr doesn't have size concern // always prefix since compiler-ssr doesn't have size concern
prefixIdentifiers: true, prefixIdentifiers: true,
// disalbe optimizations that are unnecessary for ssr // disalbe optimizations that are unnecessary for ssr

View File

@ -246,6 +246,10 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
removeStaticBinding(openTag, 'class') removeStaticBinding(openTag, 'class')
} }
if (context.scopeId) {
openTag.push(` ${context.scopeId}`)
}
openTag.push(`>`) openTag.push(`>`)
if (rawChildren) { if (rawChildren) {
openTag.push(rawChildren) openTag.push(rawChildren)