@@ -109,19 +109,6 @@ export function rolldownDevConfigureServer(server: ViteDevServer): void {
109
109
sirv ( environments . client . outDir , { dev : true , extensions : [ 'html' ] } ) ,
110
110
)
111
111
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
-
125
112
// full build on non self accepting entry
126
113
server . ws . on ( 'rolldown:hmr-deadend' , async ( data ) => {
127
114
logger . info ( `hmr-deadend '${ data . moduleId } '` , { timestamp : true } )
@@ -138,39 +125,6 @@ export async function rolldownDevHandleHotUpdate(
138
125
await environments . client . handleUpdate ( ctx )
139
126
}
140
127
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 ( / \/ \/ # s o u r c e M a p p i n g U R L .* / , '' )
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
-
174
128
//
175
129
// Rolldown dev environment
176
130
//
@@ -311,10 +265,7 @@ function patchRuntimePlugin(): rolldown.Plugin {
311
265
if ( code . includes ( '//#region rolldown:runtime' ) ) {
312
266
const output = new MagicString ( code )
313
267
// replace hard-coded WebSocket setup with custom client
314
- output . replace (
315
- / c o n s t s o c k e t = .* ?\n \} ; / s,
316
- 'import("/@rolldown/client");' ,
317
- )
268
+ output . replace ( / c o n s t s o c k e t = .* ?\n \} ; / s, getRolldownClientCode ( ) )
318
269
// trigger full rebuild on non-accepting entry invalidation
319
270
output
320
271
. replace ( 'parents: [parent],' , 'parents: parent ? [parent] : [],' )
@@ -333,6 +284,42 @@ function patchRuntimePlugin(): rolldown.Plugin {
333
284
}
334
285
}
335
286
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 ( / ^ i m p o r t \s * [ ' " ] @ v i t e \/ e n v [ ' " ] / gm, '' )
308
+ // remove esm
309
+ code = code . replace ( / ^ e x p o r t \s * \{ [ ^ } ] * \} / gm, '' )
310
+ code = code . replace ( / \b i m p o r t .m e t a .u r l \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
+
336
323
// TODO: workaround rolldownExperimental.reactPlugin which injects js to html via `load` hook
337
324
// TODO: can we inject to "react" itself?
338
325
function reactRefreshPlugin (
0 commit comments