Skip to content

Commit

Permalink
feat(plugin-preact): integrate @swc/plugin-prefresh to enhance pref…
Browse files Browse the repository at this point in the history
…resh (#3861)
  • Loading branch information
chenjiahan authored Oct 29, 2024
1 parent 6f455f2 commit cfd2467
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 1 deletion.
61 changes: 61 additions & 0 deletions e2e/cases/preact/prefresh-context/index.test.ts
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();
},
);
13 changes: 13 additions & 0 deletions e2e/cases/preact/prefresh-context/rsbuild.config.ts
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/,
],
}),
],
};
34 changes: 34 additions & 0 deletions e2e/cases/preact/prefresh-context/src/A.jsx
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>
);
};
4 changes: 4 additions & 0 deletions e2e/cases/preact/prefresh-context/src/index.js
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'));
3 changes: 2 additions & 1 deletion packages/plugin-preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"dependencies": {
"@prefresh/core": "^1.5.2",
"@prefresh/utils": "^1.2.0",
"@rspack/plugin-preact-refresh": "^1.1.0"
"@rspack/plugin-preact-refresh": "^1.1.0",
"@swc/plugin-prefresh": "^3.0.3"
},
"devDependencies": {
"@rsbuild/core": "workspace:*",
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export const pluginPreact = (
tools: {
swc: {
jsc: {
experimental: {
plugins: usePrefresh
? [[require.resolve('@swc/plugin-prefresh'), {}]]
: undefined,
},
parser: {
syntax: 'typescript',
// enable supports for JSX/TSX compilation
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

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

1 comment on commit cfd2467

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
plugins ✅ success
rspress ✅ success
rslib ✅ success
examples ✅ success

Please sign in to comment.