wip(compiler-ssr): built-in component fallthrough

This commit is contained in:
Evan You
2020-02-06 15:29:02 -05:00
parent 9cfbab0686
commit 3c27bf6133
10 changed files with 129 additions and 61 deletions

View File

@@ -79,6 +79,8 @@ function createCodegenContext(
sourceMap = false,
filename = `template.vue.html`,
scopeId = null,
runtimeGlobalName = `Vue`,
runtimeModuleName = `vue`,
ssr = false
}: CodegenOptions
): CodegenContext {
@@ -88,6 +90,8 @@ function createCodegenContext(
sourceMap,
filename,
scopeId,
runtimeGlobalName,
runtimeModuleName,
ssr,
source: ast.loc.source,
code: ``,
@@ -275,8 +279,18 @@ export function generate(
}
function genFunctionPreamble(ast: RootNode, context: CodegenContext) {
const { ssr, helper, prefixIdentifiers, push, newline } = context
const VueBinding = ssr ? `require("vue")` : `Vue`
const {
ssr,
helper,
prefixIdentifiers,
push,
newline,
runtimeModuleName,
runtimeGlobalName
} = context
const VueBinding = ssr
? `require(${JSON.stringify(runtimeModuleName)})`
: runtimeGlobalName
// Generate const declaration for helpers
// In prefix mode, we place the const declaration at top so it's done
// only once; But if we not prefixing, we place the declaration inside the
@@ -319,7 +333,7 @@ function genModulePreamble(
context: CodegenContext,
genScopeId: boolean
) {
const { push, helper, newline, scopeId } = context
const { push, helper, newline, scopeId, runtimeModuleName } = context
// generate import statements for helpers
if (genScopeId) {
ast.helpers.push(WITH_SCOPE_ID)
@@ -328,7 +342,11 @@ function genModulePreamble(
}
}
if (ast.helpers.length) {
push(`import { ${ast.helpers.map(helper).join(', ')} } from "vue"\n`)
push(
`import { ${ast.helpers.map(helper).join(', ')} } from ${JSON.stringify(
runtimeModuleName
)}\n`
)
}
if (!__BROWSER__ && ast.ssrHelpers && ast.ssrHelpers.length) {
push(

View File

@@ -71,6 +71,9 @@ export interface CodegenOptions {
scopeId?: string | null
// we need to know about this to generate proper preambles
prefixIdentifiers?: boolean
// for specifying where to import helpers
runtimeModuleName?: string
runtimeGlobalName?: string
// generate ssr-specific code?
ssr?: boolean
}

View File

@@ -182,7 +182,8 @@ export const transformElement: NodeTransform = (node, context) => {
export function resolveComponentType(
node: ComponentNode,
context: TransformContext
context: TransformContext,
ssr = false
) {
const { tag } = node
@@ -211,7 +212,9 @@ export function resolveComponentType(
// 2. built-in components (Portal, Transition, KeepAlive, Suspense...)
const builtIn = isCoreComponent(tag) || context.isBuiltInComponent(tag)
if (builtIn) {
context.helper(builtIn)
// built-ins are simply fallthroughs / have special handling during ssr
// no we don't need to import their runtime equivalents
if (!ssr) context.helper(builtIn)
return builtIn
}