Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support vue hmr #8

Open
wants to merge 5 commits into
base: refactor-rolldown-rebuild
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ class RolldownEnvironment extends DevEnvironment {
assert(this._pluginContainer)
this._pluginContainer.buildStart = async () => {}
this._pluginContainer.close = async () => {}
}

// delay build till listen since some plugins expect `configureServer` before build
override listen: DevEnvironment['listen'] = async () => {
await this.build()
}

Expand Down Expand Up @@ -205,6 +209,7 @@ class RolldownEnvironment extends DevEnvironment {
'vite:css',
'vite:css-post',
'vite:asset',
'vite:vue',
].includes(p.name) ||
[
'AliasPlugin',
Expand All @@ -230,6 +235,21 @@ class RolldownEnvironment extends DevEnvironment {
patchRuntimePlugin(this),
patchCssPlugin(),
reactRefreshPlugin(),
{
// TODO: import.meta not supported by app format
name: 'patch-import-meta',
transform: {
filter: {
code: [/import\.meta\.hot/],
},
handler(code) {
const output = new MagicString(code)
output.replaceAll('import.meta.hot.accept', 'module.hot.accept')
output.replaceAll('import.meta.hot.on', 'self.__rolldown_hot.on')
return { code: output.toString(), map: output.generateMap() }
},
},
},
],
moduleTypes: {
'.css': 'js',
Expand Down Expand Up @@ -311,6 +331,12 @@ class RolldownEnvironment extends DevEnvironment {
if (!this.fileModuleIds.has(ctx.file)) {
return
}
for (const plugin of this.plugins) {
// TODO: for now, simple hack for vue hmr
if (plugin.name === 'vite:vue') {
;(plugin.handleHotUpdate as any)(ctx)
}
}
if (
this.rolldownDevOptions.hmr ||
this.rolldownDevOptions.ssrModuleRunner
Expand Down
18 changes: 18 additions & 0 deletions playground/rolldown-dev-vue/__tests__/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { test } from 'vitest'
import { editFile, isBuild, page, viteTestUrl } from '../../test-utils'

test('basic', async () => {
await page.getByRole('button', { name: 'Count: 0' }).click()
await page.getByRole('button', { name: 'Count: 1' }).click()
})

test.runIf(!isBuild)('hmr js', async () => {
await page.goto(viteTestUrl)
await page.getByRole('button', { name: 'Count: 0' }).click()

editFile('./src/App.vue', (s) => s.replace('Count:', 'Count-x:'))
await page.getByRole('button', { name: 'Count-x: 1' }).click()

editFile('./src/App.vue', (s) => s.replace('Count-x:', 'Count:'))
await page.getByRole('button', { name: 'Count: 2' }).click()
})
10 changes: 10 additions & 0 deletions playground/rolldown-dev-vue/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions playground/rolldown-dev-vue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@vitejs/test-rolldown-dev-vue",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.5.13"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.2.1"
}
}
11 changes: 11 additions & 0 deletions playground/rolldown-dev-vue/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
<h1>Vite + Vue</h1>
<button type="button" @click="count++">Count: {{ count }}</button>
</template>

<!-- TODO: scoped css -->
4 changes: 4 additions & 0 deletions playground/rolldown-dev-vue/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createApp } from 'vue'
import App from './App.vue'

createApp(App).mount('#app')
12 changes: 12 additions & 0 deletions playground/rolldown-dev-vue/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
clearScreen: false,
experimental: {
rolldownDev: {
hmr: true,
},
},
plugins: [vue()],
})
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

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

Loading