1
1
import crypto from 'node:crypto'
2
2
import path from 'node:path'
3
- import type { types } from '@babel/core'
3
+ import { types } from '@babel/core'
4
4
import * as babel from '@babel/core'
5
5
import jsx from '@vue/babel-plugin-jsx'
6
6
import { createFilter , normalizePath } from 'vite'
@@ -150,6 +150,45 @@ function vueJsxPlugin(options: Options = {}): Plugin {
150
150
} ,
151
151
}
152
152
} )
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
+ } )
153
192
}
154
193
155
194
const result = babel . transformSync ( code , {
@@ -178,7 +217,6 @@ function vueJsxPlugin(options: Options = {}): Plugin {
178
217
// check for hmr injection
179
218
const declaredComponents : string [ ] = [ ]
180
219
const hotComponents : HotComponent [ ] = [ ]
181
- let hasDefault = false
182
220
183
221
for ( const node of result . ast ! . program . body ) {
184
222
if ( node . type === 'VariableDeclaration' ) {
@@ -240,7 +278,6 @@ function vueJsxPlugin(options: Options = {}): Plugin {
240
278
} else if (
241
279
isDefineComponentCall ( node . declaration , defineComponentName )
242
280
) {
243
- hasDefault = true
244
281
hotComponents . push ( {
245
282
local : '__default__' ,
246
283
exported : 'default' ,
@@ -251,14 +288,6 @@ function vueJsxPlugin(options: Options = {}): Plugin {
251
288
}
252
289
253
290
if ( hotComponents . length ) {
254
- if ( hasDefault && ( needHmr || ssr ) ) {
255
- result . code =
256
- result . code ! . replace (
257
- / e x p o r t d e f a u l t d e f i n e C o m p o n e n t / g,
258
- `const __default__ = defineComponent` ,
259
- ) + `\nexport default __default__`
260
- }
261
-
262
291
if ( needHmr && ! ssr && ! / \? v u e & t y p e = s c r i p t / . test ( id ) ) {
263
292
let code = result . code
264
293
let callbackCode = ``
0 commit comments