Skip to content

Commit 9a8f587

Browse files
committed
feat(plugin-vue-jsx): register jsx module during ssr
1 parent 91ba113 commit 9a8f587

File tree

1 file changed

+67
-20
lines changed

1 file changed

+67
-20
lines changed

packages/plugin-vue-jsx/index.js

+67-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ const jsx = require('@vue/babel-plugin-jsx')
44
const importMeta = require('@babel/plugin-syntax-import-meta')
55
const hash = require('hash-sum')
66

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+
730
/**
831
* @param {import('@vue/babel-plugin-jsx').VueJSXPluginOptions} options
932
* @returns {import('vite').Plugin}
@@ -35,6 +58,18 @@ function vueJsxPlugin(options = {}) {
3558
needSourceMap = config.command === 'serve' || !!config.build.sourcemap
3659
},
3760

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+
3873
transform(code, id, ssr) {
3974
if (/\.[jt]sx$/.test(id)) {
4075
const plugins = [importMeta, [jsx, options]]
@@ -53,7 +88,7 @@ function vueJsxPlugin(options = {}) {
5388
sourceFileName: id
5489
})
5590

56-
if (ssr || !needHmr) {
91+
if (!ssr && !needHmr) {
5792
return {
5893
code: result.code,
5994
map: result.map
@@ -143,28 +178,40 @@ function vueJsxPlugin(options = {}) {
143178
}
144179

145180
if (hotComponents.length) {
146-
let code = result.code
147-
if (hasDefault) {
148-
code =
149-
code.replace(
150-
/export default defineComponent/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+
/export default defineComponent/g,
187+
`const __default__ = defineComponent`
188+
) + `\nexport default __default__`
189+
}
154190

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})`
162202

163-
code += `\nimport.meta.hot.accept(({${hotComponents
164-
.map((c) => `${c.exported}: __${c.exported}`)
165-
.join(',')}}) => {${callbackCode}\n})`
203+
result.code = code
204+
}
166205

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+
}
168215
}
169216

170217
return {

0 commit comments

Comments
 (0)