2023-12-01 22:12:19 +08:00
|
|
|
import {
|
|
|
|
type CodegenOptions,
|
|
|
|
type CodegenResult,
|
|
|
|
type Position,
|
|
|
|
NewlineType,
|
|
|
|
advancePositionWithMutation,
|
|
|
|
locStub,
|
|
|
|
} from '@vue/compiler-dom'
|
2023-11-24 15:25:34 +08:00
|
|
|
import {
|
|
|
|
type DynamicChildren,
|
|
|
|
type RootIRNode,
|
|
|
|
IRNodeTypes,
|
|
|
|
OperationNode,
|
2023-12-01 05:18:20 +08:00
|
|
|
VaporHelper,
|
2023-12-01 22:12:19 +08:00
|
|
|
IRNode,
|
2023-11-24 15:25:34 +08:00
|
|
|
} from './ir'
|
2023-12-01 22:12:19 +08:00
|
|
|
import { SourceMapGenerator } from 'source-map-js'
|
2023-11-17 03:01:19 +08:00
|
|
|
|
2023-11-24 14:44:57 +08:00
|
|
|
// remove when stable
|
2023-12-01 22:12:19 +08:00
|
|
|
// @ts-expect-error
|
|
|
|
function checkNever(x: never): never {}
|
|
|
|
|
|
|
|
export interface CodegenContext
|
|
|
|
extends Omit<Required<CodegenOptions>, 'bindingMetadata' | 'inline'> {
|
|
|
|
source: string
|
|
|
|
code: string
|
|
|
|
line: number
|
|
|
|
column: number
|
|
|
|
offset: number
|
|
|
|
indentLevel: number
|
|
|
|
map?: SourceMapGenerator
|
|
|
|
|
|
|
|
push(code: string, newlineIndex?: NewlineType, node?: IRNode): void
|
|
|
|
indent(): void
|
|
|
|
deindent(withoutNewLine?: boolean): void
|
|
|
|
newline(): void
|
2023-11-24 14:44:57 +08:00
|
|
|
|
2023-12-01 05:18:20 +08:00
|
|
|
helpers: Set<string>
|
|
|
|
vaporHelpers: Set<string>
|
|
|
|
helper(name: string): string
|
|
|
|
vaporHelper(name: string): string
|
|
|
|
}
|
|
|
|
|
2023-12-01 22:12:19 +08:00
|
|
|
function createCodegenContext(
|
|
|
|
ir: RootIRNode,
|
|
|
|
{
|
|
|
|
mode = 'function',
|
|
|
|
prefixIdentifiers = mode === 'module',
|
|
|
|
sourceMap = false,
|
|
|
|
filename = `template.vue.html`,
|
|
|
|
scopeId = null,
|
|
|
|
optimizeImports = false,
|
|
|
|
runtimeGlobalName = `Vue`,
|
|
|
|
runtimeModuleName = `vue`,
|
|
|
|
ssrRuntimeModuleName = 'vue/server-renderer',
|
|
|
|
ssr = false,
|
|
|
|
isTS = false,
|
|
|
|
inSSR = false,
|
|
|
|
}: CodegenOptions,
|
|
|
|
) {
|
2023-12-01 05:18:20 +08:00
|
|
|
const { helpers, vaporHelpers } = ir
|
2023-12-01 22:12:19 +08:00
|
|
|
const context: CodegenContext = {
|
|
|
|
mode,
|
|
|
|
prefixIdentifiers,
|
|
|
|
sourceMap,
|
|
|
|
filename,
|
|
|
|
scopeId,
|
|
|
|
optimizeImports,
|
|
|
|
runtimeGlobalName,
|
|
|
|
runtimeModuleName,
|
|
|
|
ssrRuntimeModuleName,
|
|
|
|
ssr,
|
|
|
|
isTS,
|
|
|
|
inSSR,
|
|
|
|
|
|
|
|
source: ir.source,
|
|
|
|
code: ``,
|
|
|
|
column: 1,
|
|
|
|
line: 1,
|
|
|
|
offset: 0,
|
|
|
|
indentLevel: 0,
|
|
|
|
|
2023-12-01 05:18:20 +08:00
|
|
|
helpers,
|
|
|
|
vaporHelpers,
|
|
|
|
helper(name: string) {
|
|
|
|
helpers.add(name)
|
2023-12-01 08:26:01 +08:00
|
|
|
return `_${name}`
|
2023-12-01 05:18:20 +08:00
|
|
|
},
|
|
|
|
vaporHelper(name: VaporHelper) {
|
|
|
|
vaporHelpers.add(name)
|
2023-12-01 08:26:01 +08:00
|
|
|
return `_${name}`
|
2023-12-01 05:18:20 +08:00
|
|
|
},
|
2023-12-01 22:12:19 +08:00
|
|
|
push(code, newlineIndex: NewlineType = NewlineType.None, node) {
|
|
|
|
context.code += code
|
|
|
|
if (!__BROWSER__ && context.map) {
|
|
|
|
if (node) {
|
|
|
|
// TODO
|
|
|
|
let name
|
|
|
|
// if (node.type === NodeTypes.SIMPLE_EXPRESSION && !node.isStatic) {
|
|
|
|
// const content = node.content.replace(/^_ctx\./, '')
|
|
|
|
// if (content !== node.content && isSimpleIdentifier(content)) {
|
|
|
|
// name = content
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
addMapping(node.loc.start, name)
|
|
|
|
}
|
|
|
|
if (newlineIndex === NewlineType.Unknown) {
|
|
|
|
// multiple newlines, full iteration
|
|
|
|
advancePositionWithMutation(context, code)
|
|
|
|
} else {
|
|
|
|
// fast paths
|
|
|
|
context.offset += code.length
|
|
|
|
if (newlineIndex === NewlineType.None) {
|
|
|
|
// no newlines; fast path to avoid newline detection
|
|
|
|
if (__TEST__ && code.includes('\n')) {
|
|
|
|
throw new Error(
|
|
|
|
`CodegenContext.push() called newlineIndex: none, but contains` +
|
|
|
|
`newlines: ${code.replace(/\n/g, '\\n')}`,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
context.column += code.length
|
|
|
|
} else {
|
|
|
|
// single newline at known index
|
|
|
|
if (newlineIndex === NewlineType.End) {
|
|
|
|
newlineIndex = code.length - 1
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
__TEST__ &&
|
|
|
|
(code.charAt(newlineIndex) !== '\n' ||
|
|
|
|
code.slice(0, newlineIndex).includes('\n') ||
|
|
|
|
code.slice(newlineIndex + 1).includes('\n'))
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
`CodegenContext.push() called with newlineIndex: ${newlineIndex} ` +
|
|
|
|
`but does not conform: ${code.replace(/\n/g, '\\n')}`,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
context.line++
|
|
|
|
context.column = code.length - newlineIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node && node.loc !== locStub) {
|
|
|
|
addMapping(node.loc.end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
indent() {
|
|
|
|
newline(++context.indentLevel)
|
|
|
|
},
|
|
|
|
deindent(withoutNewLine = false) {
|
|
|
|
if (withoutNewLine) {
|
|
|
|
--context.indentLevel
|
|
|
|
} else {
|
|
|
|
newline(--context.indentLevel)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
newline() {
|
|
|
|
newline(context.indentLevel)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
function newline(n: number) {
|
|
|
|
context.push(`\n${` `.repeat(n)}`, NewlineType.Start)
|
|
|
|
}
|
|
|
|
|
|
|
|
function addMapping(loc: Position, name: string | null = null) {
|
|
|
|
// we use the private property to directly add the mapping
|
|
|
|
// because the addMapping() implementation in source-map-js has a bunch of
|
|
|
|
// unnecessary arg and validation checks that are pure overhead in our case.
|
|
|
|
const { _names, _mappings } = context.map!
|
|
|
|
if (name !== null && !_names.has(name)) _names.add(name)
|
|
|
|
_mappings.add({
|
|
|
|
originalLine: loc.line,
|
|
|
|
originalColumn: loc.column - 1, // source-map column is 0 based
|
|
|
|
generatedLine: context.line,
|
|
|
|
generatedColumn: context.column - 1,
|
|
|
|
source: filename,
|
|
|
|
// @ts-ignore it is possible to be null
|
|
|
|
name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!__BROWSER__ && sourceMap) {
|
|
|
|
// lazy require source-map implementation, only in non-browser builds
|
|
|
|
context.map = new SourceMapGenerator()
|
|
|
|
context.map.setSourceContent(filename, context.source)
|
|
|
|
context.map._sources.add(filename)
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
2023-12-01 22:12:19 +08:00
|
|
|
|
|
|
|
return context
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
|
|
|
|
2023-11-17 03:01:19 +08:00
|
|
|
// IR -> JS codegen
|
|
|
|
export function generate(
|
2023-11-23 23:42:08 +08:00
|
|
|
ir: RootIRNode,
|
2023-11-24 11:15:33 +08:00
|
|
|
options: CodegenOptions = {},
|
2023-11-17 03:01:19 +08:00
|
|
|
): CodegenResult {
|
2023-12-01 05:18:20 +08:00
|
|
|
const ctx = createCodegenContext(ir, options)
|
|
|
|
const { vaporHelper, helpers, vaporHelpers } = ctx
|
|
|
|
|
2023-12-01 22:12:19 +08:00
|
|
|
const functionName = 'render'
|
|
|
|
const isSetupInlined = !!options.inline
|
|
|
|
if (isSetupInlined) {
|
|
|
|
ctx.push(`(() => {\n`, NewlineType.End)
|
|
|
|
} else {
|
|
|
|
ctx.push(`export function ${functionName}(_ctx) {\n`, NewlineType.End)
|
|
|
|
}
|
2023-11-17 03:01:19 +08:00
|
|
|
|
2023-11-26 03:53:47 +08:00
|
|
|
ir.template.forEach((template, i) => {
|
|
|
|
if (template.type === IRNodeTypes.TEMPLATE_FACTORY) {
|
2023-12-01 22:12:19 +08:00
|
|
|
// TODO source map?
|
|
|
|
ctx.push(
|
|
|
|
`const t${i} = ${vaporHelper('template')}(${JSON.stringify(
|
|
|
|
template.template,
|
|
|
|
)})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
)
|
2023-11-26 03:53:47 +08:00
|
|
|
} else {
|
|
|
|
// fragment
|
2023-12-01 22:12:19 +08:00
|
|
|
ctx.push(`const t0 = ${vaporHelper('fragment')}()\n`, NewlineType.End)
|
2023-11-26 03:53:47 +08:00
|
|
|
}
|
|
|
|
})
|
2023-11-17 03:01:19 +08:00
|
|
|
|
2023-11-26 02:13:59 +08:00
|
|
|
{
|
2023-12-01 22:12:19 +08:00
|
|
|
ctx.push(`const n${ir.dynamic.id} = t0()\n`, NewlineType.End, ir)
|
2023-11-27 05:16:21 +08:00
|
|
|
const children = genChildren(ir.dynamic.children)
|
|
|
|
if (children) {
|
2023-12-01 22:12:19 +08:00
|
|
|
ctx.push(
|
|
|
|
`const ${children} = ${vaporHelper('children')}(n${ir.dynamic.id})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
)
|
2023-11-26 03:08:35 +08:00
|
|
|
}
|
2023-11-23 23:42:08 +08:00
|
|
|
|
2023-11-24 20:29:05 +08:00
|
|
|
for (const operation of ir.operation) {
|
2023-12-01 22:12:19 +08:00
|
|
|
genOperation(operation, ctx).forEach((args) => ctx.push(...args))
|
2023-11-23 23:42:08 +08:00
|
|
|
}
|
2023-11-24 20:29:05 +08:00
|
|
|
for (const [_expr, operations] of Object.entries(ir.effect)) {
|
2023-12-01 22:12:19 +08:00
|
|
|
ctx.push(`${vaporHelper('effect')}(() => {\n`, NewlineType.End)
|
2023-11-24 20:29:05 +08:00
|
|
|
for (const operation of operations) {
|
2023-12-01 22:12:19 +08:00
|
|
|
genOperation(operation, ctx).forEach((args) => ctx.push(...args))
|
2023-11-24 20:29:05 +08:00
|
|
|
}
|
2023-12-01 22:12:19 +08:00
|
|
|
ctx.push('})\n', NewlineType.End)
|
2023-11-24 20:29:05 +08:00
|
|
|
}
|
|
|
|
// TODO multiple-template
|
2023-11-26 03:53:47 +08:00
|
|
|
// TODO return statement in IR
|
2023-12-01 22:12:19 +08:00
|
|
|
ctx.push(`return n${ir.dynamic.id}\n`, NewlineType.End)
|
2023-11-24 20:29:05 +08:00
|
|
|
}
|
2023-11-17 03:01:19 +08:00
|
|
|
|
2023-12-01 22:12:19 +08:00
|
|
|
if (isSetupInlined) {
|
|
|
|
ctx.push('})()')
|
|
|
|
} else {
|
|
|
|
ctx.push('}')
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.newline()
|
|
|
|
|
2023-11-24 11:07:31 +08:00
|
|
|
if (vaporHelpers.size)
|
2023-12-01 08:26:01 +08:00
|
|
|
// TODO: extract
|
2023-12-01 22:12:19 +08:00
|
|
|
ctx.push(
|
2023-12-01 08:26:01 +08:00
|
|
|
`import { ${[...vaporHelpers]
|
|
|
|
.map((h) => `${h} as _${h}`)
|
2023-12-01 22:12:19 +08:00
|
|
|
.join(', ')} } from 'vue/vapor'\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
)
|
2023-11-24 11:07:31 +08:00
|
|
|
if (helpers.size)
|
2023-12-01 22:12:19 +08:00
|
|
|
ctx.push(
|
2023-12-01 08:26:01 +08:00
|
|
|
`import { ${[...helpers]
|
|
|
|
.map((h) => `${h} as _${h}`)
|
2023-12-01 22:12:19 +08:00
|
|
|
.join(', ')} } from 'vue'\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
)
|
2023-11-17 03:01:19 +08:00
|
|
|
|
|
|
|
return {
|
2023-12-01 22:12:19 +08:00
|
|
|
code: ctx.code,
|
2023-11-23 23:42:08 +08:00
|
|
|
ast: ir as any,
|
2023-12-01 22:12:19 +08:00
|
|
|
preamble: '',
|
|
|
|
map: ctx.map ? ctx.map.toJSON() : undefined,
|
2023-11-23 23:42:08 +08:00
|
|
|
}
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
2023-11-24 15:25:34 +08:00
|
|
|
|
2023-12-01 22:12:19 +08:00
|
|
|
function genOperation(
|
|
|
|
oper: OperationNode,
|
|
|
|
{ vaporHelper }: CodegenContext,
|
|
|
|
): Parameters<CodegenContext['push']>[] {
|
2023-12-01 05:18:20 +08:00
|
|
|
// TODO: cache old value
|
|
|
|
switch (oper.type) {
|
|
|
|
case IRNodeTypes.SET_PROP: {
|
2023-12-01 22:12:19 +08:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
`${vaporHelper('setAttr')}(n${oper.element}, ${JSON.stringify(
|
|
|
|
oper.name,
|
|
|
|
)}, undefined, ${oper.value})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
],
|
|
|
|
]
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
2023-11-24 15:25:34 +08:00
|
|
|
|
2023-12-01 05:18:20 +08:00
|
|
|
case IRNodeTypes.SET_TEXT: {
|
2023-12-01 22:12:19 +08:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
`${vaporHelper('setText')}(n${oper.element}, undefined, ${
|
|
|
|
oper.value
|
|
|
|
})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
],
|
|
|
|
]
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
2023-11-24 15:25:34 +08:00
|
|
|
|
2023-12-01 05:18:20 +08:00
|
|
|
case IRNodeTypes.SET_EVENT: {
|
|
|
|
let value = oper.value
|
|
|
|
if (oper.modifiers.length) {
|
|
|
|
value = `${vaporHelper('withModifiers')}(${value}, ${genArrayExpression(
|
|
|
|
oper.modifiers,
|
|
|
|
)})`
|
2023-11-24 15:25:34 +08:00
|
|
|
}
|
2023-12-01 22:12:19 +08:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
`${vaporHelper('on')}(n${oper.element}, ${JSON.stringify(
|
|
|
|
oper.name,
|
|
|
|
)}, ${value})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
],
|
|
|
|
]
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
2023-11-24 15:25:34 +08:00
|
|
|
|
2023-12-01 05:18:20 +08:00
|
|
|
case IRNodeTypes.SET_HTML: {
|
2023-12-01 22:12:19 +08:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
`${vaporHelper('setHtml')}(n${oper.element}, undefined, ${
|
|
|
|
oper.value
|
|
|
|
})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
],
|
|
|
|
]
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
2023-11-24 15:25:34 +08:00
|
|
|
|
2023-12-01 05:18:20 +08:00
|
|
|
case IRNodeTypes.CREATE_TEXT_NODE: {
|
2023-12-01 22:12:19 +08:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
`const n${oper.id} = ${vaporHelper('createTextNode')}(${
|
|
|
|
oper.value
|
|
|
|
})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
],
|
|
|
|
]
|
2023-11-24 15:25:34 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 05:18:20 +08:00
|
|
|
case IRNodeTypes.INSERT_NODE: {
|
|
|
|
const elements = ([] as number[]).concat(oper.element)
|
|
|
|
let element = elements.map((el) => `n${el}`).join(', ')
|
|
|
|
if (elements.length > 1) element = `[${element}]`
|
2023-12-01 22:12:19 +08:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
`${vaporHelper('insert')}(${element}, n${
|
|
|
|
oper.parent
|
|
|
|
}${`, n${oper.anchor}`})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
],
|
|
|
|
]
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
|
|
|
case IRNodeTypes.PREPEND_NODE: {
|
2023-12-01 22:12:19 +08:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
`${vaporHelper('prepend')}(n${oper.parent}, ${oper.elements
|
|
|
|
.map((el) => `n${el}`)
|
|
|
|
.join(', ')})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
],
|
|
|
|
]
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
|
|
|
case IRNodeTypes.APPEND_NODE: {
|
2023-12-01 22:12:19 +08:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
`${vaporHelper('append')}(n${oper.parent}, ${oper.elements
|
|
|
|
.map((el) => `n${el}`)
|
|
|
|
.join(', ')})\n`,
|
|
|
|
NewlineType.End,
|
|
|
|
],
|
|
|
|
]
|
2023-12-01 05:18:20 +08:00
|
|
|
}
|
|
|
|
default:
|
2023-12-01 22:12:19 +08:00
|
|
|
return checkNever(oper)
|
2023-11-24 15:25:34 +08:00
|
|
|
}
|
2023-11-23 23:42:08 +08:00
|
|
|
}
|
|
|
|
|
2023-11-24 11:07:31 +08:00
|
|
|
function genChildren(children: DynamicChildren) {
|
2023-11-27 05:16:21 +08:00
|
|
|
let code = ''
|
|
|
|
// TODO
|
|
|
|
let offset = 0
|
2023-11-23 23:42:08 +08:00
|
|
|
for (const [index, child] of Object.entries(children)) {
|
2023-11-27 05:16:21 +08:00
|
|
|
const childrenLength = Object.keys(child.children).length
|
2023-11-27 06:22:10 +08:00
|
|
|
if (child.ghost && child.placeholder === null && childrenLength === 0) {
|
|
|
|
offset--
|
2023-11-27 05:16:21 +08:00
|
|
|
continue
|
2023-11-27 06:22:10 +08:00
|
|
|
}
|
2023-11-27 05:16:21 +08:00
|
|
|
|
|
|
|
code += ` ${Number(index) + offset}: [`
|
|
|
|
|
|
|
|
const id = child.ghost ? child.placeholder : child.id
|
|
|
|
if (id !== null) code += `n${id}`
|
|
|
|
|
|
|
|
const childrenString = childrenLength && genChildren(child.children)
|
|
|
|
if (childrenString) code += `, ${childrenString}`
|
|
|
|
|
|
|
|
code += '],'
|
2023-11-17 03:01:19 +08:00
|
|
|
}
|
2023-11-27 05:16:21 +08:00
|
|
|
|
|
|
|
if (!code) return ''
|
|
|
|
return `{${code}}`
|
2023-11-17 03:01:19 +08:00
|
|
|
}
|
2023-11-30 05:11:59 +08:00
|
|
|
|
|
|
|
// TODO: other types (not only string)
|
|
|
|
function genArrayExpression(elements: string[]) {
|
|
|
|
return `[${elements.map((it) => `"${it}"`).join(', ')}]`
|
|
|
|
}
|