2020-02-02 13:05:27 +08:00
|
|
|
import {
|
|
|
|
CodegenResult,
|
|
|
|
baseParse,
|
|
|
|
parserOptions,
|
|
|
|
transform,
|
|
|
|
generate,
|
|
|
|
CompilerOptions,
|
|
|
|
transformExpression,
|
|
|
|
trackVForSlotScopes,
|
2020-02-05 01:20:51 +08:00
|
|
|
trackSlotScopes,
|
2020-02-05 05:47:12 +08:00
|
|
|
noopDirectiveTransform,
|
2020-02-05 07:37:23 +08:00
|
|
|
transformBind,
|
|
|
|
transformStyle
|
2020-02-02 13:05:27 +08:00
|
|
|
} from '@vue/compiler-dom'
|
|
|
|
import { ssrCodegenTransform } from './ssrCodegenTransform'
|
|
|
|
import { ssrTransformElement } from './transforms/ssrTransformElement'
|
|
|
|
import { ssrTransformComponent } from './transforms/ssrTransformComponent'
|
|
|
|
import { ssrTransformSlotOutlet } from './transforms/ssrTransformSlotOutlet'
|
2020-02-05 01:20:51 +08:00
|
|
|
import { ssrTransformIf } from './transforms/ssrVIf'
|
|
|
|
import { ssrTransformFor } from './transforms/ssrVFor'
|
2020-02-05 05:47:12 +08:00
|
|
|
import { ssrTransformModel } from './transforms/ssrVModel'
|
|
|
|
import { ssrTransformShow } from './transforms/ssrVShow'
|
2020-02-02 13:05:27 +08:00
|
|
|
|
|
|
|
export function compile(
|
|
|
|
template: string,
|
2020-02-05 01:20:51 +08:00
|
|
|
options: CompilerOptions = {}
|
2020-02-02 13:05:27 +08:00
|
|
|
): CodegenResult {
|
2020-02-03 11:28:54 +08:00
|
|
|
options = {
|
2020-02-04 06:47:06 +08:00
|
|
|
...options,
|
|
|
|
// apply DOM-specific parsing options
|
2020-02-02 13:05:27 +08:00
|
|
|
...parserOptions,
|
2020-02-04 06:47:06 +08:00
|
|
|
ssr: true,
|
|
|
|
// always prefix since compiler-ssr doesn't have size concern
|
|
|
|
prefixIdentifiers: true,
|
|
|
|
// disalbe optimizations that are unnecessary for ssr
|
|
|
|
cacheHandlers: false,
|
|
|
|
hoistStatic: false
|
2020-02-03 11:28:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ast = baseParse(template, options)
|
2020-02-02 13:05:27 +08:00
|
|
|
|
|
|
|
transform(ast, {
|
|
|
|
...options,
|
|
|
|
nodeTransforms: [
|
|
|
|
ssrTransformIf,
|
|
|
|
ssrTransformFor,
|
|
|
|
trackVForSlotScopes,
|
|
|
|
transformExpression,
|
|
|
|
ssrTransformSlotOutlet,
|
|
|
|
ssrTransformElement,
|
|
|
|
ssrTransformComponent,
|
|
|
|
trackSlotScopes,
|
2020-02-05 07:37:23 +08:00
|
|
|
transformStyle,
|
2020-02-02 13:05:27 +08:00
|
|
|
...(options.nodeTransforms || []) // user transforms
|
|
|
|
],
|
2020-02-05 07:37:23 +08:00
|
|
|
directiveTransforms: {
|
|
|
|
// reusing core v-bind
|
|
|
|
bind: transformBind,
|
|
|
|
// model and show has dedicated SSR handling
|
2020-02-05 05:47:12 +08:00
|
|
|
model: ssrTransformModel,
|
|
|
|
show: ssrTransformShow,
|
2020-02-05 07:37:23 +08:00
|
|
|
// the following are ignored during SSR
|
|
|
|
on: noopDirectiveTransform,
|
|
|
|
cloak: noopDirectiveTransform,
|
|
|
|
once: noopDirectiveTransform,
|
|
|
|
...(options.directiveTransforms || {}) // user transforms
|
2020-02-02 13:05:27 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// traverse the template AST and convert into SSR codegen AST
|
|
|
|
// by replacing ast.codegenNode.
|
2020-02-03 11:28:54 +08:00
|
|
|
ssrCodegenTransform(ast, options)
|
2020-02-02 13:05:27 +08:00
|
|
|
|
2020-02-04 06:47:06 +08:00
|
|
|
return generate(ast, options)
|
2020-01-22 23:39:09 +08:00
|
|
|
}
|