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: HMR behavior with CSS Modules #7434

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/core/integration-tests/test/hmr.js
Expand Up @@ -830,5 +830,68 @@ module.hot.dispose((data) => {
}
}
});

it('should handle CSS Modules update correctly', async () => {
let testDir = path.join(__dirname, '/input');
await overlayFS.rimraf(testDir);
await overlayFS.mkdirp(testDir);
await ncp(path.join(__dirname, '/integration/hmr-css-modules'), testDir);

let port = await getPort();
let b = bundler(path.join(testDir, 'index.html'), {
inputFS: overlayFS,
outputFS: overlayFS,
serveOptions: {
https: false,
port,
host: '127.0.0.1',
},
hmrOptions: {port},
shouldContentHash: false,
config,
});

subscription = await b.watch();
let bundleEvent = await getNextBuild(b);
assert.equal(bundleEvent.type, 'buildSuccess');

let window;
try {
let dom = await JSDOM.JSDOM.fromURL(
'http://127.0.0.1:' + port + '/index.html',
{
runScripts: 'dangerously',
resources: 'usable',
pretendToBeVisual: true,
},
);
let _window = (window = dom.window); // For Flow
window.WebSocket = WebSocket;
await new Promise(res =>
dom.window.document.addEventListener('load', () => {
res();
}),
);
_window.console.clear = () => {};
_window.console.warn = () => {};

let initialHref = _window.document.querySelector('link').href;

await overlayFS.copyFile(
path.join(testDir, 'index2.module.css'),
path.join(testDir, 'index.module.css'),
);
assert.equal((await getNextBuild(b)).type, 'buildSuccess');
await sleep(200);

let newHref = _window.document.querySelector('link').href;

assert.notStrictEqual(initialHref, newHref);
} finally {
if (window) {
window.close();
}
}
});
});
});
@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="app"></div>
<script type="module" src="index.jsx"></script>
</body>
</html>
@@ -0,0 +1,3 @@
import * as styles from "./index.module.css";

const Hello = () => <div classNames={styles.hello}>hello</div>;
@@ -0,0 +1,3 @@
.hello {
background: red;
}
@@ -0,0 +1,3 @@
.hello {
background: blue;
}
2 changes: 1 addition & 1 deletion packages/reporters/dev-server/src/HMRServer.js
Expand Up @@ -100,7 +100,7 @@ export default class HMRServer {

let queue = new PromiseQueue({maxConcurrent: FS_CONCURRENCY});
for (let asset of changedAssets) {
if (asset.type !== 'js') {
if (asset.type !== 'js' && asset.type !== 'css') {
// If all of the incoming dependencies of the asset actually resolve to a JS asset
// rather than the original, we can mark the runtimes as changed instead. URL runtimes
// have a cache busting query param added with HMR enabled which will trigger a reload.
Expand Down