wip: do not return type imports

This commit is contained in:
Evan You 2020-11-19 12:17:39 -05:00
parent 09c861d392
commit 03b2bfff09

View File

@ -156,6 +156,7 @@ export function compileScript(
const userImports: Record< const userImports: Record<
string, string,
{ {
isType: boolean
imported: string | null imported: string | null
source: string source: string
} }
@ -202,11 +203,9 @@ export function compileScript(
try { try {
return _parse(input, options).program.body return _parse(input, options).program.body
} catch (e) { } catch (e) {
e.message = `[@vue/compiler-sfc] ${e.message}\n\n${generateCodeFrame( e.message = `[@vue/compiler-sfc] ${e.message}\n\n${
source, sfc.filename
e.pos + offset, }\n${generateCodeFrame(source, e.pos + offset, e.pos + offset + 1)}`
e.pos + offset + 1
)}`
throw e throw e
} }
} }
@ -217,20 +216,25 @@ export function compileScript(
end: number = node.end! + startOffset end: number = node.end! + startOffset
) { ) {
throw new Error( throw new Error(
`[@vue/compiler-sfc] ${msg}\n\n` + `[@vue/compiler-sfc] ${msg}\n\n${sfc.filename}\n${generateCodeFrame(
generateCodeFrame(source, node.start! + startOffset, end) source,
node.start! + startOffset,
end
)}`
) )
} }
function registerUserImport( function registerUserImport(
source: string, source: string,
local: string, local: string,
imported: string | false imported: string | false,
isType: boolean
) { ) {
if (source === 'vue' && imported) { if (source === 'vue' && imported) {
userImportAlias[imported] = local userImportAlias[imported] = local
} }
userImports[local] = { userImports[local] = {
isType,
imported: imported || null, imported: imported || null,
source source
} }
@ -425,7 +429,12 @@ export function compileScript(
specifier.type === 'ImportSpecifier' && specifier.type === 'ImportSpecifier' &&
specifier.imported.type === 'Identifier' && specifier.imported.type === 'Identifier' &&
specifier.imported.name specifier.imported.name
registerUserImport(node.source.value, specifier.local.name, imported) registerUserImport(
node.source.value,
specifier.local.name,
imported,
node.importKind === 'type'
)
} }
} else if (node.type === 'ExportDefaultDeclaration') { } else if (node.type === 'ExportDefaultDeclaration') {
// export default // export default
@ -575,7 +584,12 @@ export function compileScript(
error(`different imports aliased to same local name.`, specifier) error(`different imports aliased to same local name.`, specifier)
} }
} else { } else {
registerUserImport(source, local, imported) registerUserImport(
source,
local,
imported,
node.importKind === 'type'
)
} }
} }
if (removed === node.specifiers.length) { if (removed === node.specifiers.length) {
@ -790,7 +804,8 @@ export function compileScript(
if (optionsArg) { if (optionsArg) {
Object.assign(bindingMetadata, analyzeBindingsFromOptions(optionsArg)) Object.assign(bindingMetadata, analyzeBindingsFromOptions(optionsArg))
} }
for (const [key, { source }] of Object.entries(userImports)) { for (const [key, { isType, source }] of Object.entries(userImports)) {
if (isType) continue
bindingMetadata[key] = bindingMetadata[key] =
source.endsWith('.vue') || source === 'vue' source.endsWith('.vue') || source === 'vue'
? BindingTypes.SETUP_CONST ? BindingTypes.SETUP_CONST
@ -841,7 +856,9 @@ export function compileScript(
} else if (err) { } else if (err) {
if (err.loc) { if (err.loc) {
err.message += err.message +=
`\n` + `\n\n` +
sfc.filename +
'\n' +
generateCodeFrame( generateCodeFrame(
source, source,
err.loc.start.offset, err.loc.start.offset,
@ -868,8 +885,10 @@ export function compileScript(
// return bindings from setup // return bindings from setup
const allBindings: Record<string, any> = { ...setupBindings } const allBindings: Record<string, any> = { ...setupBindings }
for (const key in userImports) { for (const key in userImports) {
if (!userImports[key].isType) {
allBindings[key] = true allBindings[key] = true
} }
}
returned = `{ ${Object.keys(allBindings).join(', ')} }` returned = `{ ${Object.keys(allBindings).join(', ')} }`
} }
s.appendRight(endOffset, `\nreturn ${returned}\n}\n\n`) s.appendRight(endOffset, `\nreturn ${returned}\n}\n\n`)