From 8cbe4466291d2bf890269e72528290608ce08695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Thu, 29 Dec 2022 15:12:40 +0800 Subject: [PATCH] feat(compiler-sfc): support module string names syntax https://github.com/tc39/ecma262/pull/2154 --- packages/compiler-sfc/src/compileScript.ts | 36 +++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index b7e4c0ea778..c8823aeda5f 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -42,7 +42,10 @@ import { ObjectMethod, LVal, Expression, - VariableDeclaration + VariableDeclaration, + ImportSpecifier, + ImportDefaultSpecifier, + ImportNamespaceSpecifier } from '@babel/types' import { walk } from 'estree-walker' import { RawSourceMap } from 'source-map' @@ -377,10 +380,24 @@ export function compileScript( s.move(start, end, 0) } + function getImported( + specifier: + | ImportSpecifier + | ImportDefaultSpecifier + | ImportNamespaceSpecifier + ) { + if (specifier.type === 'ImportSpecifier') + return specifier.imported.type === 'Identifier' + ? specifier.imported.name + : specifier.imported.value + else if (specifier.type === 'ImportNamespaceSpecifier') return '*' + return 'default' + } + function registerUserImport( source: string, local: string, - imported: string | false, + imported: string, isType: boolean, isFromSetup: boolean, needTemplateUsageCheck: boolean @@ -400,7 +417,7 @@ export function compileScript( userImports[local] = { isType, - imported: imported || 'default', + imported, local, source, isFromSetup, @@ -957,10 +974,7 @@ export function compileScript( if (node.type === 'ImportDeclaration') { // record imports for dedupe for (const specifier of node.specifiers) { - const imported = - specifier.type === 'ImportSpecifier' && - specifier.imported.type === 'Identifier' && - specifier.imported.name + const imported = getImported(specifier) registerUserImport( node.source.value, specifier.local.name, @@ -1002,13 +1016,7 @@ export function compileScript( for (let i = 0; i < node.specifiers.length; i++) { const specifier = node.specifiers[i] const local = specifier.local.name - let imported = - specifier.type === 'ImportSpecifier' && - specifier.imported.type === 'Identifier' && - specifier.imported.name - if (specifier.type === 'ImportNamespaceSpecifier') { - imported = '*' - } + const imported = getImported(specifier) const source = node.source.value const existing = userImports[local] if (