Skip to content

Commit 4792ebd

Browse files
authored
fix(compiler-sfc): fix 'export default' rewrite with extra whitespaces (#4375)
1 parent 9043d0d commit 4792ebd

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+42
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,48 @@ return { x }
3333
export const n = 1"
3434
`;
3535
36+
exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces in ExportDefaultDeclaration node with many spaces and newline 1`] = `
37+
"import { x } from './x'
38+
39+
export const n = 1
40+
const __default__ = {
41+
some:'option'
42+
}
43+
44+
function setup(__props, { expose }) {
45+
46+
x()
47+
48+
return { x }
49+
}
50+
51+
52+
export default /*#__PURE__*/ Object.assign(__default__, {
53+
setup
54+
})"
55+
`;
56+
57+
exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces in ExportDefaultDeclaration node with minimal spaces 1`] = `
58+
"import { x } from './x'
59+
60+
export const n = 1
61+
const __default__ = {
62+
some:'option'
63+
}
64+
65+
function setup(__props, { expose }) {
66+
67+
x()
68+
69+
return { x }
70+
}
71+
72+
73+
export default /*#__PURE__*/ Object.assign(__default__, {
74+
setup
75+
})"
76+
`;
77+
3678
exports[`SFC compile <script setup> defineEmits() 1`] = `
3779
"export default {
3880
emits: ['foo', 'bar'],

packages/compiler-sfc/__tests__/compileScript.spec.ts

+37
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,43 @@ defineExpose({ foo: 123 })
140140
})
141141

142142
describe('<script> and <script setup> co-usage', () => {
143+
describe('spaces in ExportDefaultDeclaration node', () => {
144+
// #4371
145+
test('with many spaces and newline', () => {
146+
// #4371
147+
const { content } = compile(`
148+
<script>
149+
export const n = 1
150+
export default
151+
{
152+
some:'option'
153+
}
154+
</script>
155+
<script setup>
156+
import { x } from './x'
157+
x()
158+
</script>
159+
`)
160+
assertCode(content)
161+
})
162+
163+
test('with minimal spaces', () => {
164+
const { content } = compile(`
165+
<script>
166+
export const n = 1
167+
export default{
168+
some:'option'
169+
}
170+
</script>
171+
<script setup>
172+
import { x } from './x'
173+
x()
174+
</script>
175+
`)
176+
assertCode(content)
177+
})
178+
})
179+
143180
test('script first', () => {
144181
const { content } = compile(`
145182
<script>

packages/compiler-sfc/src/compileScript.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,8 @@ export function compileScript(
605605
// export default
606606
defaultExport = node
607607
const start = node.start! + scriptStartOffset!
608-
s.overwrite(
609-
start,
610-
start + `export default`.length,
611-
`const ${defaultTempVar} =`
612-
)
608+
const end = node.declaration.start! + scriptStartOffset!
609+
s.overwrite(start, end, `const ${defaultTempVar} = `)
613610
} else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
614611
const defaultSpecifier = node.specifiers.find(
615612
s => s.exported.type === 'Identifier' && s.exported.name === 'default'

0 commit comments

Comments
 (0)