Skip to content

Commit

Permalink
fix: avoid reloading all csses when hot load (#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe authored Apr 16, 2024
1 parent e73061d commit 1a56673
Show file tree
Hide file tree
Showing 9 changed files with 1,786 additions and 18 deletions.
38 changes: 24 additions & 14 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,31 @@ const MiniCssExtractPlugin = require("./index");
* @returns {string}
*/
function hotLoader(content, context) {
const accept = context.locals
? ""
: "module.hot.accept(undefined, cssReload);";

const localsJsonString = JSON.stringify(JSON.stringify(context.locals));
return `${content}
if(module.hot) {
// ${Date.now()}
var cssReload = require(${stringifyRequest(
context.loaderContext,
path.join(__dirname, "hmr/hotModuleReplacement.js")
)})(module.id, ${JSON.stringify({
...context.options,
locals: !!context.locals,
})});
module.hot.dispose(cssReload);
${accept}
(function() {
var localsJsonString = ${localsJsonString};
// ${Date.now()}
var cssReload = require(${stringifyRequest(
context.loaderContext,
path.join(__dirname, "hmr/hotModuleReplacement.js")
)})(module.id, ${JSON.stringify(context.options)});
// only invalidate when locals change
if (
module.hot.data &&
module.hot.data.value &&
module.hot.data.value !== localsJsonString
) {
module.hot.invalidate();
} else {
module.hot.accept();
}
module.hot.dispose(function(data) {
data.value = localsJsonString;
cssReload();
});
})();
}
`;
}
Expand Down Expand Up @@ -563,3 +572,4 @@ function loader(content) {

module.exports = loader;
module.exports.pitch = pitch;
module.exports.hotLoader = hotLoader;
26 changes: 26 additions & 0 deletions test/HMR.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/* eslint-disable no-console */

import hotModuleReplacement from "../src/hmr/hotModuleReplacement";
import { hotLoader } from "../src/loader";

function getLoadEvent() {
const event = document.createEvent("Event");
Expand Down Expand Up @@ -361,4 +362,29 @@ describe("HMR", () => {
done();
}, 100);
});

it("hotLoader works for non-locals", () => {
const o = Date.now;
Date.now = () => 1;
const code = hotLoader("//content;", {
loaderContext: {
context: __dirname,
},
});
Date.now = o;
expect(code).toMatchSnapshot();
});

it("hotLoader works for locals", () => {
const o = Date.now;
Date.now = () => 1;
const code = hotLoader("//content;", {
loaderContext: {
context: __dirname,
},
locals: { foo: "bar" },
});
Date.now = o;
expect(code).toMatchSnapshot();
});
});
52 changes: 52 additions & 0 deletions test/__snapshots__/HMR.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`HMR hotLoader works for locals 1`] = `
"//content;
if(module.hot) {
(function() {
var localsJsonString = \\"{\\\\\\"foo\\\\\\":\\\\\\"bar\\\\\\"}\\";
// 1
var cssReload = require(\\"../src/hmr/hotModuleReplacement.js\\")(module.id, undefined);
// only invalidate when locals change
if (
module.hot.data &&
module.hot.data.value &&
module.hot.data.value !== localsJsonString
) {
module.hot.invalidate();
} else {
module.hot.accept();
}
module.hot.dispose(function(data) {
data.value = localsJsonString;
cssReload();
});
})();
}
"
`;

exports[`HMR hotLoader works for non-locals 1`] = `
"//content;
if(module.hot) {
(function() {
var localsJsonString = undefined;
// 1
var cssReload = require(\\"../src/hmr/hotModuleReplacement.js\\")(module.id, undefined);
// only invalidate when locals change
if (
module.hot.data &&
module.hot.data.value &&
module.hot.data.value !== localsJsonString
) {
module.hot.invalidate();
} else {
module.hot.accept();
}
module.hot.dispose(function(data) {
data.value = localsJsonString;
cssReload();
});
})();
}
"
`;

exports[`HMR should handle error event 1`] = `"[HMR] css reload %s"`;

exports[`HMR should handle error event 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
Expand Down
7 changes: 7 additions & 0 deletions test/cases/hmr-locals/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*!************************************************************************************************!*\
!*** css ../../../node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[0].use[1]!./index.css ***!
\************************************************************************************************/
.VoofDB21D_QzDbRdwMiY {
color: red;
}

Loading

0 comments on commit 1a56673

Please sign in to comment.