Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): fix 'export default' with extra spaces transformation #4375

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -33,6 +33,48 @@ return { x }
export const n = 1"
`;

exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces in ExportDefaultDeclaration node with many spaces and newline 1`] = `
"import { x } from './x'

export const n = 1
const __default__ = {
some:'option'
}

function setup(__props, { expose }) {

x()

return { x }
}


export default /*#__PURE__*/ Object.assign(__default__, {
setup
})"
`;

exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces in ExportDefaultDeclaration node with minimal spaces 1`] = `
"import { x } from './x'

export const n = 1
const __default__ = {
some:'option'
}

function setup(__props, { expose }) {

x()

return { x }
}


export default /*#__PURE__*/ Object.assign(__default__, {
setup
})"
`;

exports[`SFC compile <script setup> defineEmits() 1`] = `
"export default {
emits: ['foo', 'bar'],
Expand Down
37 changes: 37 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -140,6 +140,43 @@ defineExpose({ foo: 123 })
})

describe('<script> and <script setup> co-usage', () => {
describe('spaces in ExportDefaultDeclaration node', () => {
// #4371
test('with many spaces and newline', () => {
// #4371
const { content } = compile(`
<script>
export const n = 1
export default
{
some:'option'
}
</script>
<script setup>
import { x } from './x'
x()
</script>
`)
assertCode(content)
})

test('with minimal spaces', () => {
const { content } = compile(`
<script>
export const n = 1
export default{
some:'option'
}
</script>
<script setup>
import { x } from './x'
x()
</script>
`)
assertCode(content)
})
})

test('script first', () => {
const { content } = compile(`
<script>
Expand Down
7 changes: 2 additions & 5 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -789,11 +789,8 @@ export function compileScript(
// export default
defaultExport = node
const start = node.start! + scriptStartOffset!
s.overwrite(
start,
start + `export default`.length,
`const ${defaultTempVar} =`
)
const end = node.declaration.start! + scriptStartOffset!
s.overwrite(start, end, `const ${defaultTempVar} = `)
} else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
const defaultSpecifier = node.specifiers.find(
s => s.exported.type === 'Identifier' && s.exported.name === 'default'
Expand Down