11import crypto from 'node:crypto'
22import path from 'node:path'
3- import type { types } from '@babel/core'
3+ import { types } from '@babel/core'
44import * as babel from '@babel/core'
55import jsx from '@vue/babel-plugin-jsx'
66import { 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- / 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-
262291 if ( needHmr && ! ssr && ! / \? v u e & t y p e = s c r i p t / . test ( id ) ) {
263292 let code = result . code
264293 let callbackCode = ``
0 commit comments