Skip to content

Commit 9885cb2

Browse files
committed
fix(plugin-vue-jsx): replace export default defineComponent with babel
1 parent d3160e8 commit 9885cb2

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createHash } 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'
@@ -112,6 +112,36 @@ function vueJsxPlugin(options: Options = {}): Plugin {
112112
},
113113
}
114114
})
115+
} else {
116+
plugins.push(() => {
117+
return {
118+
visitor: {
119+
ExportDefaultDeclaration: {
120+
enter(_path: babel.NodePath<types.ExportDefaultDeclaration>) {
121+
if (isDefineComponentCall(_path.node.declaration)) {
122+
const declaration = _path.node
123+
.declaration as CallExpression
124+
const nodesPath = _path.replaceWithMultiple([
125+
types.variableDeclaration('const', [
126+
types.variableDeclarator(
127+
types.identifier('__default__'),
128+
types.callExpression(
129+
declaration.callee,
130+
declaration.arguments,
131+
),
132+
),
133+
]),
134+
types.exportDefaultDeclaration(
135+
types.identifier('__default__'),
136+
),
137+
])
138+
_path.scope.registerDeclaration(nodesPath[0])
139+
}
140+
},
141+
},
142+
},
143+
}
144+
})
115145
}
116146

117147
const result = babel.transformSync(code, {
@@ -140,7 +170,6 @@ function vueJsxPlugin(options: Options = {}): Plugin {
140170
// check for hmr injection
141171
const declaredComponents: string[] = []
142172
const hotComponents: HotComponent[] = []
143-
let hasDefault = false
144173

145174
for (const node of result.ast!.program.body) {
146175
if (node.type === 'VariableDeclaration') {
@@ -195,7 +224,6 @@ function vueJsxPlugin(options: Options = {}): Plugin {
195224
})
196225
}
197226
} else if (isDefineComponentCall(node.declaration)) {
198-
hasDefault = true
199227
hotComponents.push({
200228
local: '__default__',
201229
exported: 'default',
@@ -206,14 +234,6 @@ function vueJsxPlugin(options: Options = {}): Plugin {
206234
}
207235

208236
if (hotComponents.length) {
209-
if (hasDefault && (needHmr || ssr)) {
210-
result.code =
211-
result.code!.replace(
212-
/export default defineComponent/g,
213-
`const __default__ = defineComponent`,
214-
) + `\nexport default __default__`
215-
}
216-
217237
if (needHmr && !ssr && !/\?vue&type=script/.test(id)) {
218238
let code = result.code
219239
let callbackCode = ``
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)