-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-preact): integrate
@swc/plugin-prefresh
to enhance pref…
…resh (#3861)
- Loading branch information
1 parent
6f455f2
commit cfd2467
Showing
7 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { dev, rspackOnlyTest } from '@e2e/helper'; | ||
import { expect, test } from '@playwright/test'; | ||
|
||
rspackOnlyTest( | ||
'HMR should work properly with `createContext`', | ||
async ({ page }) => { | ||
// HMR cases will fail in Windows | ||
if (process.platform === 'win32') { | ||
test.skip(); | ||
} | ||
|
||
const root = __dirname; | ||
const compFilePath = path.join(root, 'src/test-temp-B.jsx'); | ||
const compSourceCode = `const B = (props) => { | ||
return <div id="B">B: {props.count}</div>; | ||
}; | ||
export default B; | ||
`; | ||
|
||
fs.writeFileSync(compFilePath, compSourceCode, 'utf-8'); | ||
|
||
const rsbuild = await dev({ | ||
cwd: root, | ||
page, | ||
}); | ||
|
||
const a = page.locator('#A'); | ||
const b = page.locator('#B'); | ||
|
||
await expect(a).toHaveText('A: 0'); | ||
await expect(b).toHaveText('B: 0'); | ||
|
||
await a.click({ clickCount: 5 }); | ||
await expect(a).toHaveText('A: 5'); | ||
await expect(b).toHaveText('B: 5'); | ||
|
||
// simulate a change to component B's source code | ||
fs.writeFileSync( | ||
compFilePath, | ||
compSourceCode.replace('B:', 'Beep:'), | ||
'utf-8', | ||
); | ||
|
||
await page.waitForFunction(() => { | ||
const aText = document.querySelector('#A')!.textContent; | ||
const bText = document.querySelector('#B')!.textContent; | ||
|
||
return ( | ||
// the state (count) of A should be kept | ||
aText === 'A: 5' && | ||
// content of B changed to `Beep: 5` means HMR has taken effect | ||
bText === 'Beep: 5' | ||
); | ||
}); | ||
|
||
await rsbuild.close(); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { pluginPreact } from '@rsbuild/plugin-preact'; | ||
|
||
export default { | ||
plugins: [ | ||
pluginPreact({ | ||
exclude: [ | ||
/node_modules/, | ||
// exclude Rsbuild internal HMR client | ||
/packages\/core\/dist/, | ||
], | ||
}), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createContext } from 'preact'; | ||
import { useContext, useState } from 'preact/hooks'; | ||
import B from './test-temp-B'; | ||
|
||
const MyContext = createContext(); | ||
|
||
export const MyProvider = ({ children }) => { | ||
const [value, setValue] = useState(0); | ||
return ( | ||
<MyContext.Provider value={{ value, setValue }}> | ||
{children} | ||
</MyContext.Provider> | ||
); | ||
}; | ||
|
||
const App = () => { | ||
const { value, setValue } = useContext(MyContext); | ||
return ( | ||
<> | ||
<button id="A" type="button" onClick={() => setValue(value + 1)}> | ||
A: {value} | ||
</button> | ||
<B count={value} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default () => { | ||
return ( | ||
<MyProvider> | ||
<App /> | ||
</MyProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { h, render } from 'preact'; | ||
import A from './A'; | ||
|
||
render(h(A), document.getElementById('root')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
cfd2467
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open