2019-09-21 00:16:19 +08:00
|
|
|
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'
|
2019-09-24 08:45:40 +08:00
|
|
|
import { transformElement } from './transforms/transformElement'
|
2019-09-23 10:19:42 +08:00
|
|
|
import { transformOn } from './transforms/vOn'
|
|
|
|
import { transformBind } from './transforms/vBind'
|
2019-09-24 08:45:40 +08:00
|
|
|
import { transformExpression } from './transforms/transformExpression'
|
2019-09-24 01:25:18 +08:00
|
|
|
import { defaultOnError, createCompilerError, ErrorCodes } from './errors'
|
2019-09-21 00:16:19 +08:00
|
|
|
|
|
|
|
export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions
|
|
|
|
|
|
|
|
export function compile(
|
2019-09-22 03:47:26 +08:00
|
|
|
template: string | RootNode,
|
2019-09-21 00:16:19 +08:00
|
|
|
options: CompilerOptions = {}
|
|
|
|
): CodegenResult {
|
2019-09-25 10:39:20 +08:00
|
|
|
if (__BROWSER__ && options.prefixIdentifiers) {
|
2019-09-24 01:25:18 +08:00
|
|
|
;(options.onError || defaultOnError)(
|
2019-09-24 01:29:41 +08:00
|
|
|
createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED)
|
2019-09-24 01:25:18 +08:00
|
|
|
)
|
|
|
|
}
|
2019-09-21 00:16:19 +08:00
|
|
|
|
2019-09-25 10:39:20 +08:00
|
|
|
const ast = isString(template) ? parse(template, options) : template
|
|
|
|
|
2019-09-21 00:16:19 +08:00
|
|
|
transform(ast, {
|
|
|
|
...options,
|
2019-09-25 10:39:20 +08:00
|
|
|
prefixIdentifiers: !__BROWSER__ && options.prefixIdentifiers === true,
|
2019-09-22 05:42:12 +08:00
|
|
|
nodeTransforms: [
|
2019-09-23 04:50:57 +08:00
|
|
|
transformIf,
|
|
|
|
transformFor,
|
2019-09-25 10:39:20 +08:00
|
|
|
transformExpression,
|
2019-09-24 08:45:40 +08:00
|
|
|
transformElement,
|
2019-09-22 05:42:12 +08:00
|
|
|
...(options.nodeTransforms || []) // user transforms
|
|
|
|
],
|
|
|
|
directiveTransforms: {
|
2019-09-23 10:19:42 +08:00
|
|
|
on: transformOn,
|
|
|
|
bind: transformBind,
|
2019-09-22 05:42:12 +08:00
|
|
|
...(options.directiveTransforms || {}) // user transforms
|
|
|
|
}
|
2019-09-21 00:16:19 +08:00
|
|
|
})
|
|
|
|
return generate(ast, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also expose lower level APIs & types
|
2019-09-18 07:08:47 +08:00
|
|
|
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'
|
2019-09-17 02:43:29 +08:00
|
|
|
export * from './ast'
|
2019-09-22 05:42:12 +08:00
|
|
|
|
|
|
|
// debug
|
2019-09-24 08:45:40 +08:00
|
|
|
export {
|
|
|
|
transformElement as prepareElementForCodegen
|
|
|
|
} from './transforms/transformElement'
|