Skip to content

Commit 5feb9a2

Browse files
authored
fix(vue-jsx): replace export default defineComponent with babel (#348)
1 parent 67534e5 commit 5feb9a2

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

packages/plugin-vue-jsx/src/index.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import crypto from 'node:crypto'
22
import path from 'node:path'
3-
import type { types } from '@babel/core'
3+
import { types } from '@babel/core'
44
import * as babel from '@babel/core'
55
import jsx from '@vue/babel-plugin-jsx'
66
import { createFilter, normalizePath } from 'vite'
@@ -150,6 +150,45 @@ function vueJsxPlugin(options: Options = {}): Plugin {
150150
},
151151
}
152152
})
153+
} else {
154+
plugins.push(() => {
155+
return {
156+
visitor: {
157+
ExportDefaultDeclaration: {
158+
enter(
159+
_path: babel.NodePath<types.ExportDefaultDeclaration>,
160+
) {
161+
if (
162+
isDefineComponentCall(
163+
_path.node.declaration,
164+
defineComponentName,
165+
)
166+
) {
167+
const declaration = _path.node
168+
.declaration as types.CallExpression
169+
const nodesPath = _path.replaceWithMultiple([
170+
// const __default__ = defineComponent(...)
171+
types.variableDeclaration('const', [
172+
types.variableDeclarator(
173+
types.identifier('__default__'),
174+
types.callExpression(
175+
declaration.callee,
176+
declaration.arguments,
177+
),
178+
),
179+
]),
180+
// export default __default__
181+
types.exportDefaultDeclaration(
182+
types.identifier('__default__'),
183+
),
184+
])
185+
_path.scope.registerDeclaration(nodesPath[0])
186+
}
187+
},
188+
},
189+
},
190+
}
191+
})
153192
}
154193

155194
const result = babel.transformSync(code, {
@@ -178,7 +217,6 @@ function vueJsxPlugin(options: Options = {}): Plugin {
178217
// check for hmr injection
179218
const declaredComponents: string[] = []
180219
const hotComponents: HotComponent[] = []
181-
let hasDefault = false
182220

183221
for (const node of result.ast!.program.body) {
184222
if (node.type === 'VariableDeclaration') {
@@ -240,7 +278,6 @@ function vueJsxPlugin(options: Options = {}): Plugin {
240278
} else if (
241279
isDefineComponentCall(node.declaration, defineComponentName)
242280
) {
243-
hasDefault = true
244281
hotComponents.push({
245282
local: '__default__',
246283
exported: 'default',
@@ -251,14 +288,6 @@ function vueJsxPlugin(options: Options = {}): Plugin {
251288
}
252289

253290
if (hotComponents.length) {
254-
if (hasDefault && (needHmr || ssr)) {
255-
result.code =
256-
result.code!.replace(
257-
/export default defineComponent/g,
258-
`const __default__ = defineComponent`,
259-
) + `\nexport default __default__`
260-
}
261-
262291
if (needHmr && !ssr && !/\?vue&type=script/.test(id)) {
263292
let code = result.code
264293
let callbackCode = ``

playground/vue-jsx/ExportDefault.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineComponent } from 'vue'
2+
3+
export default defineComponent({
4+
render() {
5+
return <span class="export-default">export default defineComponent</span>
6+
},
7+
})

playground/vue-jsx/__tests__/vue-jsx.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ test('should render', async () => {
1111
expect(await page.textContent('.jsx-with-query')).toMatch('6')
1212
expect(await page.textContent('.other-ext')).toMatch('Other Ext')
1313
expect(await page.textContent('.ts-import')).toMatch('success')
14+
expect(await page.textContent('.export-default')).toMatch(
15+
'export default defineComponent',
16+
)
1417
})
1518

1619
test('should update', async () => {

playground/vue-jsx/main.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import JsxSetupSyntax from './setup-syntax-jsx.vue'
88
// eslint-disable-next-line
99
import JsxWithQuery from './Query.jsx?query=true'
1010
import TsImport from './TsImport.vue'
11+
import ExportDefault from './ExportDefault'
1112

1213
function App() {
1314
return (
@@ -22,6 +23,7 @@ function App() {
2223
<JsxSetupSyntax />
2324
<JsxWithQuery />
2425
<TsImport />
26+
<ExportDefault />
2527
</>
2628
)
2729
}

0 commit comments

Comments
 (0)