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

90 lines
2.6 KiB
TypeScript
Raw Normal View History

import {
CodegenResult,
baseParse,
parserOptions,
transform,
generate,
CompilerOptions,
transformExpression,
trackVForSlotScopes,
2020-02-05 01:20:51 +08:00
trackSlotScopes,
noopDirectiveTransform,
transformBind,
transformStyle
} from '@vue/compiler-dom'
import { ssrCodegenTransform } from './ssrCodegenTransform'
import { ssrTransformElement } from './transforms/ssrTransformElement'
import {
ssrTransformComponent,
rawOptionsMap
} 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'
import { ssrTransformModel } from './transforms/ssrVModel'
import { ssrTransformShow } from './transforms/ssrVShow'
import { ssrInjectFallthroughAttrs } from './transforms/ssrInjectFallthroughAttrs'
2020-07-13 06:04:09 +08:00
import { ssrInjectCssVars } from './transforms/ssrInjectCssVars'
export function compile(
template: string,
2020-02-05 01:20:51 +08:00
options: CompilerOptions = {}
): CodegenResult {
options = {
2020-02-04 06:47:06 +08:00
...options,
// apply DOM-specific parsing options
...parserOptions,
2020-02-04 06:47:06 +08:00
ssr: true,
inSSR: true,
2020-02-07 05:51:26 +08:00
scopeId: options.mode === 'function' ? null : options.scopeId,
2020-02-04 06:47:06 +08:00
// always prefix since compiler-ssr doesn't have size concern
prefixIdentifiers: true,
2020-02-10 22:20:07 +08:00
// disable optimizations that are unnecessary for ssr
2020-02-04 06:47:06 +08:00
cacheHandlers: false,
hoistStatic: false
}
const ast = baseParse(template, options)
// Save raw options for AST. This is needed when performing sub-transforms
// on slot vnode branches.
rawOptionsMap.set(ast, options)
transform(ast, {
...options,
hoistStatic: false,
nodeTransforms: [
ssrTransformIf,
ssrTransformFor,
trackVForSlotScopes,
transformExpression,
ssrTransformSlotOutlet,
ssrInjectFallthroughAttrs,
2020-07-13 06:04:09 +08:00
ssrInjectCssVars,
ssrTransformElement,
ssrTransformComponent,
trackSlotScopes,
transformStyle,
...(options.nodeTransforms || []) // user transforms
],
directiveTransforms: {
// reusing core v-bind
bind: transformBind,
// model and show has dedicated SSR handling
model: ssrTransformModel,
show: ssrTransformShow,
// the following are ignored during SSR
on: noopDirectiveTransform,
cloak: noopDirectiveTransform,
once: noopDirectiveTransform,
...(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
}