2016-11-06 04:20:00 +08:00
|
|
|
|
2017-02-25 06:20:48 +08:00
|
|
|
import { genStaticKeys } from 'shared/util'
|
|
|
|
import { createCompiler } from 'compiler/index'
|
|
|
|
|
2016-11-06 04:20:00 +08:00
|
|
|
import modules from './modules/index'
|
|
|
|
import directives from './directives/index'
|
2017-02-25 06:20:48 +08:00
|
|
|
|
2016-11-06 04:20:00 +08:00
|
|
|
import {
|
2017-02-25 06:20:48 +08:00
|
|
|
isUnaryTag,
|
|
|
|
mustUseProp,
|
|
|
|
isReservedTag,
|
2017-03-15 09:56:25 +08:00
|
|
|
canBeLeftOpenTag,
|
2021-04-03 22:06:56 +08:00
|
|
|
getTagNamespace,
|
2017-12-19 10:51:38 +08:00
|
|
|
} from '../util/element'
|
2021-04-04 00:29:42 +08:00
|
|
|
import type { WeexCompiledResult, WeexCompilerOptions } from 'typescript/weex'
|
2016-11-06 04:20:00 +08:00
|
|
|
|
2017-11-29 23:35:20 +08:00
|
|
|
export const baseOptions: WeexCompilerOptions = {
|
2016-11-06 04:20:00 +08:00
|
|
|
modules,
|
|
|
|
directives,
|
|
|
|
isUnaryTag,
|
|
|
|
mustUseProp,
|
2017-03-15 09:56:25 +08:00
|
|
|
canBeLeftOpenTag,
|
2017-02-25 06:20:48 +08:00
|
|
|
isReservedTag,
|
|
|
|
getTagNamespace,
|
|
|
|
preserveWhitespace: false,
|
2017-11-14 00:47:39 +08:00
|
|
|
recyclable: false,
|
2021-04-03 22:06:56 +08:00
|
|
|
// @ts-ignore
|
|
|
|
staticKeys: genStaticKeys(modules),
|
|
|
|
} as any
|
2016-11-06 04:20:00 +08:00
|
|
|
|
2017-11-14 00:47:39 +08:00
|
|
|
const compiler = createCompiler(baseOptions)
|
|
|
|
|
2021-04-03 22:06:56 +08:00
|
|
|
export function compile(
|
2017-11-14 00:47:39 +08:00
|
|
|
template: string,
|
2017-11-29 23:35:20 +08:00
|
|
|
options?: WeexCompilerOptions
|
|
|
|
): WeexCompiledResult {
|
2017-11-14 00:47:39 +08:00
|
|
|
let generateAltRender = false
|
|
|
|
if (options && options.recyclable === true) {
|
|
|
|
generateAltRender = true
|
|
|
|
options.recyclable = false
|
|
|
|
}
|
|
|
|
const result = compiler.compile(template, options)
|
|
|
|
|
|
|
|
// generate @render function for <recycle-list>
|
|
|
|
if (options && generateAltRender) {
|
|
|
|
options.recyclable = true
|
2017-11-29 06:36:06 +08:00
|
|
|
// disable static optimizations
|
|
|
|
options.optimize = false
|
2017-11-14 00:47:39 +08:00
|
|
|
const { render } = compiler.compile(template, options)
|
|
|
|
result['@render'] = render
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|