wip: export { x as default } handling

This commit is contained in:
Evan You 2020-07-08 17:21:39 -04:00
parent a44d53003e
commit e4df2d7749

View File

@ -10,6 +10,7 @@ import {
Identifier, Identifier,
ExpressionStatement, ExpressionStatement,
ArrowFunctionExpression, ArrowFunctionExpression,
ExportSpecifier,
TSTypeLiteral, TSTypeLiteral,
TSFunctionType, TSFunctionType,
TSDeclareFunction TSDeclareFunction
@ -90,18 +91,38 @@ export function compileScriptSetup(
start + `export default`.length, start + `export default`.length,
`const __default__ =` `const __default__ =`
) )
} else if ( } else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
node.type === 'ExportNamedDeclaration' && const defaultSpecifier = node.specifiers.find(
node.specifiers && s => s.exported.name === 'default'
node.specifiers.some(s => s.exported.name === 'default') ) as ExportSpecifier
) { if (defaultSpecifier) {
defaultExport = node defaultExport = node
if (node.source) { // 1. remove specifier
// export { x as default } from './x' if (node.specifiers.length > 1) {
// TODO s.remove(
} else { defaultSpecifier.start! + scriptStartOffset!,
// export { x as default } defaultSpecifier.end! + scriptStartOffset!
// TODO )
} else {
s.remove(
node.start! + scriptStartOffset!,
node.end! + scriptStartOffset!
)
}
if (node.source) {
// export { x as default } from './x'
// rewrite to `import { x as __default__ } from './x'` and
// add to top
s.prepend(
`import { ${defaultSpecifier.local.name} as __default__ } from '${
node.source.value
}'\n`
)
} else {
// export { x as default }
// rewrite to `const __default__ = x` and move to end
s.append(`\nconst __default__ = ${defaultSpecifier.local.name}\n`)
}
} }
} }
} }
@ -230,22 +251,39 @@ export function compileScriptSetup(
} else if (specifier.type == 'ExportSpecifier') { } else if (specifier.type == 'ExportSpecifier') {
if (specifier.exported.name === 'default') { if (specifier.exported.name === 'default') {
defaultExport = node defaultExport = node
if (!node.source) { // 1. remove specifier
// export { x as default } if (node.specifiers.length > 1) {
// rewrite to `const __default__ = x` s.remove(
s.overwrite( specifier.start! + startOffset,
start, specifier.end! + startOffset
end,
`const __default__ = ${specifier.local.name}\n`
) )
s.move(start, end, source.length) } else {
s.remove(node.start! + startOffset!, node.end! + startOffset!)
}
if (!node.source) {
// export { x as default, ... }
const local = specifier.local.name
if (setupScopeVars[local] || setupExports[local]) {
throw new Error(
`Cannot export locally defined variable as default in <script setup>.\n` +
`Default export must be an object literal with no reference to local scope.\n` +
generateCodeFrame(
source,
specifier.start! + startOffset,
specifier.end! + startOffset
)
)
}
// rewrite to `const __default__ = x` and move to end
s.append(`\nconst __default__ = ${local}\n`)
} else { } else {
// export { x as default } from './x' // export { x as default } from './x'
// rewrite to `import { x as __default__ } from './x'` // rewrite to `import { x as __default__ } from './x'` and
s.overwrite( // add to top
specifier.exported.start! + startOffset, s.prepend(
specifier.exported.start! + startOffset + 7, `import { ${specifier.local.name} as __default__ } from '${
'__default__' node.source.value
}'\n`
) )
} }
} else { } else {
@ -261,7 +299,7 @@ export function compileScriptSetup(
s.overwrite( s.overwrite(
start, start,
node.source.start! + startOffset, node.source.start! + startOffset,
`import * as __import_all_${exportAllIndex++}__ from ` `import * as __export_all_${exportAllIndex++}__ from `
) )
s.move(start, end, 0) s.move(start, end, 0)
} }