2020-02-02 00:05:27 -05:00
|
|
|
import {
|
|
|
|
CodegenResult,
|
|
|
|
baseParse,
|
|
|
|
parserOptions,
|
|
|
|
transform,
|
|
|
|
generate,
|
|
|
|
CompilerOptions,
|
|
|
|
transformExpression,
|
|
|
|
trackVForSlotScopes,
|
2020-02-04 12:20:51 -05:00
|
|
|
trackSlotScopes,
|
2020-02-04 16:47:12 -05:00
|
|
|
noopDirectiveTransform,
|
2020-02-04 18:37:23 -05:00
|
|
|
transformBind,
|
2020-02-07 13:56:18 -05:00
|
|
|
transformStyle
|
2020-02-02 00:05:27 -05:00
|
|
|
} from '@vue/compiler-dom'
|
|
|
|
import { ssrCodegenTransform } from './ssrCodegenTransform'
|
|
|
|
import { ssrTransformElement } from './transforms/ssrTransformElement'
|
2020-02-07 13:56:18 -05:00
|
|
|
import {
|
|
|
|
ssrTransformComponent,
|
|
|
|
rawOptionsMap
|
|
|
|
} from './transforms/ssrTransformComponent'
|
2020-02-02 00:05:27 -05:00
|
|
|
import { ssrTransformSlotOutlet } from './transforms/ssrTransformSlotOutlet'
|
2020-02-04 12:20:51 -05:00
|
|
|
import { ssrTransformIf } from './transforms/ssrVIf'
|
|
|
|
import { ssrTransformFor } from './transforms/ssrVFor'
|
2020-02-04 16:47:12 -05:00
|
|
|
import { ssrTransformModel } from './transforms/ssrVModel'
|
|
|
|
import { ssrTransformShow } from './transforms/ssrVShow'
|
2020-06-26 14:23:50 -04:00
|
|
|
import { ssrInjectFallthroughAttrs } from './transforms/ssrInjectFallthroughAttrs'
|
2020-07-12 18:04:09 -04:00
|
|
|
import { ssrInjectCssVars } from './transforms/ssrInjectCssVars'
|
2020-02-02 00:05:27 -05:00
|
|
|
|
|
|
|
export function compile(
|
|
|
|
template: string,
|
2020-02-04 12:20:51 -05:00
|
|
|
options: CompilerOptions = {}
|
2020-02-02 00:05:27 -05:00
|
|
|
): CodegenResult {
|
2020-02-02 22:28:54 -05:00
|
|
|
options = {
|
2020-02-03 17:47:06 -05:00
|
|
|
...options,
|
|
|
|
// apply DOM-specific parsing options
|
2020-02-02 00:05:27 -05:00
|
|
|
...parserOptions,
|
2020-02-03 17:47:06 -05:00
|
|
|
ssr: true,
|
2021-06-23 07:15:20 +08:00
|
|
|
inSSR: true,
|
2020-02-06 16:51:26 -05:00
|
|
|
scopeId: options.mode === 'function' ? null : options.scopeId,
|
2020-02-03 17:47:06 -05: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-03 17:47:06 -05:00
|
|
|
cacheHandlers: false,
|
|
|
|
hoistStatic: false
|
2020-02-02 22:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const ast = baseParse(template, options)
|
2020-02-02 00:05:27 -05:00
|
|
|
|
2020-02-07 13:56:18 -05:00
|
|
|
// Save raw options for AST. This is needed when performing sub-transforms
|
|
|
|
// on slot vnode branches.
|
|
|
|
rawOptionsMap.set(ast, options)
|
|
|
|
|
2020-02-02 00:05:27 -05:00
|
|
|
transform(ast, {
|
|
|
|
...options,
|
2021-05-24 16:48:24 -04:00
|
|
|
hoistStatic: false,
|
2020-02-02 00:05:27 -05:00
|
|
|
nodeTransforms: [
|
|
|
|
ssrTransformIf,
|
|
|
|
ssrTransformFor,
|
|
|
|
trackVForSlotScopes,
|
|
|
|
transformExpression,
|
|
|
|
ssrTransformSlotOutlet,
|
2020-06-26 14:23:50 -04:00
|
|
|
ssrInjectFallthroughAttrs,
|
2020-07-12 18:04:09 -04:00
|
|
|
ssrInjectCssVars,
|
2020-02-02 00:05:27 -05:00
|
|
|
ssrTransformElement,
|
|
|
|
ssrTransformComponent,
|
|
|
|
trackSlotScopes,
|
2020-02-04 18:37:23 -05:00
|
|
|
transformStyle,
|
2020-02-02 00:05:27 -05:00
|
|
|
...(options.nodeTransforms || []) // user transforms
|
|
|
|
],
|
2020-02-04 18:37:23 -05:00
|
|
|
directiveTransforms: {
|
|
|
|
// reusing core v-bind
|
|
|
|
bind: transformBind,
|
|
|
|
// model and show has dedicated SSR handling
|
2020-02-04 16:47:12 -05:00
|
|
|
model: ssrTransformModel,
|
|
|
|
show: ssrTransformShow,
|
2020-02-04 18:37:23 -05:00
|
|
|
// the following are ignored during SSR
|
|
|
|
on: noopDirectiveTransform,
|
|
|
|
cloak: noopDirectiveTransform,
|
|
|
|
once: noopDirectiveTransform,
|
|
|
|
...(options.directiveTransforms || {}) // user transforms
|
2020-02-07 13:56:18 -05:00
|
|
|
}
|
2020-02-02 00:05:27 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// traverse the template AST and convert into SSR codegen AST
|
|
|
|
// by replacing ast.codegenNode.
|
2020-02-02 22:28:54 -05:00
|
|
|
ssrCodegenTransform(ast, options)
|
2020-02-02 00:05:27 -05:00
|
|
|
|
2020-02-03 17:47:06 -05:00
|
|
|
return generate(ast, options)
|
2020-01-22 10:39:09 -05:00
|
|
|
}
|