@@ -22,16 +22,34 @@ const RSC_SOURCE_FILES = {
2222 require ( '!raw-loader?esModule=false!./sandpack-rsc/sandbox-code/src/webpack-shim.js' ) as string ,
2323 'rsc-client' :
2424 require ( '!raw-loader?esModule=false!./sandpack-rsc/sandbox-code/src/rsc-client.js' ) as string ,
25+ 'react-refresh-init' :
26+ require ( '!raw-loader?esModule=false!./sandpack-rsc/sandbox-code/src/__react_refresh_init__.js' ) as string ,
2527 'worker-bundle' : `export default ${ JSON . stringify (
2628 require ( '!raw-loader?esModule=false!./sandpack-rsc/sandbox-code/src/worker-bundle.dist.js' ) as string
2729 ) } ;`,
2830 'rsdw-client' :
2931 require ( '!raw-loader?esModule=false!../../../../node_modules/react-server-dom-webpack/cjs/react-server-dom-webpack-client.browser.production.js' ) as string ,
3032} ;
3133
34+ // Load react-refresh runtime and strip the process.env.NODE_ENV guard
35+ // so it works in Sandpack's bundler which may not replace process.env.
36+ const reactRefreshRaw =
37+ require ( '!raw-loader?esModule=false!../../../../node_modules/next/dist/compiled/react-refresh/cjs/react-refresh-runtime.development.js' ) as string ;
38+
39+ // Wrap as a CJS module that Sandpack can require.
40+ // Strip the `if (process.env.NODE_ENV !== "production")` guard so the
41+ // runtime always executes inside the sandbox.
42+ const reactRefreshModule = reactRefreshRaw . replace (
43+ / i f \( p r o c e s s \. e n v \. N O D E _ E N V ! = = " p r o d u c t i o n " \) \{ / ,
44+ '{'
45+ ) ;
46+
3247// Entry point that bootstraps the RSC client pipeline.
48+ // __react_refresh_init__ must be imported BEFORE rsc-client so the
49+ // DevTools hook stub exists before React's renderer loads.
3350const indexEntry = `
3451import './styles.css';
52+ import './__react_refresh_init__';
3553import { initClient } from './rsc-client.js';
3654initClient();
3755` . trim ( ) ;
@@ -54,6 +72,7 @@ export const templateRSC: SandpackFiles = {
5472 ...hideFiles ( {
5573 '/public/index.html' : indexHTML ,
5674 '/src/index.js' : indexEntry ,
75+ '/src/__react_refresh_init__.js' : RSC_SOURCE_FILES [ 'react-refresh-init' ] ,
5776 '/src/rsc-client.js' : RSC_SOURCE_FILES [ 'rsc-client' ] ,
5877 '/src/rsc-server.js' : RSC_SOURCE_FILES [ 'worker-bundle' ] ,
5978 '/src/__webpack_shim__.js' : RSC_SOURCE_FILES [ 'webpack-shim' ] ,
@@ -62,6 +81,10 @@ export const templateRSC: SandpackFiles = {
6281 '{"name":"react-server-dom-webpack","main":"index.js"}' ,
6382 '/node_modules/react-server-dom-webpack/client.browser.js' :
6483 RSC_SOURCE_FILES [ 'rsdw-client' ] ,
84+ // react-refresh runtime as a Sandpack local dependency
85+ '/node_modules/react-refresh/package.json' :
86+ '{"name":"react-refresh","main":"runtime.js"}' ,
87+ '/node_modules/react-refresh/runtime.js' : reactRefreshModule ,
6588 '/package.json' : JSON . stringify (
6689 {
6790 name : 'react.dev' ,
0 commit comments