Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ export default function viteReact(opts: Options = {}): Plugin[] {
{
tag: 'script',
attrs: { type: 'module' },
children: getPreambleCode(base),
// In bundled dev mode, Rolldown resolves module specifiers at build
// time without URL-level base stripping, so we must use '/' instead
// of config.base to match the resolveId hook for '/@react-refresh'.
children: getPreambleCode('/'),
},
]
},
Expand Down
15 changes: 15 additions & 0 deletions playground/bundled-dev-base-path/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useState } from 'react'

function App() {
const [count, setCount] = useState(0)
return (
<div>
<h1>bundledDev + base path</h1>
<button id="state-button" onClick={() => setCount((c) => c + 1)}>
count is: {count}
</button>
</div>
)
}

export default App
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, test } from 'vitest'
import { browserErrors, isServe, page, viteTestUrl } from '~utils'

// Regression test for https://github.com/vitejs/vite-plugin-react/issues/1190
// bundledDev mode with a non-root base should not fail to resolve /@react-refresh.
test.runIf(isServe)(
'should load without UNRESOLVED_IMPORT error for @react-refresh',
async () => {
await page.goto(viteTestUrl)
// bundledDev shows "Bundling in progress" until the first bundle is ready.
await page.waitForSelector('h1')
await expect
.poll(() => page.textContent('h1'))
.toMatch('bundledDev + base path')
expect(browserErrors).toHaveLength(0)
},
)

test.runIf(isServe)('should render and update state', async () => {
await page.goto(viteTestUrl)
await page.waitForSelector('#state-button')
await expect
.poll(() => page.textContent('#state-button'))
.toMatch('count is: 0')
await page.click('#state-button')
await expect
.poll(() => page.textContent('#state-button'))
.toMatch('count is: 1')
})
10 changes: 10 additions & 0 deletions playground/bundled-dev-base-path/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div id="app"></div>
<script type="module">
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'

ReactDOM.createRoot(document.getElementById('app')).render(
React.createElement(App),
)
</script>
17 changes: 17 additions & 0 deletions playground/bundled-dev-base-path/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@vitejs/test-bundled-dev-base-path",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.2.5",
"react-dom": "^19.2.5"
},
"devDependencies": {
"@vitejs/plugin-react": "workspace:*"
}
}
13 changes: 13 additions & 0 deletions playground/bundled-dev-base-path/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import react from '@vitejs/plugin-react'
import type { UserConfig } from 'vite'

const config: UserConfig = {
server: { port: 8930 /* Should be unique */ },
mode: 'development',
base: '/static/',
plugins: [react()],
experimental: { bundledDev: true },
build: { minify: false },
}

export default config
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading