mirror of https://github.com/vuejs/core.git
121 lines
3.7 KiB
TypeScript
121 lines
3.7 KiB
TypeScript
import type { CompilerOptions } from './options'
|
|
import { baseParse } from './parser'
|
|
import {
|
|
type DirectiveTransform,
|
|
type NodeTransform,
|
|
transform,
|
|
} from './transform'
|
|
import { type CodegenResult, generate } from './codegen'
|
|
import type { RootNode } from './ast'
|
|
import { extend, isString } from '@vue/shared'
|
|
import { transformIf } from './transforms/vIf'
|
|
import { transformFor } from './transforms/vFor'
|
|
import { transformExpression } from './transforms/transformExpression'
|
|
import { transformSlotOutlet } from './transforms/transformSlotOutlet'
|
|
import { transformElement } from './transforms/transformElement'
|
|
import { transformOn } from './transforms/vOn'
|
|
import { transformBind } from './transforms/vBind'
|
|
import { trackSlotScopes, trackVForSlotScopes } from './transforms/vSlot'
|
|
import { transformText } from './transforms/transformText'
|
|
import { transformOnce } from './transforms/vOnce'
|
|
import { transformModel } from './transforms/vModel'
|
|
import { transformFilter } from './compat/transformFilter'
|
|
import { ErrorCodes, createCompilerError, defaultOnError } from './errors'
|
|
import { transformMemo } from './transforms/vMemo'
|
|
|
|
export type TransformPreset = [
|
|
NodeTransform[],
|
|
Record<string, DirectiveTransform>,
|
|
]
|
|
|
|
export function getBaseTransformPreset(
|
|
prefixIdentifiers?: boolean,
|
|
): TransformPreset {
|
|
return [
|
|
[
|
|
transformOnce,
|
|
transformIf,
|
|
transformMemo,
|
|
transformFor,
|
|
...(__COMPAT__ ? [transformFilter] : []),
|
|
...(!__BROWSER__ && prefixIdentifiers
|
|
? [
|
|
// order is important
|
|
trackVForSlotScopes,
|
|
transformExpression,
|
|
]
|
|
: __BROWSER__ && __DEV__
|
|
? [transformExpression]
|
|
: []),
|
|
transformSlotOutlet,
|
|
transformElement,
|
|
trackSlotScopes,
|
|
transformText,
|
|
],
|
|
{
|
|
on: transformOn,
|
|
bind: transformBind,
|
|
model: transformModel,
|
|
},
|
|
]
|
|
}
|
|
|
|
// we name it `baseCompile` so that higher order compilers like
|
|
// @vue/compiler-dom can export `compile` while re-exporting everything else.
|
|
export function baseCompile(
|
|
source: string | RootNode,
|
|
options: CompilerOptions = {},
|
|
): CodegenResult {
|
|
const onError = options.onError || defaultOnError
|
|
const isModuleMode = options.mode === 'module'
|
|
/* v8 ignore start */
|
|
if (__BROWSER__) {
|
|
if (options.prefixIdentifiers === true) {
|
|
onError(createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED))
|
|
} else if (isModuleMode) {
|
|
onError(createCompilerError(ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED))
|
|
}
|
|
}
|
|
/* v8 ignore stop */
|
|
|
|
const prefixIdentifiers =
|
|
!__BROWSER__ && (options.prefixIdentifiers === true || isModuleMode)
|
|
if (!prefixIdentifiers && options.cacheHandlers) {
|
|
onError(createCompilerError(ErrorCodes.X_CACHE_HANDLER_NOT_SUPPORTED))
|
|
}
|
|
if (options.scopeId && !isModuleMode) {
|
|
onError(createCompilerError(ErrorCodes.X_SCOPE_ID_NOT_SUPPORTED))
|
|
}
|
|
|
|
const resolvedOptions = extend({}, options, {
|
|
prefixIdentifiers,
|
|
})
|
|
const ast = isString(source) ? baseParse(source, resolvedOptions) : source
|
|
const [nodeTransforms, directiveTransforms] =
|
|
getBaseTransformPreset(prefixIdentifiers)
|
|
|
|
if (!__BROWSER__ && options.isTS) {
|
|
const { expressionPlugins } = options
|
|
if (!expressionPlugins || !expressionPlugins.includes('typescript')) {
|
|
options.expressionPlugins = [...(expressionPlugins || []), 'typescript']
|
|
}
|
|
}
|
|
|
|
transform(
|
|
ast,
|
|
extend({}, resolvedOptions, {
|
|
nodeTransforms: [
|
|
...nodeTransforms,
|
|
...(options.nodeTransforms || []), // user transforms
|
|
],
|
|
directiveTransforms: extend(
|
|
{},
|
|
directiveTransforms,
|
|
options.directiveTransforms || {}, // user transforms
|
|
),
|
|
}),
|
|
)
|
|
|
|
return generate(ast, resolvedOptions)
|
|
}
|