Skip to content

Commit

Permalink
wip: css hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 13, 2024
1 parent 7a53b1f commit 2051f52
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin {
!inlined &&
dataToEsm(modules, { namedExports: true, preferConst: true })

if (config.command === 'serve' && !config.experimental.rolldownDev) {
if (config.command === 'serve') {
const getContentWithSourcemap = async (content: string) => {
if (config.css?.devSourcemap) {
const sourcemap = this.getCombinedSourcemap()
Expand Down
45 changes: 38 additions & 7 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ export function rolldownDevHandleConfig(
reactRefresh: config.experimental?.rolldownDev?.reactRefresh,
}),
},
// NOTE
// this is not "build" option any more (or is there a way to handle entry lazily?)
build: {
modulePreload: false,
cssCodeSplit: false,
assetsInlineLimit: 0,
rollupOptions: {
input:
Expand Down Expand Up @@ -192,9 +189,12 @@ class RolldownEnvironment extends DevEnvironment {
'vite:css-post',
'vite:asset',
].includes(p.name) ||
['AliasPlugin', 'TransformPlugin', 'LoadFallbackPlugin'].includes(
p.constructor.name,
),
[
'AliasPlugin',
'TransformPlugin',
'LoadFallbackPlugin',
'ManifestPlugin',
].includes(p.constructor.name),
)
plugins = plugins.map((p) => injectEnvironmentToHooks(this as any, p))

Expand All @@ -212,8 +212,12 @@ class RolldownEnvironment extends DevEnvironment {
plugins: [
...plugins,
patchRuntimePlugin(this.rolldownDevOptions),
patchCssPlugin(),
reactRefreshPlugin(),
],
moduleTypes: {
'.css': 'js',
},
}
this.instance = await rolldown.rolldown(inputOptions)

Expand Down Expand Up @@ -300,6 +304,32 @@ function patchRuntimePlugin(
}
}

// patch vite:css transform for hmr
function patchCssPlugin(): rolldown.Plugin {
return {
name: 'vite:rolldown-patch-css',
transform: {
filter: {
id: {
include: ['*.css'],
},
code: {
include: ['__vite__updateStyle'],
},
},
handler(code, id) {
// TODO: import.meta.hot.prune
const cssCode = code.match(/^const __vite__css = (.*)$/m)![1]
const jsCode = `
__rolldown_updateStyle(${JSON.stringify(id)}, ${cssCode});
module.hot.accept();
`
return { code: jsCode, moduleSideEffects: true }
},
},
}
}

// reuse /@vite/client for Websocket API
function getRolldownClientCode() {
let code = fs.readFileSync(CLIENT_ENTRY, 'utf-8')
Expand Down Expand Up @@ -331,7 +361,8 @@ const hot = createHotContext("/__rolldown");
hot.on("rolldown:hmr", (data) => {
(0, eval)(data[1]);
});
window.__rolldown_hot = hot;
self.__rolldown_hot = hot;
self.__rolldown_updateStyle = updateStyle;
`
return `(() => {/*** @vite/client ***/\n${code}}\n)()`
}
Expand Down
29 changes: 28 additions & 1 deletion playground/rolldown-dev-react/__tests__/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('style', async () => {
).toBe('rgb(255, 165, 0)')
})

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

Expand All @@ -38,3 +38,30 @@ test.runIf(!isBuild)('hmr', async () => {
editFile('./src/app.tsx', (s) => s.replace('Count-x:', 'Count-x-y:'))
await page.getByRole('button', { name: 'Count-x-y: 2' }).click()
})

test.runIf(!isBuild)('hmr css', async () => {
await page.goto(viteTestUrl)

await expect
.poll(() =>
page.locator('.test-style').evaluate((el) => getComputedStyle(el).color),
)
.toBe('rgb(255, 165, 0)')
await page.getByRole('button', { name: 'Count: 0' }).click()

editFile('./src/test-style.css', (s) => s.replace('orange', 'blue'))
await expect
.poll(() =>
page.locator('.test-style').evaluate((el) => getComputedStyle(el).color),
)
.toBe('rgb(0, 0, 255)')
await page.getByRole('button', { name: 'Count: 1' }).click()

editFile('./src/test-style.css', (s) => s.replace('blue', 'green'))
await expect
.poll(() =>
page.locator('.test-style').evaluate((el) => getComputedStyle(el).color),
)
.toBe('rgb(0, 128, 0)')
await page.getByRole('button', { name: 'Count: 2' }).click()
})
2 changes: 1 addition & 1 deletion playground/rolldown-dev-react/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import virtualTest from 'virtual:test'
// @ts-expect-error no type
import testAlias from 'test-alias'
import { throwError } from './error'
// TODO: hmr
import './test-style.css'
// TODO: hmr for assets?
import testStyleUrl from './test-style-url.css?url'
import testStyleInline from './test-style-inline.css?inline'

Expand Down

0 comments on commit 2051f52

Please sign in to comment.