Skip to content

Commit

Permalink
feat(compiler-sfc): support module string names syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Dec 29, 2022
1 parent c6e5bda commit c6dc7b7
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -375,10 +378,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
Expand All @@ -398,7 +415,7 @@ export function compileScript(

userImports[local] = {
isType,
imported: imported || 'default',
imported,
local,
source,
isFromSetup,
Expand Down Expand Up @@ -951,10 +968,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,
Expand Down Expand Up @@ -996,13 +1010,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 (
Expand Down

0 comments on commit c6dc7b7

Please sign in to comment.