Skip to content

Commit 5568bd5

Browse files
committed
feat: expose virtual for simpler preamble setup on ssr
1 parent 73be2f0 commit 5568bd5

File tree

8 files changed

+62
-5
lines changed

8 files changed

+62
-5
lines changed

packages/common/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
".": "./index.ts",
88
"./refresh-runtime": "./refresh-runtime.js"
99
},
10+
"dependencies": {
11+
"@rolldown/pluginutils": "1.0.0-beta.41"
12+
},
1013
"peerDependencies": {
1114
"vite": "^4.2.0 || ^5.0.0 || ^6.0.0"
1215
}

packages/common/refresh-utils.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type { Plugin } from 'vite'
2+
import { exactRegex } from '@rolldown/pluginutils'
3+
14
export const runtimePublicPath = '/@react-refresh'
25

36
const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/
@@ -60,3 +63,36 @@ function $RefreshSig$() { return RefreshRuntime.createSignatureFunctionForTransf
6063

6164
return newCode
6265
}
66+
67+
export function virtualPreamblePlugin({
68+
name,
69+
isEnabled,
70+
}: {
71+
name: string
72+
isEnabled: () => boolean
73+
}): Plugin {
74+
return {
75+
name: 'vite:react-virtual-preamble',
76+
resolveId: {
77+
order: 'pre',
78+
filter: { id: exactRegex(name) },
79+
handler(source) {
80+
if (source === name) {
81+
return '\0' + source
82+
}
83+
},
84+
},
85+
load: {
86+
filter: { id: exactRegex('\0' + name) },
87+
handler(id) {
88+
if (id === '\0' + name) {
89+
if (isEnabled()) {
90+
// vite dev import analysis can rewrite base
91+
return preambleCode.replace('__BASE__', '/')
92+
}
93+
return ''
94+
}
95+
},
96+
},
97+
}
98+
}

packages/plugin-react/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
"dist"
2121
],
2222
"type": "module",
23-
"exports": "./dist/index.js",
23+
"exports": {
24+
".": "./dist/index.js",
25+
"./preamble": "./preamble.d.ts"
26+
},
2427
"scripts": {
2528
"dev": "tsdown --watch ./src --watch ../common",
2629
"build": "tsdown",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

packages/plugin-react/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
preambleCode,
1313
runtimePublicPath,
1414
silenceUseClientWarning,
15+
virtualPreamblePlugin,
1516
} from '@vitejs/react-common'
1617
import {
1718
exactRegex,
@@ -524,6 +525,10 @@ export default function viteReact(opts: Options = {}): Plugin[] {
524525
? [viteRefreshWrapper, viteConfigPost, viteReactRefreshFullBundleMode]
525526
: []),
526527
viteReactRefresh,
528+
virtualPreamblePlugin({
529+
name: '@vitejs/plugin-react/preamble',
530+
isEnabled: () => !skipFastRefresh && !isFullBundle,
531+
}),
527532
]
528533
}
529534

playground/ssr-react/src/entry-client.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '@vitejs/plugin-react/preamble'
12
import ReactDOM from 'react-dom/client'
23
import { App } from './App'
34

playground/ssr-react/vite.config.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ export default defineConfig({
4141
'/src/entry-server.jsx',
4242
)
4343
const appHtml = render(url)
44-
const template = await server.transformIndexHtml(
45-
url,
46-
fs.readFileSync(path.resolve(_dirname, 'index.html'), 'utf-8'),
44+
const template = fs.readFileSync(
45+
path.resolve(_dirname, 'index.html'),
46+
'utf-8',
4747
)
48+
// const template = await server.transformIndexHtml(
49+
// url,
50+
// fs.readFileSync(path.resolve(_dirname, 'index.html'), 'utf-8'),
51+
// )
4852
const html = template.replace(`<!--app-html-->`, appHtml)
4953
res.setHeader('content-type', 'text/html').end(html)
5054
} catch (e) {

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)