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,
|
2020-02-08 02:56:18 +08:00
|
|
|
transformStyle
|
2020-02-02 13:05:27 +08:00
|
|
|
} from '@vue/compiler-dom'
|
|
|
|
import { ssrCodegenTransform } from './ssrCodegenTransform'
|
|
|
|
import { ssrTransformElement } from './transforms/ssrTransformElement'
|
2020-02-08 02:56:18 +08:00
|
|
|
import {
|
|
|
|
ssrTransformComponent,
|
|
|
|
rawOptionsMap
|
|
|
|
} from './transforms/ssrTransformComponent'
|
2020-02-02 13:05:27 +08:00
|
|
|
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-06-27 02:23:50 +08:00
|
|
|
import { ssrInjectFallthroughAttrs } from './transforms/ssrInjectFallthroughAttrs'
|
2020-07-13 06:04:09 +08:00
|
|
|
import { ssrInjectCssVars } from './transforms/ssrInjectCssVars'
|
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,
|
2021-06-23 07:15:20 +08:00
|
|
|
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
|
2020-02-03 11:28:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ast = baseParse(template, options)
|
2020-02-02 13:05:27 +08:00
|
|
|
|
2020-02-08 02:56:18 +08:00
|
|
|
// Save raw options for AST. This is needed when performing sub-transforms
|
|
|
|
// on slot vnode branches.
|
|
|
|
rawOptionsMap.set(ast, options)
|
|
|
|
|
2020-02-02 13:05:27 +08:00
|
|
|
transform(ast, {
|
|
|
|
...options,
|
2021-05-25 04:48:24 +08:00
|
|
|
hoistStatic: false,
|
2020-02-02 13:05:27 +08:00
|
|
|
nodeTransforms: [
|
|
|
|
ssrTransformIf,
|
|
|
|
ssrTransformFor,
|
|
|
|
trackVForSlotScopes,
|
|
|
|
transformExpression,
|
|
|
|
ssrTransformSlotOutlet,
|
2020-06-27 02:23:50 +08:00
|
|
|
ssrInjectFallthroughAttrs,
|
2020-07-13 06:04:09 +08:00
|
|
|
ssrInjectCssVars,
|
2020-02-02 13:05:27 +08:00
|
|
|
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,
|
2021-09-17 22:23:06 +08:00
|
|
|
memo: noopDirectiveTransform,
|
2020-02-05 07:37:23 +08:00
|
|
|
...(options.directiveTransforms || {}) // user transforms
|
2020-02-08 02:56:18 +08:00
|
|
|
}
|
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
|
|
|
}
|