Created
June 7, 2017 19:20
-
-
Save tfoxy/3b335a41adeb6b08d6ae6315ceb5f12a to your computer and use it in GitHub Desktop.
Hot extract text loader plugin for webpack 1.x
This file contains hidden or 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
| const loaderUtils = require('loader-utils'); | |
| const extractTextLoader = require('extract-text-webpack-plugin/loader'); | |
| function hotExtractedCss() { | |
| if (module.hot) { | |
| module.hot.accept(); | |
| if (module.hot.data) { | |
| console.log('Reloading css...'); | |
| Array.prototype.forEach.call(document.querySelectorAll('link[href][rel=stylesheet]'), function(link) { | |
| var nextStyleHref = link.href.replace(/(\?\d+)?$/, '?' + Date.now()); | |
| var newLink = link.cloneNode(); | |
| newLink.href = nextStyleHref; | |
| link.parentNode.appendChild(newLink); | |
| setTimeout(function() { | |
| link.parentNode.removeChild(link); | |
| }, 800); | |
| }); | |
| } | |
| } | |
| } | |
| const pitch = extractTextLoader.pitch; | |
| extractTextLoader.pitch = function hotPitch(request) { | |
| const moduleName = loaderUtils.stringifyRequest(this, "!!" + request); | |
| const async = this.async; | |
| this.async = function hotAsync() { | |
| const callback = async.apply(this, arguments); | |
| function hotCallback(err, resultSource) { | |
| if (resultSource && resultSource.startsWith('// removed by extract-text-webpack-plugin')) { | |
| arguments[1] = | |
| `(${hotExtractedCss.toString()})(${moduleName})\n${resultSource}`; | |
| } | |
| return callback.apply(this, arguments); | |
| } | |
| return hotCallback; | |
| }; | |
| return pitch.apply(this, arguments); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment