2020-02-02 13:05:27 +08:00
|
|
|
import {
|
|
|
|
CodegenResult,
|
|
|
|
baseParse,
|
|
|
|
parserOptions,
|
|
|
|
transform,
|
|
|
|
generate,
|
|
|
|
CompilerOptions,
|
|
|
|
transformExpression,
|
|
|
|
trackVForSlotScopes,
|
|
|
|
trackSlotScopes
|
|
|
|
} from '@vue/compiler-dom'
|
|
|
|
import { ssrCodegenTransform } from './ssrCodegenTransform'
|
|
|
|
import { ssrTransformIf } from './transforms/ssrVIf'
|
|
|
|
import { ssrTransformFor } from './transforms/ssrVFor'
|
|
|
|
import { ssrTransformElement } from './transforms/ssrTransformElement'
|
|
|
|
import { ssrTransformComponent } from './transforms/ssrTransformComponent'
|
|
|
|
import { ssrTransformSlotOutlet } from './transforms/ssrTransformSlotOutlet'
|
|
|
|
|
|
|
|
export interface SSRCompilerOptions extends CompilerOptions {}
|
|
|
|
|
|
|
|
export function compile(
|
|
|
|
template: string,
|
|
|
|
options: SSRCompilerOptions = {}
|
|
|
|
): CodegenResult {
|
2020-02-03 11:28:54 +08:00
|
|
|
options = {
|
2020-02-04 06:47:06 +08:00
|
|
|
mode: 'cjs',
|
|
|
|
...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,
|
|
|
|
...(options.nodeTransforms || []) // user transforms
|
|
|
|
],
|
|
|
|
directiveTransforms: {
|
|
|
|
// TODO server-side directive transforms
|
|
|
|
...(options.directiveTransforms || {}) // user transforms
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|