vue3-yuanma/packages/compiler-core/src/index.ts

60 lines
1.8 KiB
TypeScript
Raw Normal View History

import { parse, ParserOptions } from './parse'
import { transform, TransformOptions } from './transform'
import { generate, CodegenOptions, CodegenResult } from './codegen'
2019-09-22 03:47:26 +08:00
import { RootNode } from './ast'
import { isString } from '@vue/shared'
2019-09-23 04:50:57 +08:00
import { transformIf } from './transforms/vIf'
import { transformFor } from './transforms/vFor'
import { prepareElementForCodegen } from './transforms/element'
import { transformOn } from './transforms/vOn'
import { transformBind } from './transforms/vBind'
2019-09-23 14:52:54 +08:00
import { rewriteExpression } from './transforms/expression'
export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions
export function compile(
2019-09-22 03:47:26 +08:00
template: string | RootNode,
options: CompilerOptions = {}
): CodegenResult {
2019-09-22 03:47:26 +08:00
const ast = isString(template) ? parse(template, options) : template
transform(ast, {
...options,
2019-09-22 05:42:12 +08:00
nodeTransforms: [
2019-09-23 19:57:56 +08:00
...(!__BROWSER__ && options.useWith === false ? [rewriteExpression] : []),
2019-09-23 04:50:57 +08:00
transformIf,
transformFor,
prepareElementForCodegen,
2019-09-22 05:42:12 +08:00
...(options.nodeTransforms || []) // user transforms
],
directiveTransforms: {
on: transformOn,
bind: transformBind,
2019-09-22 05:42:12 +08:00
...(options.directiveTransforms || {}) // user transforms
}
})
return generate(ast, options)
}
// Also expose lower level APIs & types
export { parse, ParserOptions, TextModes } from './parse'
2019-09-20 12:12:37 +08:00
export {
transform,
2019-09-22 05:42:12 +08:00
createStructuralDirectiveTransform,
2019-09-20 12:12:37 +08:00
TransformOptions,
2019-09-22 03:47:26 +08:00
TransformContext,
2019-09-22 05:42:12 +08:00
NodeTransform as Transform,
StructuralDirectiveTransform
2019-09-20 12:12:37 +08:00
} from './transform'
2019-09-22 03:47:26 +08:00
export {
generate,
CodegenOptions,
CodegenContext,
CodegenResult
} from './codegen'
2019-09-20 12:12:37 +08:00
export { ErrorCodes, CompilerError, createCompilerError } from './errors'
export * from './ast'
2019-09-22 05:42:12 +08:00
// debug
export { prepareElementForCodegen } from './transforms/element'