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

108 lines
3.3 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 { transformExpression } from './transforms/transformExpression'
import { transformSlotOutlet } from './transforms/transformSlotOutlet'
2019-09-24 08:45:40 +08:00
import { transformElement } from './transforms/transformElement'
import { transformOn } from './transforms/vOn'
import { transformBind } from './transforms/vBind'
import { defaultOnError, createCompilerError, ErrorCodes } from './errors'
import { trackSlotScopes, trackVForSlotScopes } from './transforms/vSlot'
import { optimizeText } from './transforms/optimizeText'
import { transformOnce } from './transforms/vOnce'
import { transformModel } from './transforms/vModel'
export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions
// we name it `baseCompile` so that higher order compilers like @vue/compiler-dom
// can export `compile` while re-exporting everything else.
export function baseCompile(
2019-09-22 03:47:26 +08:00
template: string | RootNode,
options: CompilerOptions = {}
): CodegenResult {
2019-09-29 02:15:10 +08:00
/* istanbul ignore if */
2019-09-26 10:29:37 +08:00
if (__BROWSER__) {
const onError = options.onError || defaultOnError
if (options.prefixIdentifiers === true) {
onError(createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED))
} else if (options.mode === 'module') {
onError(createCompilerError(ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED))
}
}
2019-09-25 10:39:20 +08:00
const ast = isString(template) ? parse(template, options) : template
2019-09-26 10:29:37 +08:00
const prefixIdentifiers =
!__BROWSER__ &&
(options.prefixIdentifiers === true || options.mode === 'module')
transform(ast, {
...options,
prefixIdentifiers,
2019-09-22 05:42:12 +08:00
nodeTransforms: [
2019-09-23 04:50:57 +08:00
transformIf,
transformFor,
...(prefixIdentifiers
? [
// order is important
trackVForSlotScopes,
transformExpression
]
: []),
transformSlotOutlet,
2019-09-24 08:45:40 +08:00
transformElement,
trackSlotScopes,
optimizeText,
2019-09-22 05:42:12 +08:00
...(options.nodeTransforms || []) // user transforms
],
directiveTransforms: {
on: transformOn,
bind: transformBind,
once: transformOnce,
model: transformModel,
2019-09-22 05:42:12 +08:00
...(options.directiveTransforms || {}) // user transforms
}
})
return generate(ast, {
...options,
prefixIdentifiers
})
}
// 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,
NodeTransform,
StructuralDirectiveTransform,
DirectiveTransform
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'
export {
ErrorCodes,
CoreCompilerError,
CompilerError,
createCompilerError
} from './errors'
export * from './ast'
2019-10-11 06:02:51 +08:00
export * from './utils'
export * from './codeframe'
2019-10-11 06:02:51 +08:00
export { registerRuntimeHelpers } from './runtimeHelpers'
// expose transforms so higher-order compilers can import and extend them
export { transformModel } from './transforms/vModel'
export { transformOn } from './transforms/vOn'