From acd7eb22cfa298ce9cc75e9876e625fa0ff20bd1 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 11 Apr 2023 13:06:04 +0800 Subject: [PATCH] refactor(compiler-sfc): (wip) split compileScript logic, use context, move defineProps --- packages/compiler-sfc/src/compileScript.ts | 411 +++++------------- packages/compiler-sfc/src/script/context.ts | 153 +++++++ .../compiler-sfc/src/script/defineProps.ts | 148 +++++++ ...structure.ts => definePropsDestructure.ts} | 0 .../compiler-sfc/src/script/resolveType.ts | 114 +++++ packages/compiler-sfc/src/script/utils.ts | 14 + 6 files changed, 541 insertions(+), 299 deletions(-) create mode 100644 packages/compiler-sfc/src/script/context.ts create mode 100644 packages/compiler-sfc/src/script/defineProps.ts rename packages/compiler-sfc/src/script/{propsDestructure.ts => definePropsDestructure.ts} (100%) create mode 100644 packages/compiler-sfc/src/script/resolveType.ts create mode 100644 packages/compiler-sfc/src/script/utils.ts diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 575ce6caa..5dacc84d5 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -15,12 +15,7 @@ import { isCallOf } from '@vue/compiler-dom' import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse' -import { - parse as _parse, - parseExpression, - ParserOptions, - ParserPlugin -} from '@babel/parser' +import { parse as _parse, parseExpression, ParserPlugin } from '@babel/parser' import { camelize, capitalize, generateCodeFrame, makeMap } from '@vue/shared' import { Node, @@ -41,7 +36,6 @@ import { TSInterfaceBody, TSTypeElement, AwaitExpression, - Program, ObjectMethod, LVal, Expression, @@ -59,13 +53,19 @@ import { warnOnce } from './warn' import { rewriteDefaultAST } from './rewriteDefault' import { createCache } from './cache' import { shouldTransform, transformAST } from '@vue/reactivity-transform' -import { transformDestructuredProps } from './script/propsDestructure' +import { transformDestructuredProps } from './script/definePropsDestructure' +import { resolveObjectKey, FromNormalScript } from './script/utils' +import { ScriptCompileContext } from './script/context' +import { + processDefineProps, + DEFINE_PROPS, + WITH_DEFAULTS, + PropsDeclType +} from './script/defineProps' // Special compiler macros -const DEFINE_PROPS = 'defineProps' const DEFINE_EMITS = 'defineEmits' const DEFINE_EXPOSE = 'defineExpose' -const WITH_DEFAULTS = 'withDefaults' const DEFINE_OPTIONS = 'defineOptions' const DEFINE_SLOTS = 'defineSlots' const DEFINE_MODEL = 'defineModel' @@ -149,8 +149,6 @@ export type PropsDestructureBindings = Record< } > -type FromNormalScript = T & { __fromNormalScript?: boolean | null } -type PropsDeclType = FromNormalScript type EmitsDeclType = FromNormalScript< TSFunctionType | TSTypeLiteral | TSInterfaceBody > @@ -195,41 +193,15 @@ export function compileScript( ? `const ${options.genDefaultAs} =` : `export default` const normalScriptDefaultVar = `__default__` - const isJS = - scriptLang === 'js' || - scriptLang === 'jsx' || - scriptSetupLang === 'js' || - scriptSetupLang === 'jsx' - const isTS = - scriptLang === 'ts' || - scriptLang === 'tsx' || - scriptSetupLang === 'ts' || - scriptSetupLang === 'tsx' - // resolve parser plugins - const plugins: ParserPlugin[] = [] - if (!isTS || scriptLang === 'tsx' || scriptSetupLang === 'tsx') { - plugins.push('jsx') - } else { - // If don't match the case of adding jsx, should remove the jsx from the babelParserPlugins - if (options.babelParserPlugins) - options.babelParserPlugins = options.babelParserPlugins.filter( - n => n !== 'jsx' - ) - } - if (options.babelParserPlugins) plugins.push(...options.babelParserPlugins) - if (isTS) { - plugins.push('typescript') - if (!plugins.includes('decorators')) { - plugins.push('decorators-legacy') - } - } + const ctx = new ScriptCompileContext(sfc, options) + const { isTS } = ctx if (!scriptSetup) { if (!script) { throw new Error(`[@vue/compiler-sfc] SFC contains no