Skip to content

Commit

Permalink
fix: avoid reload all css when hot load
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Mar 21, 2024
1 parent 8bf0ad6 commit 3695ac6
Show file tree
Hide file tree
Showing 6 changed files with 1,661 additions and 11 deletions.
25 changes: 17 additions & 8 deletions src/loader.js
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) {
const localsJsonString = ${localsJsonString};
// ${Date.now()}
var cssReload = require(${stringifyRequest(
const 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}
// 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
7 changes: 7 additions & 0 deletions test/cases/hmr-locals/expected/main.css
@@ -0,0 +1,7 @@
/*!************************************************************************************************!*\
!*** css ../../../node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[0].use[1]!./index.css ***!
\************************************************************************************************/
.VoofDB21D_QzDbRdwMiY {
color: red;
}

0 comments on commit 3695ac6

Please sign in to comment.