1
+ import { isFunctionalNode } from '@vue-jsx-vapor/macros/api'
1
2
import getHash from 'hash-sum'
2
- import { normalizePath } from 'unplugin-utils'
3
3
import type { BabelFileResult , types } from '@babel/core'
4
4
5
5
interface HotComponent {
@@ -11,19 +11,19 @@ interface HotComponent {
11
11
export function registerHMR (
12
12
result : BabelFileResult ,
13
13
id : string ,
14
- defineComponentName = [ 'defineComponent' , 'defineVaporComponent' ] ,
14
+ defineComponentNames = [ 'defineComponent' , 'defineVaporComponent' ] ,
15
15
) {
16
16
const { ast } = result
17
17
18
18
// check for hmr injection
19
19
const declaredComponents : string [ ] = [ ]
20
20
const hotComponents : HotComponent [ ] = [ ]
21
- let defaultName = ''
21
+ let hasDefaultExport = false
22
22
const ssr = false
23
23
24
24
for ( const node of ast ! . program . body ) {
25
25
if ( node . type === 'VariableDeclaration' ) {
26
- const names = parseComponentDecls ( node , defineComponentName )
26
+ const names = parseComponentDecls ( node , defineComponentNames )
27
27
if ( names . length ) {
28
28
declaredComponents . push ( ...names )
29
29
}
@@ -32,7 +32,7 @@ export function registerHMR(
32
32
if ( node . type === 'ExportNamedDeclaration' ) {
33
33
if ( node . declaration && node . declaration . type === 'VariableDeclaration' ) {
34
34
hotComponents . push (
35
- ...parseComponentDecls ( node . declaration , defineComponentName ) . map (
35
+ ...parseComponentDecls ( node . declaration , defineComponentNames ) . map (
36
36
( name ) => ( {
37
37
local : name ,
38
38
exported : name ,
@@ -72,8 +72,11 @@ export function registerHMR(
72
72
id : getHash ( `${ id } default` ) ,
73
73
} )
74
74
}
75
- } else if ( isDefineComponentCall ( node . declaration , defineComponentName ) ) {
76
- defaultName = ( node . declaration . callee as any ) . name
75
+ } else if (
76
+ isDefineComponentCall ( node . declaration , defineComponentNames ) ||
77
+ isFunctionalNode ( node . declaration )
78
+ ) {
79
+ hasDefaultExport = true
77
80
hotComponents . push ( {
78
81
local : '__default__' ,
79
82
exported : 'default' ,
@@ -84,10 +87,10 @@ export function registerHMR(
84
87
}
85
88
86
89
if ( hotComponents . length ) {
87
- if ( defaultName || ssr ) {
90
+ if ( hasDefaultExport || ssr ) {
88
91
result . code = `${ result . code ! . replaceAll (
89
- `export default ${ defaultName } ` ,
90
- `const __default__ = ${ defaultName } ` ,
92
+ `export default ` ,
93
+ `const __default__ = ` ,
91
94
) } \nexport default __default__`
92
95
}
93
96
@@ -99,19 +102,12 @@ export function registerHMR(
99
102
`\n${ local } .__hmrId = "${ id } "` +
100
103
`\n__VUE_HMR_RUNTIME__.createRecord("${ id } ", ${ local } )`
101
104
callbackCode += `
102
- if (mod._rerender_only) {
103
- __VUE_HMR_RUNTIME__.rerender(mod['${ exported } '].__hmrId, mod['${ exported } '].setup)
104
- } else {
105
- __VUE_HMR_RUNTIME__.reload(mod['${ exported } '].__hmrId, mod['${ exported } '])
106
- }`
105
+ __VUE_HMR_RUNTIME__.rerender(mod['${ exported } '].__hmrId, mod['${ exported } '].setup || mod['${ exported } '])
106
+ `
107
107
}
108
108
109
- code += `\nexport const _rerender_only = __VUE_HMR_RUNTIME__.CHANGED_FILE === ${ JSON . stringify ( normalizePath ( id ) ) } `
110
109
code += `
111
110
if (import.meta.hot) {
112
- import.meta.hot.on('file-changed', ({ file }) => {
113
- __VUE_HMR_RUNTIME__.CHANGED_FILE = file
114
- })
115
111
import.meta.hot.accept((mod) => {${ callbackCode } \n})
116
112
}`
117
113
result . code = code
@@ -138,10 +134,9 @@ function parseComponentDecls(
138
134
for ( const decl of node . declarations ) {
139
135
if (
140
136
decl . id . type === 'Identifier' &&
141
- isDefineComponentCall ( decl . init , fnNames )
142
- ) {
137
+ ( isDefineComponentCall ( decl . init , fnNames ) || isFunctionalNode ( decl . init ) )
138
+ )
143
139
names . push ( decl . id . name )
144
- }
145
140
}
146
141
return names
147
142
}
0 commit comments