wip(ssr): handle <textarea> with dynamic key v-bind

This commit is contained in:
Evan You
2020-02-04 22:49:47 -05:00
parent 1958314976
commit 8da6df7235
9 changed files with 239 additions and 13 deletions

View File

@@ -50,7 +50,8 @@ export const enum NodeTypes {
// ssr codegen
JS_BLOCK_STATEMENT,
JS_TEMPLATE_LITERAL,
JS_IF_STATEMENT
JS_IF_STATEMENT,
JS_ASSIGNMENT_EXPRESSION
}
export const enum ElementTypes {
@@ -102,8 +103,9 @@ export interface RootNode extends Node {
hoists: JSChildNode[]
imports: ImportItem[]
cached: number
codegenNode?: TemplateChildNode | JSChildNode | BlockStatement | undefined
temps: number
ssrHelpers?: symbol[]
codegenNode?: TemplateChildNode | JSChildNode | BlockStatement | undefined
}
export type ElementNode =
@@ -255,6 +257,7 @@ export type JSChildNode =
| ConditionalExpression
| SequenceExpression
| CacheExpression
| AssignmentExpression
export interface CallExpression extends Node {
type: NodeTypes.JS_CALL_EXPRESSION
@@ -335,6 +338,12 @@ export interface IfStatement extends Node {
alternate: IfStatement | BlockStatement | undefined
}
export interface AssignmentExpression extends Node {
type: NodeTypes.JS_ASSIGNMENT_EXPRESSION
left: SimpleExpressionNode
right: JSChildNode
}
// Codegen Node Types ----------------------------------------------------------
// createVNode(...)
@@ -709,3 +718,15 @@ export function createIfStatement(
loc: locStub
}
}
export function createAssignmentExpression(
left: AssignmentExpression['left'],
right: AssignmentExpression['right']
): AssignmentExpression {
return {
type: NodeTypes.JS_ASSIGNMENT_EXPRESSION,
left,
right,
loc: locStub
}
}

View File

@@ -21,7 +21,8 @@ import {
locStub,
SSRCodegenNode,
TemplateLiteral,
IfStatement
IfStatement,
AssignmentExpression
} from './ast'
import { SourceMapGenerator, RawSourceMap } from 'source-map'
import {
@@ -232,7 +233,14 @@ export function generate(
if (ast.directives.length) {
genAssets(ast.directives, 'directive', context)
}
if (ast.components.length || ast.directives.length) {
if (ast.temps > 0) {
push(`let `)
for (let i = 0; i < ast.temps; i++) {
push(`${i > 0 ? `, ` : ``}_temp${i}`)
}
newline()
}
if (ast.components.length || ast.directives.length || ast.temps) {
newline()
}
@@ -520,6 +528,9 @@ function genNode(node: CodegenNode | symbol | string, context: CodegenContext) {
case NodeTypes.JS_IF_STATEMENT:
!__BROWSER__ && genIfStatement(node, context)
break
case NodeTypes.JS_ASSIGNMENT_EXPRESSION:
!__BROWSER__ && genAssignmentExpression(node, context)
break
/* istanbul ignore next */
default:
@@ -790,3 +801,12 @@ function genIfStatement(node: IfStatement, context: CodegenContext) {
}
}
}
function genAssignmentExpression(
node: AssignmentExpression,
context: CodegenContext
) {
genNode(node.left, context)
context.push(` = `)
genNode(node.right, context)
}

View File

@@ -82,6 +82,7 @@ export function baseParse(
hoists: [],
imports: [],
cached: 0,
temps: 0,
codegenNode: undefined,
loc: getSelection(context, start)
}

View File

@@ -83,6 +83,7 @@ export interface TransformContext extends Required<TransformOptions> {
components: Set<string>
directives: Set<string>
hoists: JSChildNode[]
temps: number
imports: Set<ImportItem>
cached: number
identifiers: { [name: string]: number | undefined }
@@ -136,6 +137,7 @@ function createTransformContext(
components: new Set(),
directives: new Set(),
hoists: [],
temps: 0,
imports: new Set(),
cached: 0,
identifiers: {},
@@ -267,6 +269,7 @@ export function transform(root: RootNode, options: TransformOptions) {
root.directives = [...context.directives]
root.imports = [...context.imports]
root.hoists = context.hoists
root.temps = context.temps
root.cached = context.cached
}