2019-09-20 12:12:37 +08:00
|
|
|
import {
|
|
|
|
parse,
|
|
|
|
transform,
|
|
|
|
generate,
|
|
|
|
CompilerError,
|
|
|
|
Transform,
|
|
|
|
CodegenResult
|
|
|
|
} from '@vue/compiler-core'
|
2019-09-17 23:57:25 +08:00
|
|
|
import { parserOptionsMinimal } from './parserOptionsMinimal'
|
|
|
|
import { parserOptionsStandard } from './parserOptionsStandard'
|
|
|
|
|
2019-09-20 12:12:37 +08:00
|
|
|
const parserOptions = __BROWSER__ ? parserOptionsMinimal : parserOptionsStandard
|
|
|
|
|
|
|
|
export interface CompilerOptions {
|
|
|
|
module?: boolean
|
|
|
|
onError?(err: CompilerError): void
|
|
|
|
transforms?: Transform[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export function compile(
|
|
|
|
template: string,
|
|
|
|
options: CompilerOptions = {}
|
|
|
|
): CodegenResult {
|
|
|
|
const {
|
|
|
|
module = false,
|
|
|
|
onError = (err: CompilerError) => {
|
|
|
|
throw err
|
|
|
|
},
|
|
|
|
transforms = []
|
|
|
|
} = options
|
|
|
|
const ast = parse(template, {
|
|
|
|
...parserOptions,
|
|
|
|
onError
|
|
|
|
})
|
|
|
|
transform(ast, {
|
|
|
|
transforms: [
|
|
|
|
// TODO include core transforms
|
|
|
|
// TODO include DOM transforms
|
|
|
|
...transforms // user transforms
|
|
|
|
],
|
|
|
|
onError
|
|
|
|
})
|
|
|
|
return generate(ast, { module })
|
|
|
|
}
|
|
|
|
|
|
|
|
export * from '@vue/compiler-core'
|