Skip to content

Commit 7e0d29c

Browse files
committed
refactor: inline @vite/client in runtime
1 parent 69b56f7 commit 7e0d29c

File tree

2 files changed

+37
-51
lines changed

2 files changed

+37
-51
lines changed

packages/vite/src/node/server/environments/rolldown.ts

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,6 @@ export function rolldownDevConfigureServer(server: ViteDevServer): void {
109109
sirv(environments.client.outDir, { dev: true, extensions: ['html'] }),
110110
)
111111

112-
// reuse /@vite/client for Websocket API but serve it on our own
113-
// TODO: include it in `rolldown_runtime`?
114-
const rolldownClientCode = getRolldownClientCode()
115-
server.middlewares.use((req, res, next) => {
116-
const url = new URL(req.url ?? '', 'https://rolldown.rs')
117-
if (url.pathname === '/@rolldown/client') {
118-
res.setHeader('content-type', 'text/javascript;charset=utf-8')
119-
res.end(rolldownClientCode)
120-
return
121-
}
122-
next()
123-
})
124-
125112
// full build on non self accepting entry
126113
server.ws.on('rolldown:hmr-deadend', async (data) => {
127114
logger.info(`hmr-deadend '${data.moduleId}'`, { timestamp: true })
@@ -138,39 +125,6 @@ export async function rolldownDevHandleHotUpdate(
138125
await environments.client.handleUpdate(ctx)
139126
}
140127

141-
function getRolldownClientCode() {
142-
let code = fs.readFileSync(CLIENT_ENTRY, 'utf-8')
143-
const replacements = {
144-
// TODO: packages/vite/src/node/plugins/clientInjections.ts
145-
__BASE__: `"/"`,
146-
__SERVER_HOST__: `""`,
147-
__HMR_PROTOCOL__: `null`,
148-
__HMR_HOSTNAME__: `null`,
149-
__HMR_PORT__: `new URL(import.meta.url).port`,
150-
__HMR_DIRECT_TARGET__: `""`,
151-
__HMR_BASE__: `"/"`,
152-
__HMR_TIMEOUT__: `30000`,
153-
__HMR_ENABLE_OVERLAY__: `true`,
154-
__HMR_CONFIG_NAME__: `""`,
155-
// runtime define is not necessary
156-
[`import '@vite/env';`]: ``,
157-
[`import "@vite/env";`]: ``, // for local pnpm dev
158-
}
159-
for (const [k, v] of Object.entries(replacements)) {
160-
code = code.replaceAll(k, v)
161-
}
162-
code = code.replace(/\/\/# sourceMappingURL.*/, '')
163-
// inject own hmr event handler
164-
code += `
165-
const hot = createHotContext("/__rolldown");
166-
hot.on("rolldown:hmr", (data) => {
167-
(0, eval)(data[1]);
168-
});
169-
window.__rolldown_hot = hot;
170-
`
171-
return code
172-
}
173-
174128
//
175129
// Rolldown dev environment
176130
//
@@ -311,10 +265,7 @@ function patchRuntimePlugin(): rolldown.Plugin {
311265
if (code.includes('//#region rolldown:runtime')) {
312266
const output = new MagicString(code)
313267
// replace hard-coded WebSocket setup with custom client
314-
output.replace(
315-
/const socket =.*?\n\};/s,
316-
'import("/@rolldown/client");',
317-
)
268+
output.replace(/const socket =.*?\n\};/s, getRolldownClientCode())
318269
// trigger full rebuild on non-accepting entry invalidation
319270
output
320271
.replace('parents: [parent],', 'parents: parent ? [parent] : [],')
@@ -333,6 +284,42 @@ function patchRuntimePlugin(): rolldown.Plugin {
333284
}
334285
}
335286

287+
// reuse /@vite/client for Websocket API
288+
function getRolldownClientCode() {
289+
let code = fs.readFileSync(CLIENT_ENTRY, 'utf-8')
290+
const replacements = {
291+
// TODO: packages/vite/src/node/plugins/clientInjections.ts
292+
__BASE__: `"/"`,
293+
__SERVER_HOST__: `""`,
294+
__HMR_PROTOCOL__: `null`,
295+
__HMR_HOSTNAME__: `null`,
296+
__HMR_PORT__: `new URL(self.location.href).port`,
297+
__HMR_DIRECT_TARGET__: `""`,
298+
__HMR_BASE__: `"/"`,
299+
__HMR_TIMEOUT__: `30000`,
300+
__HMR_ENABLE_OVERLAY__: `true`,
301+
__HMR_CONFIG_NAME__: `""`,
302+
}
303+
for (const [k, v] of Object.entries(replacements)) {
304+
code = code.replaceAll(k, v)
305+
}
306+
// runtime define is not necessary
307+
code = code.replace(/^import\s*['"]@vite\/env['"]/gm, '')
308+
// remove esm
309+
code = code.replace(/^export\s*\{[^}]*\}/gm, '')
310+
code = code.replace(/\bimport.meta.url\b/g, 'self.location.href')
311+
code = code.replace(/^\/\/#.*/gm, '')
312+
// inject own hmr event handler
313+
code += `
314+
const hot = createHotContext("/__rolldown");
315+
hot.on("rolldown:hmr", (data) => {
316+
(0, eval)(data[1]);
317+
});
318+
window.__rolldown_hot = hot;
319+
`
320+
return `(() => {/*** @vite/client ***/\n${code}}\n)()`
321+
}
322+
336323
// TODO: workaround rolldownExperimental.reactPlugin which injects js to html via `load` hook
337324
// TODO: can we inject to "react" itself?
338325
function reactRefreshPlugin(

playground/rolldown-dev-ssr/src/entry-server.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const handler: Connect.SimpleHandleFunction = (req, res) => {
1313
<head>
1414
<meta charset="UTF-8" />
1515
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
16-
<script type="module" src="/@rolldown/client"></script>
1716
</head>
1817
<body>
1918
<div id="root">${ssrHtml}</div>

0 commit comments

Comments
 (0)