Skip to content

Commit c655057

Browse files
committed
chore: demo late discovery
1 parent 3c1ca38 commit c655057

File tree

7 files changed

+59
-1
lines changed

7 files changed

+59
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default { id: Math.random().toString(36).slice(2) }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './index.mjs'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@vitejs/test-dep",
3+
"private": true,
4+
"type": "module",
5+
"exports": {
6+
".": "./index.mjs",
7+
"./other": "./other.mjs"
8+
}
9+
}

playground/environment-react-ssr/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"version": "0.0.0",
55
"type": "module",
66
"scripts": {
7-
"dev": "vite --force",
7+
"dev": "vite",
88
"build": "vite build --app",
99
"preview": "vite preview"
1010
},
1111
"devDependencies": {
1212
"@types/react": "^18.3.11",
1313
"@types/react-dom": "^18.3.0",
14+
"@vitejs/test-dep": "file:./deps/test-dep",
1415
"react": "^18.3.1",
1516
"react-fake-client": "npm:react@^18.3.1",
1617
"react-fake-server": "npm:react@^18.3.1",

playground/environment-react-ssr/src/entry-server.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
import ReactDomServer from 'react-dom/server'
22
import type { Connect, ViteDevServer } from 'vite'
3+
import testDep from '@vitejs/test-dep'
34
import Root from './root'
45

56
const handler: Connect.NextHandleFunction = async (_req, res) => {
7+
const url = new URL(_req.url!, 'http://localhost')
8+
if (url.pathname === '/late-discovery') {
9+
// simulate late discovery by importing new modules from virtual.
10+
// this can cuase double modules on first request handling and
11+
// response would look like
12+
// {"entry":{"id":"heyzuawcitr"},"late":{"id":"autz5sutu1k"}}
13+
// @ts-expect-error no dts
14+
const mod = await import('virtual:late-discovery')
15+
res.setHeader('content-type', 'application/json').end(
16+
JSON.stringify({
17+
entry: testDep,
18+
late: mod.default,
19+
}),
20+
)
21+
return
22+
}
623
const ssrHtml = ReactDomServer.renderToString(<Root />)
724
let html = await importHtml()
825
html = html.replace(/<body>/, `<body><div id="root">${ssrHtml}</div>`)

playground/environment-react-ssr/vite.config.ts

+19
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,27 @@ export default defineConfig((env) => ({
2020
Object.assign(globalThis, { __globalServer: server })
2121
},
2222
},
23+
{
24+
name: 'virtual-late-discovery',
25+
resolveId(source) {
26+
if (source === 'virtual:late-discovery') {
27+
return '\0' + source
28+
}
29+
},
30+
load(id) {
31+
if (id === '\0virtual:late-discovery') {
32+
return `export { default } from "@vitejs/test-dep/other"`
33+
}
34+
},
35+
},
2336
],
2437
environments: {
2538
client: {
39+
dev: {
40+
optimizeDeps: {
41+
include: ['react/jsx-dev-runtime'],
42+
},
43+
},
2644
build: {
2745
minify: false,
2846
sourcemap: true,
@@ -33,6 +51,7 @@ export default defineConfig((env) => ({
3351
dev: {
3452
optimizeDeps: {
3553
noDiscovery: false,
54+
include: ['react/jsx-dev-runtime'],
3655
},
3756
},
3857
build: {

pnpm-lock.yaml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)