Skip to content

Commit c6f252a

Browse files
committed
refactor: resolve Evan's review comment
1 parent 9f38f96 commit c6f252a

File tree

5 files changed

+85
-98
lines changed

5 files changed

+85
-98
lines changed

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

Lines changed: 45 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
import { babelParse, rewriteDefault } from '../src'
2-
3-
function compileRewriteDefault(code: string, as: string) {
4-
const ast = babelParse(code, {
5-
sourceType: 'module',
6-
plugins: ['typescript', 'decorators-legacy']
7-
})
8-
return rewriteDefault(code, as, undefined, ast.program.body)
9-
}
1+
import { rewriteDefault } from '../src'
102

113
describe('compiler sfc: rewriteDefault', () => {
124
test('without export default', () => {
13-
expect(compileRewriteDefault(`export const a = {}`, 'script'))
5+
expect(rewriteDefault(`export const a = {}`, 'script'))
146
.toMatchInlineSnapshot(`
157
"export const a = {}
168
const script = {}"
@@ -19,12 +11,12 @@ describe('compiler sfc: rewriteDefault', () => {
1911

2012
test('rewrite export default', () => {
2113
expect(
22-
compileRewriteDefault(`export default {}`, 'script')
14+
rewriteDefault(`export default {}`, 'script')
2315
).toMatchInlineSnapshot(`"const script = {}"`)
2416
})
2517

2618
test('rewrite variable value default', () => {
27-
expect(compileRewriteDefault(`export const foo = 'default'`, 'script'))
19+
expect(rewriteDefault(`export const foo = 'default'`, 'script'))
2820
.toMatchInlineSnapshot(`
2921
"export const foo = 'default'
3022
const script = {}"
@@ -33,7 +25,7 @@ describe('compiler sfc: rewriteDefault', () => {
3325

3426
test('rewrite export named default', () => {
3527
expect(
36-
compileRewriteDefault(
28+
rewriteDefault(
3729
`const a = 1 \n export { a as b, a as default, a as c}`,
3830
'script'
3931
)
@@ -44,7 +36,7 @@ describe('compiler sfc: rewriteDefault', () => {
4436
`)
4537

4638
expect(
47-
compileRewriteDefault(
39+
rewriteDefault(
4840
`const a = 1 \n export { a as b, a as default , a as c}`,
4941
'script'
5042
)
@@ -55,7 +47,7 @@ describe('compiler sfc: rewriteDefault', () => {
5547
`)
5648

5749
expect(
58-
compileRewriteDefault(
50+
rewriteDefault(
5951
`const a = 1 \n export { a as b } \n export { a as default, a as c }`,
6052
'script'
6153
)
@@ -68,32 +60,28 @@ describe('compiler sfc: rewriteDefault', () => {
6860
})
6961

7062
test('w/ comments', async () => {
71-
expect(
72-
compileRewriteDefault(`// export default\nexport default {}`, 'script')
73-
).toMatchInlineSnapshot(`
63+
expect(rewriteDefault(`// export default\nexport default {}`, 'script'))
64+
.toMatchInlineSnapshot(`
7465
"// export default
7566
const script = {}"
7667
`)
7768
})
7869

7970
test('export named default multiline', () => {
8071
expect(
81-
compileRewriteDefault(
82-
`let App = {}\n export {\nApp as default\n}`,
83-
'_sfc_main'
84-
)
72+
rewriteDefault(`let App = {}\n export {\nApp as default\n}`, '_sfc_main')
8573
).toMatchInlineSnapshot(`
8674
"let App = {}
8775
export {
88-
76+
8977
}
9078
const _sfc_main = App"
9179
`)
9280
})
9381

9482
test('export named default multiline /w comments', () => {
9583
expect(
96-
compileRewriteDefault(
84+
rewriteDefault(
9785
`const a = 1 \n export {\n a as b,\n a as default,\n a as c}\n` +
9886
`// export { myFunction as default }`,
9987
'script'
@@ -109,7 +97,7 @@ describe('compiler sfc: rewriteDefault', () => {
10997
`)
11098

11199
expect(
112-
compileRewriteDefault(
100+
rewriteDefault(
113101
`const a = 1 \n export {\n a as b,\n a as default ,\n a as c}\n` +
114102
`// export { myFunction as default }`,
115103
'script'
@@ -127,40 +115,31 @@ describe('compiler sfc: rewriteDefault', () => {
127115

128116
test(`export { default } from '...'`, async () => {
129117
expect(
130-
compileRewriteDefault(
131-
`export { default, foo } from './index.js'`,
132-
'script'
133-
)
118+
rewriteDefault(`export { default, foo } from './index.js'`, 'script')
134119
).toMatchInlineSnapshot(`
135-
"import { default as __VUE_DEFAULT__ } from './index.js'
136-
export { foo } from './index.js'
137-
const script = __VUE_DEFAULT__"
138-
`)
120+
"import { default as __VUE_DEFAULT__ } from './index.js'
121+
export { foo } from './index.js'
122+
const script = __VUE_DEFAULT__"
123+
`)
139124

140125
expect(
141-
compileRewriteDefault(
142-
`export { default , foo } from './index.js'`,
143-
'script'
144-
)
126+
rewriteDefault(`export { default , foo } from './index.js'`, 'script')
145127
).toMatchInlineSnapshot(`
146-
"import { default as __VUE_DEFAULT__ } from './index.js'
147-
export { foo } from './index.js'
148-
const script = __VUE_DEFAULT__"
149-
`)
128+
"import { default as __VUE_DEFAULT__ } from './index.js'
129+
export { foo } from './index.js'
130+
const script = __VUE_DEFAULT__"
131+
`)
150132

151133
expect(
152-
compileRewriteDefault(
153-
`export { foo, default } from './index.js'`,
154-
'script'
155-
)
134+
rewriteDefault(`export { foo, default } from './index.js'`, 'script')
156135
).toMatchInlineSnapshot(`
157-
"import { default as __VUE_DEFAULT__ } from './index.js'
158-
export { foo, } from './index.js'
159-
const script = __VUE_DEFAULT__"
160-
`)
136+
"import { default as __VUE_DEFAULT__ } from './index.js'
137+
export { foo, } from './index.js'
138+
const script = __VUE_DEFAULT__"
139+
`)
161140

162141
expect(
163-
compileRewriteDefault(
142+
rewriteDefault(
164143
`export { foo as default, bar } from './index.js'`,
165144
'script'
166145
)
@@ -171,7 +150,7 @@ describe('compiler sfc: rewriteDefault', () => {
171150
`)
172151

173152
expect(
174-
compileRewriteDefault(
153+
rewriteDefault(
175154
`export { foo as default , bar } from './index.js'`,
176155
'script'
177156
)
@@ -182,7 +161,7 @@ describe('compiler sfc: rewriteDefault', () => {
182161
`)
183162

184163
expect(
185-
compileRewriteDefault(
164+
rewriteDefault(
186165
`export { bar, foo as default } from './index.js'`,
187166
'script'
188167
)
@@ -193,7 +172,7 @@ describe('compiler sfc: rewriteDefault', () => {
193172
`)
194173

195174
expect(
196-
compileRewriteDefault(
175+
rewriteDefault(
197176
`export { foo as default } from './index.js' \n const foo = 1`,
198177
'script'
199178
)
@@ -205,7 +184,7 @@ describe('compiler sfc: rewriteDefault', () => {
205184
`)
206185

207186
expect(
208-
compileRewriteDefault(
187+
rewriteDefault(
209188
`const a = 1 \nexport { a as default } from 'xxx'`,
210189
'script'
211190
)
@@ -218,7 +197,7 @@ describe('compiler sfc: rewriteDefault', () => {
218197
})
219198

220199
test('export default class', async () => {
221-
expect(compileRewriteDefault(`export default class Foo {}`, 'script'))
200+
expect(rewriteDefault(`export default class Foo {}`, 'script'))
222201
.toMatchInlineSnapshot(`
223202
"class Foo {}
224203
const script = Foo"
@@ -227,10 +206,7 @@ describe('compiler sfc: rewriteDefault', () => {
227206

228207
test('export default class w/ comments', async () => {
229208
expect(
230-
compileRewriteDefault(
231-
`// export default\nexport default class Foo {}`,
232-
'script'
233-
)
209+
rewriteDefault(`// export default\nexport default class Foo {}`, 'script')
234210
).toMatchInlineSnapshot(`
235211
"// export default
236212
class Foo {}
@@ -240,7 +216,7 @@ describe('compiler sfc: rewriteDefault', () => {
240216

241217
test('export default class w/ comments 2', async () => {
242218
expect(
243-
compileRewriteDefault(
219+
rewriteDefault(
244220
`export default {}\n` + `// export default class Foo {}`,
245221
'script'
246222
)
@@ -252,7 +228,7 @@ describe('compiler sfc: rewriteDefault', () => {
252228

253229
test('export default class w/ comments 3', async () => {
254230
expect(
255-
compileRewriteDefault(
231+
rewriteDefault(
256232
`/*\nexport default class Foo {}*/\n` + `export default class Bar {}`,
257233
'script'
258234
)
@@ -266,7 +242,9 @@ describe('compiler sfc: rewriteDefault', () => {
266242

267243
test('@Component\nexport default class', async () => {
268244
expect(
269-
compileRewriteDefault(`@Component\nexport default class Foo {}`, 'script')
245+
rewriteDefault(`@Component\nexport default class Foo {}`, 'script', [
246+
'decorators-legacy'
247+
])
270248
).toMatchInlineSnapshot(`
271249
"@Component
272250
class Foo {}
@@ -276,9 +254,10 @@ describe('compiler sfc: rewriteDefault', () => {
276254

277255
test('@Component\nexport default class w/ comments', async () => {
278256
expect(
279-
compileRewriteDefault(
257+
rewriteDefault(
280258
`// export default\n@Component\nexport default class Foo {}`,
281-
'script'
259+
'script',
260+
['decorators-legacy']
282261
)
283262
).toMatchInlineSnapshot(`
284263
"// export default
@@ -290,7 +269,7 @@ describe('compiler sfc: rewriteDefault', () => {
290269

291270
test('@Component\nexport default class w/ comments 2', async () => {
292271
expect(
293-
compileRewriteDefault(
272+
rewriteDefault(
294273
`export default {}\n` + `// @Component\n// export default class Foo {}`,
295274
'script'
296275
)
@@ -303,7 +282,7 @@ describe('compiler sfc: rewriteDefault', () => {
303282

304283
test('@Component\nexport default class w/ comments 3', async () => {
305284
expect(
306-
compileRewriteDefault(
285+
rewriteDefault(
307286
`/*\n@Component\nexport default class Foo {}*/\n` +
308287
`export default class Bar {}`,
309288
'script'

packages/compiler-sfc/src/compileScript.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import {
5353
} from './cssVars'
5454
import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate'
5555
import { warnOnce } from './warn'
56-
import { rewriteDefault } from './rewriteDefault'
56+
import { rewriteDefaultAST } from './rewriteDefault'
5757
import { createCache } from './cache'
5858
import { shouldTransform, transformAST } from '@vue/reactivity-transform'
5959

@@ -235,12 +235,9 @@ export function compileScript(
235235
}
236236
}
237237
if (cssVars.length) {
238-
content = rewriteDefault(
239-
content,
240-
DEFAULT_VAR,
241-
undefined,
242-
scriptAst.body
243-
)
238+
const s = new MagicString(content)
239+
rewriteDefaultAST(scriptAst.body, s, DEFAULT_VAR)
240+
content = s.toString()
244241
content += genNormalScriptCssVarsCode(
245242
cssVars,
246243
bindings,
@@ -1695,6 +1692,7 @@ export function compileScript(
16951692

16961693
return {
16971694
...scriptSetup,
1695+
s,
16981696
bindings: bindingMetadata,
16991697
imports: userImports,
17001698
content: s.toString(),

packages/compiler-sfc/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export { parse } from './parse'
33
export { compileTemplate } from './compileTemplate'
44
export { compileStyle, compileStyleAsync } from './compileStyle'
55
export { compileScript } from './compileScript'
6-
export { rewriteDefault } from './rewriteDefault'
6+
export { rewriteDefault, rewriteDefaultAST } from './rewriteDefault'
77
export {
88
shouldTransform as shouldTransformRef,
99
transform as transformRef,

packages/compiler-sfc/src/parse.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { TemplateCompiler } from './compileTemplate'
1212
import { parseCssVars } from './cssVars'
1313
import { createCache } from './cache'
1414
import { hmrShouldReload, ImportBinding } from './compileScript'
15+
import MagicString from 'magic-string'
1516

1617
export const DEFAULT_FILENAME = 'anonymous.vue'
1718

@@ -41,6 +42,7 @@ export interface SFCTemplateBlock extends SFCBlock {
4142

4243
export interface SFCScriptBlock extends SFCBlock {
4344
type: 'script'
45+
s: MagicString
4446
setup?: string | boolean
4547
bindings?: BindingMetadata
4648
imports?: Record<string, ImportBinding>

0 commit comments

Comments
 (0)