Skip to content

Commit

Permalink
fix: perform a HMR update after inserting a new CSS module
Browse files Browse the repository at this point in the history
Fixes #706
  • Loading branch information
latin-1 committed Mar 13, 2023
1 parent 1d226c1 commit 048c8f1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/hmr/hotModuleReplacement.js
Expand Up @@ -246,6 +246,8 @@ function isUrlRequest(url) {
return true;
}

const updateFunctionMap = Object.create(null);

/**
* @param {TODO} moduleId
* @param {TODO} options
Expand All @@ -258,6 +260,11 @@ module.exports = function (moduleId, options) {
return noop;
}

const key = JSON.stringify({ moduleId, options });
if (updateFunctionMap[key]) {
return updateFunctionMap[key];
}

const getScriptSrc = getCurrentScriptUrl(moduleId);

function update() {
Expand All @@ -281,5 +288,6 @@ module.exports = function (moduleId, options) {
}
}

return debounce(update, 50);
updateFunctionMap[key] = debounce(update, 50);
return updateFunctionMap[key];
};
3 changes: 3 additions & 0 deletions src/loader.js
Expand Up @@ -60,6 +60,9 @@ function hotLoader(content, context) {
})});
module.hot.dispose(cssReload);
${accept}
if (module.hot.status() !== "idle") {
cssReload();
}
}
`;
}
Expand Down
13 changes: 12 additions & 1 deletion test/cases/hmr/expected/main.js
Expand Up @@ -255,6 +255,8 @@ function isUrlRequest(url) {
return true;
}

const updateFunctionMap = Object.create(null);

/**
* @param {TODO} moduleId
* @param {TODO} options
Expand All @@ -267,6 +269,11 @@ module.exports = function (moduleId, options) {
return noop;
}

const key = JSON.stringify({ moduleId, options });
if (updateFunctionMap[key]) {
return updateFunctionMap[key];
}

const getScriptSrc = getCurrentScriptUrl(moduleId);

function update() {
Expand All @@ -290,7 +297,8 @@ module.exports = function (moduleId, options) {
}
}

return debounce(update, 50);
updateFunctionMap[key] = debounce(update, 50);
return updateFunctionMap[key];
};


Expand Down Expand Up @@ -367,6 +375,9 @@ __webpack_require__.r(__webpack_exports__);
var cssReload = __webpack_require__(/*! ../../../src/hmr/hotModuleReplacement.js */ "../../../src/hmr/hotModuleReplacement.js")(module.id, {"locals":false});
module.hot.dispose(cssReload);
module.hot.accept(undefined, cssReload);
if (module.hot.status() !== "idle") {
cssReload();
}
}


Expand Down

0 comments on commit 048c8f1

Please sign in to comment.