2023-11-17 03:01:19 +08:00
|
|
|
import {
|
|
|
|
CodegenContext,
|
|
|
|
CodegenOptions,
|
|
|
|
CodegenResult
|
|
|
|
} from '@vue/compiler-dom'
|
|
|
|
import { RootIRNode } from './transform'
|
|
|
|
|
|
|
|
// IR -> JS codegen
|
|
|
|
export function generate(
|
|
|
|
ast: RootIRNode,
|
|
|
|
options: CodegenOptions & {
|
|
|
|
onContextCreated?: (context: CodegenContext) => void
|
|
|
|
} = {}
|
|
|
|
): CodegenResult {
|
|
|
|
let code = ''
|
|
|
|
let preamble = "import { template } from 'vue/vapor'\n"
|
|
|
|
|
|
|
|
const isSetupInlined = !!options.inline
|
|
|
|
|
|
|
|
preamble += ast.template
|
2023-11-17 17:35:49 +08:00
|
|
|
.map((template, i) => `const t${i} = template(\`${template.template}\`)\n`)
|
|
|
|
.join('')
|
2023-11-17 03:01:19 +08:00
|
|
|
|
|
|
|
code += 'const root = t0()\n'
|
|
|
|
code += 'return root'
|
|
|
|
|
|
|
|
const functionName = options.ssr ? `ssrRender` : `render`
|
|
|
|
if (isSetupInlined) {
|
|
|
|
code = `(() => {\n${code}\n})();`
|
|
|
|
} else {
|
2023-11-17 17:35:49 +08:00
|
|
|
code = `${preamble}export function ${functionName}() {\n${code}\n}`
|
2023-11-17 03:01:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
code,
|
|
|
|
ast: ast as any,
|
|
|
|
preamble
|
|
|
|
}
|
|
|
|
}
|