Skip to content

Commit c6dc7b7

Browse files
committed
feat(compiler-sfc): support module string names syntax
tc39/ecma262#2154
1 parent c6e5bda commit c6dc7b7

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

packages/compiler-sfc/src/compileScript.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ import {
4242
ObjectMethod,
4343
LVal,
4444
Expression,
45-
VariableDeclaration
45+
VariableDeclaration,
46+
ImportSpecifier,
47+
ImportDefaultSpecifier,
48+
ImportNamespaceSpecifier
4649
} from '@babel/types'
4750
import { walk } from 'estree-walker'
4851
import { RawSourceMap } from 'source-map'
@@ -375,10 +378,24 @@ export function compileScript(
375378
s.move(start, end, 0)
376379
}
377380

381+
function getImported(
382+
specifier:
383+
| ImportSpecifier
384+
| ImportDefaultSpecifier
385+
| ImportNamespaceSpecifier
386+
) {
387+
if (specifier.type === 'ImportSpecifier')
388+
return specifier.imported.type === 'Identifier'
389+
? specifier.imported.name
390+
: specifier.imported.value
391+
else if (specifier.type === 'ImportNamespaceSpecifier') return '*'
392+
return 'default'
393+
}
394+
378395
function registerUserImport(
379396
source: string,
380397
local: string,
381-
imported: string | false,
398+
imported: string,
382399
isType: boolean,
383400
isFromSetup: boolean,
384401
needTemplateUsageCheck: boolean
@@ -398,7 +415,7 @@ export function compileScript(
398415

399416
userImports[local] = {
400417
isType,
401-
imported: imported || 'default',
418+
imported,
402419
local,
403420
source,
404421
isFromSetup,
@@ -951,10 +968,7 @@ export function compileScript(
951968
if (node.type === 'ImportDeclaration') {
952969
// record imports for dedupe
953970
for (const specifier of node.specifiers) {
954-
const imported =
955-
specifier.type === 'ImportSpecifier' &&
956-
specifier.imported.type === 'Identifier' &&
957-
specifier.imported.name
971+
const imported = getImported(specifier)
958972
registerUserImport(
959973
node.source.value,
960974
specifier.local.name,
@@ -996,13 +1010,7 @@ export function compileScript(
9961010
for (let i = 0; i < node.specifiers.length; i++) {
9971011
const specifier = node.specifiers[i]
9981012
const local = specifier.local.name
999-
let imported =
1000-
specifier.type === 'ImportSpecifier' &&
1001-
specifier.imported.type === 'Identifier' &&
1002-
specifier.imported.name
1003-
if (specifier.type === 'ImportNamespaceSpecifier') {
1004-
imported = '*'
1005-
}
1013+
const imported = getImported(specifier)
10061014
const source = node.source.value
10071015
const existing = userImports[local]
10081016
if (

0 commit comments

Comments
 (0)