Forked from christopher4lis/hot-reload-extracted-stylesheets
Created
March 4, 2018 16:39
-
-
Save kousherAlam/2b305f2706e7bb70c5deddc70ffb8301 to your computer and use it in GitHub Desktop.
This snippet allows webpack-dev-server to hot reload stylesheets extracted with the ExtractTextWebpackPlugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (module.hot) { | |
const hotEmitter = require("webpack/hot/emitter"); | |
const DEAD_CSS_TIMEOUT = 2000; | |
hotEmitter.on("webpackHotUpdate", function(currentHash) { | |
document.querySelectorAll("link[href][rel=stylesheet]").forEach((link) => { | |
const nextStyleHref = link.href.replace(/(\?\d+)?$/, `?${Date.now()}`); | |
const newLink = link.cloneNode(); | |
newLink.href = nextStyleHref; | |
link.parentNode.appendChild(newLink); | |
setTimeout(() => { | |
link.parentNode.removeChild(link); | |
}, DEAD_CSS_TIMEOUT); | |
}); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment