2019-09-20 12:16:19 -04:00
|
|
|
import { parse, ParserOptions } from './parse'
|
|
|
|
import { transform, TransformOptions } from './transform'
|
|
|
|
import { generate, CodegenOptions, CodegenResult } from './codegen'
|
2019-09-21 15:47:26 -04:00
|
|
|
import { RootNode } from './ast'
|
|
|
|
import { isString } from '@vue/shared'
|
2019-09-22 16:50:57 -04:00
|
|
|
import { transformIf } from './transforms/vIf'
|
|
|
|
import { transformFor } from './transforms/vFor'
|
2019-09-28 00:19:24 -04:00
|
|
|
import { transformExpression } from './transforms/transformExpression'
|
2019-10-05 03:12:51 +01:00
|
|
|
import { transformSlotOutlet } from './transforms/transformSlotOutlet'
|
2019-09-23 20:45:40 -04:00
|
|
|
import { transformElement } from './transforms/transformElement'
|
2019-09-22 22:19:42 -04:00
|
|
|
import { transformOn } from './transforms/vOn'
|
|
|
|
import { transformBind } from './transforms/vBind'
|
2019-09-23 13:25:18 -04:00
|
|
|
import { defaultOnError, createCompilerError, ErrorCodes } from './errors'
|
2019-10-02 23:10:41 -04:00
|
|
|
import { trackSlotScopes, trackVForSlotScopes } from './transforms/vSlot'
|
2019-10-21 15:52:29 -04:00
|
|
|
import { transformText } from './transforms/transformText'
|
2019-10-09 17:32:58 -04:00
|
|
|
import { transformOnce } from './transforms/vOnce'
|
2019-10-10 11:15:24 -04:00
|
|
|
import { transformModel } from './transforms/vModel'
|
2019-09-20 12:16:19 -04:00
|
|
|
|
|
|
|
export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions
|
|
|
|
|
2019-09-26 11:51:04 -04:00
|
|
|
// 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-21 15:47:26 -04:00
|
|
|
template: string | RootNode,
|
2019-09-20 12:16:19 -04:00
|
|
|
options: CompilerOptions = {}
|
|
|
|
): CodegenResult {
|
2019-09-28 14:15:10 -04:00
|
|
|
/* istanbul ignore if */
|
2019-09-25 22:29:37 -04: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-23 13:25:18 -04:00
|
|
|
}
|
2019-09-20 12:16:19 -04:00
|
|
|
|
2019-09-24 22:39:20 -04:00
|
|
|
const ast = isString(template) ? parse(template, options) : template
|
|
|
|
|
2019-09-25 22:29:37 -04:00
|
|
|
const prefixIdentifiers =
|
|
|
|
!__BROWSER__ &&
|
|
|
|
(options.prefixIdentifiers === true || options.mode === 'module')
|
|
|
|
|
2019-09-20 12:16:19 -04:00
|
|
|
transform(ast, {
|
|
|
|
...options,
|
2019-09-24 22:50:00 -04:00
|
|
|
prefixIdentifiers,
|
2019-09-21 17:42:12 -04:00
|
|
|
nodeTransforms: [
|
2019-10-23 17:57:40 -04:00
|
|
|
transformOnce,
|
2019-09-22 16:50:57 -04:00
|
|
|
transformIf,
|
|
|
|
transformFor,
|
2019-10-02 23:10:41 -04:00
|
|
|
...(prefixIdentifiers
|
|
|
|
? [
|
|
|
|
// order is important
|
|
|
|
trackVForSlotScopes,
|
2019-10-03 16:27:46 -04:00
|
|
|
transformExpression
|
2019-10-02 23:10:41 -04:00
|
|
|
]
|
|
|
|
: []),
|
2019-09-28 00:19:24 -04:00
|
|
|
transformSlotOutlet,
|
2019-09-23 20:45:40 -04:00
|
|
|
transformElement,
|
2019-10-16 15:30:21 -04:00
|
|
|
trackSlotScopes,
|
2019-10-21 15:52:29 -04:00
|
|
|
transformText,
|
2019-09-21 17:42:12 -04:00
|
|
|
...(options.nodeTransforms || []) // user transforms
|
|
|
|
],
|
|
|
|
directiveTransforms: {
|
2019-09-22 22:19:42 -04:00
|
|
|
on: transformOn,
|
|
|
|
bind: transformBind,
|
2019-10-10 11:15:24 -04:00
|
|
|
model: transformModel,
|
2019-09-21 17:42:12 -04:00
|
|
|
...(options.directiveTransforms || {}) // user transforms
|
|
|
|
}
|
2019-09-20 12:16:19 -04:00
|
|
|
})
|
2019-09-24 22:50:00 -04:00
|
|
|
|
2019-10-05 17:18:25 -04:00
|
|
|
return generate(ast, {
|
|
|
|
...options,
|
|
|
|
prefixIdentifiers
|
|
|
|
})
|
2019-09-20 12:16:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Also expose lower level APIs & types
|
2019-09-17 19:08:47 -04:00
|
|
|
export { parse, ParserOptions, TextModes } from './parse'
|
2019-09-20 00:12:37 -04:00
|
|
|
export {
|
|
|
|
transform,
|
2019-09-21 17:42:12 -04:00
|
|
|
createStructuralDirectiveTransform,
|
2019-09-20 00:12:37 -04:00
|
|
|
TransformOptions,
|
2019-09-21 15:47:26 -04:00
|
|
|
TransformContext,
|
2019-09-25 14:13:33 -04:00
|
|
|
NodeTransform,
|
2019-10-08 15:35:57 -04:00
|
|
|
StructuralDirectiveTransform,
|
|
|
|
DirectiveTransform
|
2019-09-20 00:12:37 -04:00
|
|
|
} from './transform'
|
2019-09-21 15:47:26 -04:00
|
|
|
export {
|
|
|
|
generate,
|
|
|
|
CodegenOptions,
|
|
|
|
CodegenContext,
|
|
|
|
CodegenResult
|
|
|
|
} from './codegen'
|
2019-10-09 11:13:13 -04:00
|
|
|
export {
|
|
|
|
ErrorCodes,
|
|
|
|
CoreCompilerError,
|
|
|
|
CompilerError,
|
|
|
|
createCompilerError
|
|
|
|
} from './errors'
|
2019-09-16 14:43:29 -04:00
|
|
|
export * from './ast'
|
2019-10-10 18:02:51 -04:00
|
|
|
export * from './utils'
|
|
|
|
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'
|