Skip to content

Commit

Permalink
chore: support client without hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 9, 2024
1 parent 8be84b8 commit a282d91
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
12 changes: 10 additions & 2 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class RolldownEnvironment extends DevEnvironment {
rolldownExperimental.transformPlugin({
reactRefresh: this.rolldownDevOptions?.reactRefresh,
}),
this.rolldownDevOptions?.reactRefresh ? reactRefreshPlugin() : [],
reactRefreshPlugin(this.rolldownDevOptions),
rolldownExperimental.aliasPlugin({
entries: this.config.resolve.alias,
}),
Expand Down Expand Up @@ -228,6 +228,9 @@ export class RolldownEnvironment extends DevEnvironment {
ctx.server.ws.send('rolldown:hmr', result)
} else {
await this.build()
if (this.name === 'client') {
ctx.server.ws.send({ type: 'full-reload' })
}
}
}

Expand Down Expand Up @@ -352,7 +355,9 @@ function viterollEntryPlugin(
}

// TODO: workaround rolldownExperimental.reactPlugin which injects js to html via `load` hook
function reactRefreshPlugin(): rolldown.Plugin {
function reactRefreshPlugin(
rolldownDevOptions: RolldownDevOptions,
): rolldown.Plugin {
return {
name: 'react-hmr',
transform: {
Expand Down Expand Up @@ -390,6 +395,9 @@ function reactRefreshPlugin(): rolldown.Plugin {
async handler(id) {
const resolved = require.resolve('react-refresh/runtime')
if (id === '\0virtual:react-refresh/entry') {
if (!rolldownDevOptions.reactRefresh) {
return `export {}`
}
return `
import runtime from ${JSON.stringify(resolved)};
runtime.injectIntoGlobalHook(window);
Expand Down
10 changes: 5 additions & 5 deletions playground/rolldown-dev-react/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import React from 'react'
import ReactDOMClient from 'react-dom/client'
import { App } from './app.tsx'

createRoot(document.getElementById('root')!).render(
<StrictMode>
ReactDOMClient.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</StrictMode>,
</React.StrictMode>,
)
5 changes: 4 additions & 1 deletion playground/rolldown-dev-react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { defineConfig } from 'vite'
export default defineConfig({
clearScreen: false,
experimental: {
rolldownDev: { hmr: true, reactRefresh: true },
rolldownDev: {
hmr: true,
reactRefresh: true,
},
},
})

0 comments on commit a282d91

Please sign in to comment.