Skip to content

Commit

Permalink
refactor: resolve Evan's review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jan 26, 2023
1 parent 9f38f96 commit c6f252a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 98 deletions.
111 changes: 45 additions & 66 deletions packages/compiler-sfc/__tests__/rewriteDefault.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { babelParse, rewriteDefault } from '../src'

function compileRewriteDefault(code: string, as: string) {
const ast = babelParse(code, {
sourceType: 'module',
plugins: ['typescript', 'decorators-legacy']
})
return rewriteDefault(code, as, undefined, ast.program.body)
}
import { rewriteDefault } from '../src'

describe('compiler sfc: rewriteDefault', () => {
test('without export default', () => {
expect(compileRewriteDefault(`export const a = {}`, 'script'))
expect(rewriteDefault(`export const a = {}`, 'script'))
.toMatchInlineSnapshot(`
"export const a = {}
const script = {}"
Expand All @@ -19,12 +11,12 @@ describe('compiler sfc: rewriteDefault', () => {

test('rewrite export default', () => {
expect(
compileRewriteDefault(`export default {}`, 'script')
rewriteDefault(`export default {}`, 'script')
).toMatchInlineSnapshot(`"const script = {}"`)
})

test('rewrite variable value default', () => {
expect(compileRewriteDefault(`export const foo = 'default'`, 'script'))
expect(rewriteDefault(`export const foo = 'default'`, 'script'))
.toMatchInlineSnapshot(`
"export const foo = 'default'
const script = {}"
Expand All @@ -33,7 +25,7 @@ describe('compiler sfc: rewriteDefault', () => {

test('rewrite export named default', () => {
expect(
compileRewriteDefault(
rewriteDefault(
`const a = 1 \n export { a as b, a as default, a as c}`,
'script'
)
Expand All @@ -44,7 +36,7 @@ describe('compiler sfc: rewriteDefault', () => {
`)

expect(
compileRewriteDefault(
rewriteDefault(
`const a = 1 \n export { a as b, a as default , a as c}`,
'script'
)
Expand All @@ -55,7 +47,7 @@ describe('compiler sfc: rewriteDefault', () => {
`)

expect(
compileRewriteDefault(
rewriteDefault(
`const a = 1 \n export { a as b } \n export { a as default, a as c }`,
'script'
)
Expand All @@ -68,32 +60,28 @@ describe('compiler sfc: rewriteDefault', () => {
})

test('w/ comments', async () => {
expect(
compileRewriteDefault(`// export default\nexport default {}`, 'script')
).toMatchInlineSnapshot(`
expect(rewriteDefault(`// export default\nexport default {}`, 'script'))
.toMatchInlineSnapshot(`
"// export default
const script = {}"
`)
})

test('export named default multiline', () => {
expect(
compileRewriteDefault(
`let App = {}\n export {\nApp as default\n}`,
'_sfc_main'
)
rewriteDefault(`let App = {}\n export {\nApp as default\n}`, '_sfc_main')
).toMatchInlineSnapshot(`
"let App = {}
export {
}
const _sfc_main = App"
`)
})

test('export named default multiline /w comments', () => {
expect(
compileRewriteDefault(
rewriteDefault(
`const a = 1 \n export {\n a as b,\n a as default,\n a as c}\n` +
`// export { myFunction as default }`,
'script'
Expand All @@ -109,7 +97,7 @@ describe('compiler sfc: rewriteDefault', () => {
`)

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

test(`export { default } from '...'`, async () => {
expect(
compileRewriteDefault(
`export { default, foo } from './index.js'`,
'script'
)
rewriteDefault(`export { default, foo } from './index.js'`, 'script')
).toMatchInlineSnapshot(`
"import { default as __VUE_DEFAULT__ } from './index.js'
export { foo } from './index.js'
const script = __VUE_DEFAULT__"
`)
"import { default as __VUE_DEFAULT__ } from './index.js'
export { foo } from './index.js'
const script = __VUE_DEFAULT__"
`)

expect(
compileRewriteDefault(
`export { default , foo } from './index.js'`,
'script'
)
rewriteDefault(`export { default , foo } from './index.js'`, 'script')
).toMatchInlineSnapshot(`
"import { default as __VUE_DEFAULT__ } from './index.js'
export { foo } from './index.js'
const script = __VUE_DEFAULT__"
`)
"import { default as __VUE_DEFAULT__ } from './index.js'
export { foo } from './index.js'
const script = __VUE_DEFAULT__"
`)

expect(
compileRewriteDefault(
`export { foo, default } from './index.js'`,
'script'
)
rewriteDefault(`export { foo, default } from './index.js'`, 'script')
).toMatchInlineSnapshot(`
"import { default as __VUE_DEFAULT__ } from './index.js'
export { foo, } from './index.js'
const script = __VUE_DEFAULT__"
`)
"import { default as __VUE_DEFAULT__ } from './index.js'
export { foo, } from './index.js'
const script = __VUE_DEFAULT__"
`)

expect(
compileRewriteDefault(
rewriteDefault(
`export { foo as default, bar } from './index.js'`,
'script'
)
Expand All @@ -171,7 +150,7 @@ describe('compiler sfc: rewriteDefault', () => {
`)

expect(
compileRewriteDefault(
rewriteDefault(
`export { foo as default , bar } from './index.js'`,
'script'
)
Expand All @@ -182,7 +161,7 @@ describe('compiler sfc: rewriteDefault', () => {
`)

expect(
compileRewriteDefault(
rewriteDefault(
`export { bar, foo as default } from './index.js'`,
'script'
)
Expand All @@ -193,7 +172,7 @@ describe('compiler sfc: rewriteDefault', () => {
`)

expect(
compileRewriteDefault(
rewriteDefault(
`export { foo as default } from './index.js' \n const foo = 1`,
'script'
)
Expand All @@ -205,7 +184,7 @@ describe('compiler sfc: rewriteDefault', () => {
`)

expect(
compileRewriteDefault(
rewriteDefault(
`const a = 1 \nexport { a as default } from 'xxx'`,
'script'
)
Expand All @@ -218,7 +197,7 @@ describe('compiler sfc: rewriteDefault', () => {
})

test('export default class', async () => {
expect(compileRewriteDefault(`export default class Foo {}`, 'script'))
expect(rewriteDefault(`export default class Foo {}`, 'script'))
.toMatchInlineSnapshot(`
"class Foo {}
const script = Foo"
Expand All @@ -227,10 +206,7 @@ describe('compiler sfc: rewriteDefault', () => {

test('export default class w/ comments', async () => {
expect(
compileRewriteDefault(
`// export default\nexport default class Foo {}`,
'script'
)
rewriteDefault(`// export default\nexport default class Foo {}`, 'script')
).toMatchInlineSnapshot(`
"// export default
class Foo {}
Expand All @@ -240,7 +216,7 @@ describe('compiler sfc: rewriteDefault', () => {

test('export default class w/ comments 2', async () => {
expect(
compileRewriteDefault(
rewriteDefault(
`export default {}\n` + `// export default class Foo {}`,
'script'
)
Expand All @@ -252,7 +228,7 @@ describe('compiler sfc: rewriteDefault', () => {

test('export default class w/ comments 3', async () => {
expect(
compileRewriteDefault(
rewriteDefault(
`/*\nexport default class Foo {}*/\n` + `export default class Bar {}`,
'script'
)
Expand All @@ -266,7 +242,9 @@ describe('compiler sfc: rewriteDefault', () => {

test('@Component\nexport default class', async () => {
expect(
compileRewriteDefault(`@Component\nexport default class Foo {}`, 'script')
rewriteDefault(`@Component\nexport default class Foo {}`, 'script', [
'decorators-legacy'
])
).toMatchInlineSnapshot(`
"@Component
class Foo {}
Expand All @@ -276,9 +254,10 @@ describe('compiler sfc: rewriteDefault', () => {

test('@Component\nexport default class w/ comments', async () => {
expect(
compileRewriteDefault(
rewriteDefault(
`// export default\n@Component\nexport default class Foo {}`,
'script'
'script',
['decorators-legacy']
)
).toMatchInlineSnapshot(`
"// export default
Expand All @@ -290,7 +269,7 @@ describe('compiler sfc: rewriteDefault', () => {

test('@Component\nexport default class w/ comments 2', async () => {
expect(
compileRewriteDefault(
rewriteDefault(
`export default {}\n` + `// @Component\n// export default class Foo {}`,
'script'
)
Expand All @@ -303,7 +282,7 @@ describe('compiler sfc: rewriteDefault', () => {

test('@Component\nexport default class w/ comments 3', async () => {
expect(
compileRewriteDefault(
rewriteDefault(
`/*\n@Component\nexport default class Foo {}*/\n` +
`export default class Bar {}`,
'script'
Expand Down
12 changes: 5 additions & 7 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
} from './cssVars'
import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate'
import { warnOnce } from './warn'
import { rewriteDefault } from './rewriteDefault'
import { rewriteDefaultAST } from './rewriteDefault'
import { createCache } from './cache'
import { shouldTransform, transformAST } from '@vue/reactivity-transform'

Expand Down Expand Up @@ -235,12 +235,9 @@ export function compileScript(
}
}
if (cssVars.length) {
content = rewriteDefault(
content,
DEFAULT_VAR,
undefined,
scriptAst.body
)
const s = new MagicString(content)
rewriteDefaultAST(scriptAst.body, s, DEFAULT_VAR)
content = s.toString()
content += genNormalScriptCssVarsCode(
cssVars,
bindings,
Expand Down Expand Up @@ -1695,6 +1692,7 @@ export function compileScript(

return {
...scriptSetup,
s,
bindings: bindingMetadata,
imports: userImports,
content: s.toString(),
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { parse } from './parse'
export { compileTemplate } from './compileTemplate'
export { compileStyle, compileStyleAsync } from './compileStyle'
export { compileScript } from './compileScript'
export { rewriteDefault } from './rewriteDefault'
export { rewriteDefault, rewriteDefaultAST } from './rewriteDefault'
export {
shouldTransform as shouldTransformRef,
transform as transformRef,
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-sfc/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { TemplateCompiler } from './compileTemplate'
import { parseCssVars } from './cssVars'
import { createCache } from './cache'
import { hmrShouldReload, ImportBinding } from './compileScript'
import MagicString from 'magic-string'

export const DEFAULT_FILENAME = 'anonymous.vue'

Expand Down Expand Up @@ -41,6 +42,7 @@ export interface SFCTemplateBlock extends SFCBlock {

export interface SFCScriptBlock extends SFCBlock {
type: 'script'
s: MagicString
setup?: string | boolean
bindings?: BindingMetadata
imports?: Record<string, ImportBinding>
Expand Down
Loading

0 comments on commit c6f252a

Please # to comment.