fix(compiler-sfc): handle more edge cases in default rewrite
This commit is contained in:
parent
012dc5a303
commit
1dedc19e1f
@ -26,4 +26,55 @@ describe('compiler sfc: rewriteDefault', () => {
|
|||||||
const script = a"
|
const script = a"
|
||||||
`)
|
`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('w/ comments', async () => {
|
||||||
|
expect(rewriteDefault(`// export default\nexport default {}`, 'script'))
|
||||||
|
.toMatchInlineSnapshot(`
|
||||||
|
"// export default
|
||||||
|
const script = {}"
|
||||||
|
`)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('export default class', async () => {
|
||||||
|
expect(rewriteDefault(`export default class Foo {}`, 'script'))
|
||||||
|
.toMatchInlineSnapshot(`
|
||||||
|
"class Foo {}
|
||||||
|
const script = Foo"
|
||||||
|
`)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('export default class w/ comments', async () => {
|
||||||
|
expect(
|
||||||
|
rewriteDefault(`// export default\nexport default class Foo {}`, 'script')
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
|
"// export default
|
||||||
|
class Foo {}
|
||||||
|
const script = Foo"
|
||||||
|
`)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('export default class w/ comments 2', async () => {
|
||||||
|
expect(
|
||||||
|
rewriteDefault(
|
||||||
|
`export default {}\n` + `// export default class Foo {}`,
|
||||||
|
'script'
|
||||||
|
)
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
|
"const script = {}
|
||||||
|
// export default class Foo {}"
|
||||||
|
`)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('export default class w/ comments 3', async () => {
|
||||||
|
expect(
|
||||||
|
rewriteDefault(
|
||||||
|
`/*\nexport default class Foo {}*/\n` + `export default class Bar {}`,
|
||||||
|
'script'
|
||||||
|
)
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
|
"/*
|
||||||
|
export default class Foo {}*/
|
||||||
|
const script = class Bar {}"
|
||||||
|
`)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -3,6 +3,7 @@ import MagicString from 'magic-string'
|
|||||||
|
|
||||||
const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/
|
const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/
|
||||||
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)as(\s*)default/
|
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)as(\s*)default/
|
||||||
|
const exportDefaultClassRE = /((?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility for rewriting `export default` in a script block into a variable
|
* Utility for rewriting `export default` in a script block into a variable
|
||||||
@ -17,7 +18,16 @@ export function rewriteDefault(
|
|||||||
return input + `\nconst ${as} = {}`
|
return input + `\nconst ${as} = {}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const replaced = input.replace(defaultExportRE, `$1const ${as} =`)
|
let replaced: string | undefined
|
||||||
|
|
||||||
|
const classMatch = input.match(exportDefaultClassRE)
|
||||||
|
if (classMatch) {
|
||||||
|
replaced =
|
||||||
|
input.replace(exportDefaultClassRE, '$1class $2') +
|
||||||
|
`\nconst ${as} = ${classMatch[2]}`
|
||||||
|
} else {
|
||||||
|
replaced = input.replace(defaultExportRE, `$1const ${as} =`)
|
||||||
|
}
|
||||||
if (!hasDefaultExport(replaced)) {
|
if (!hasDefaultExport(replaced)) {
|
||||||
return replaced
|
return replaced
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user