@@ -4,6 +4,29 @@ const jsx = require('@vue/babel-plugin-jsx')
4
4
const importMeta = require ( '@babel/plugin-syntax-import-meta' )
5
5
const hash = require ( 'hash-sum' )
6
6
7
+ const ssrRegisterHelperId = '/__vue-jsx-ssr-register-helper'
8
+ const ssrRegisterHelperCode =
9
+ `import { useSSRContext } from "vue"\n` +
10
+ `export ${ ssrRegisterHelper . toString ( ) } `
11
+
12
+ /**
13
+ * This function is serialized with toString() and evaluated as a virtual
14
+ * module during SSR
15
+ * @param {import('vue').ComponentOptions } comp
16
+ * @param {string } filename
17
+ */
18
+ function ssrRegisterHelper ( comp , filename ) {
19
+ const setup = comp . setup
20
+ comp . setup = ( props , ctx ) => {
21
+ // @ts -ignore
22
+ const ssrContext = useSSRContext ( )
23
+ ; ( ssrContext . modules || ( ssrContext . modules = new Set ( ) ) ) . add ( filename )
24
+ if ( setup ) {
25
+ return setup ( props , ctx )
26
+ }
27
+ }
28
+ }
29
+
7
30
/**
8
31
* @param {import('@vue/babel-plugin-jsx').VueJSXPluginOptions } options
9
32
* @returns {import('vite').Plugin }
@@ -35,6 +58,18 @@ function vueJsxPlugin(options = {}) {
35
58
needSourceMap = config . command === 'serve' || ! ! config . build . sourcemap
36
59
} ,
37
60
61
+ resolveId ( id ) {
62
+ if ( id === ssrRegisterHelperId ) {
63
+ return id
64
+ }
65
+ } ,
66
+
67
+ load ( id ) {
68
+ if ( id === ssrRegisterHelperId ) {
69
+ return ssrRegisterHelperCode
70
+ }
71
+ } ,
72
+
38
73
transform ( code , id , ssr ) {
39
74
if ( / \. [ j t ] s x $ / . test ( id ) ) {
40
75
const plugins = [ importMeta , [ jsx , options ] ]
@@ -53,7 +88,7 @@ function vueJsxPlugin(options = {}) {
53
88
sourceFileName : id
54
89
} )
55
90
56
- if ( ssr || ! needHmr ) {
91
+ if ( ! ssr && ! needHmr ) {
57
92
return {
58
93
code : result . code ,
59
94
map : result . map
@@ -143,28 +178,40 @@ function vueJsxPlugin(options = {}) {
143
178
}
144
179
145
180
if ( hotComponents . length ) {
146
- let code = result . code
147
- if ( hasDefault ) {
148
- code =
149
- code . replace (
150
- / 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,
151
- `const __default__ = defineComponent`
152
- ) + `\nexport default __default__`
153
- }
181
+ if ( needHmr && ! ssr ) {
182
+ let code = result . code
183
+ if ( hasDefault ) {
184
+ code =
185
+ code . replace (
186
+ / 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,
187
+ `const __default__ = defineComponent`
188
+ ) + `\nexport default __default__`
189
+ }
154
190
155
- let callbackCode = ``
156
- for ( const { local, exported, id } of hotComponents ) {
157
- code +=
158
- `\n${ local } .__hmrId = "${ id } "` +
159
- `\n__VUE_HMR_RUNTIME__.createRecord("${ id } ", ${ local } )`
160
- callbackCode += `\n__VUE_HMR_RUNTIME__.reload("${ id } ", __${ exported } )`
161
- }
191
+ let callbackCode = ``
192
+ for ( const { local, exported, id } of hotComponents ) {
193
+ code +=
194
+ `\n${ local } .__hmrId = "${ id } "` +
195
+ `\n__VUE_HMR_RUNTIME__.createRecord("${ id } ", ${ local } )`
196
+ callbackCode += `\n__VUE_HMR_RUNTIME__.reload("${ id } ", __${ exported } )`
197
+ }
198
+
199
+ code += `\nimport.meta.hot.accept(({${ hotComponents
200
+ . map ( ( c ) => `${ c . exported } : __${ c . exported } ` )
201
+ . join ( ',' ) } }) => {${ callbackCode } \n})`
162
202
163
- code += `\nimport.meta.hot.accept(({${ hotComponents
164
- . map ( ( c ) => `${ c . exported } : __${ c . exported } ` )
165
- . join ( ',' ) } }) => {${ callbackCode } \n})`
203
+ result . code = code
204
+ }
166
205
167
- result . code = code
206
+ if ( ssr ) {
207
+ let ssrInjectCode =
208
+ `\nimport { ssrRegisterHelper } from "${ ssrRegisterHelperId } "` +
209
+ `\nconst __moduleId = ${ JSON . stringify ( id ) } `
210
+ for ( const { local } of hotComponents ) {
211
+ ssrInjectCode += `\nssrRegisterHelper(${ local } , __moduleId)`
212
+ }
213
+ result . code += ssrInjectCode
214
+ }
168
215
}
169
216
170
217
return {
0 commit comments