vue3-yuanma/packages/compiler-ssr/src/index.ts

65 lines
1.7 KiB
TypeScript
Raw Normal View History

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 {
options = {
2020-02-04 06:47:06 +08:00
mode: 'cjs',
...options,
// apply DOM-specific parsing options
...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
}
const ast = baseParse(template, options)
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.
ssrCodegenTransform(ast, options)
2020-02-04 06:47:06 +08:00
return generate(ast, options)
2020-01-22 23:39:09 +08:00
}