Skip to content

Commit 9f38f96

Browse files
committed
fix: breaking change
1 parent 77e470f commit 9f38f96

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function compileRewriteDefault(code: string, as: string) {
55
sourceType: 'module',
66
plugins: ['typescript', 'decorators-legacy']
77
})
8-
return rewriteDefault(code, ast.program.body, as)
8+
return rewriteDefault(code, as, undefined, ast.program.body)
99
}
1010

1111
describe('compiler sfc: rewriteDefault', () => {

packages/compiler-sfc/src/compileScript.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,12 @@ export function compileScript(
235235
}
236236
}
237237
if (cssVars.length) {
238-
content = rewriteDefault(content, scriptAst.body, DEFAULT_VAR)
238+
content = rewriteDefault(
239+
content,
240+
DEFAULT_VAR,
241+
undefined,
242+
scriptAst.body
243+
)
239244
content += genNormalScriptCssVarsCode(
240245
cssVars,
241246
bindings,

packages/compiler-sfc/src/rewriteDefault.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { parse } from '@babel/parser'
12
import MagicString from 'magic-string'
3+
import type { ParserPlugin } from '@babel/parser'
24
import type { Identifier, Statement } from '@babel/types'
35

46
/**
@@ -7,9 +9,17 @@ import type { Identifier, Statement } from '@babel/types'
79
*/
810
export function rewriteDefault(
911
input: string,
10-
ast: Statement[],
11-
as: string
12+
as: string,
13+
/** @deprecated use `ast` param instead */
14+
parserPlugins?: ParserPlugin[],
15+
ast?: Statement[]
1216
): string {
17+
if (!ast)
18+
ast = parse(input, {
19+
sourceType: 'module',
20+
plugins: parserPlugins
21+
}).program.body
22+
1323
if (!hasDefaultExport(ast)) {
1424
return input + `\nconst ${as} = {}`
1525
}

0 commit comments

Comments
 (0)