From dc098c7f817cd2e6abc30cbcdf5ba2100c719908 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 11 Nov 2020 19:40:27 -0500 Subject: [PATCH] wip: defineContext --- packages/compiler-sfc/src/compileScript.ts | 318 +++++++++--------- .../src/helpers/useSetupContext.ts | 15 + packages/runtime-core/src/index.ts | 2 + 3 files changed, 171 insertions(+), 164 deletions(-) create mode 100644 packages/runtime-core/src/helpers/useSetupContext.ts diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 89781961..ebb60a29 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -10,8 +10,6 @@ import { ObjectExpression, ArrayPattern, Identifier, - ExpressionStatement, - ArrowFunctionExpression, ExportSpecifier, Function as FunctionNode, TSType, @@ -29,6 +27,8 @@ import { RawSourceMap } from 'source-map' import { genCssVarsCode, injectCssVarsCalls } from './genCssVars' import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate' +const CTX_FN_NAME = 'defineContext' + export interface SFCScriptCompileOptions { /** * https://babeljs.io/docs/en/babel-parser#plugins @@ -127,13 +127,21 @@ export function compileScript( const defaultTempVar = `__default__` const bindingMetadata: BindingMetadata = {} const helperImports: Set = new Set() - const userImports: Record = Object.create(null) + const userImports: Record< + string, + { + imported: string | null + source: string + } + > = Object.create(null) const setupBindings: Record = Object.create(null) const refBindings: Record = Object.create(null) const refIdentifiers: Set = new Set() const enableRefSugar = options.refSugar !== false let defaultExport: Node | undefined - let needDefaultExportRefCheck = false + let setupContextExp: string | undefined + let setupContextArg: Node | undefined + let setupContextType: TSTypeLiteral | undefined let hasAwait = false const s = new MagicString(source) @@ -314,10 +322,16 @@ export function compileScript( for (const node of scriptAst) { if (node.type === 'ImportDeclaration') { // record imports for dedupe - for (const { - local: { name } - } of node.specifiers) { - userImports[name] = node.source.value + for (const specifier of node.specifiers) { + const name = specifier.local.name + const imported = + specifier.type === 'ImportSpecifier' && + specifier.imported.type === 'Identifier' && + specifier.imported.name + userImports[name] = { + imported: imported || null, + source: node.source.value + } } } else if (node.type === 'ExportDefaultDeclaration') { // export default @@ -367,75 +381,17 @@ export function compileScript( } } - // 2. check