Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 676942a

Browse files
committed
HMR take 1
working, but double init
1 parent 2db50ee commit 676942a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ function ExtractTextPluginCompilation() {
1616
}
1717

1818
ExtractTextPlugin.prototype.mergeNonInitialChunks = function(chunk, intoChunk, checkedChunks) {
19+
if (chunk.chunks) {
20+
// Fix error when hot module replacement used with CommonsChunkPlugin
21+
chunk.chunks = chunk.chunks.filter(function(c) {
22+
return typeof c !== 'undefined';
23+
})
24+
}
25+
1926
if(!intoChunk) {
2027
checkedChunks = [];
2128
chunk.chunks.forEach(function(c) {
@@ -285,7 +292,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
285292
callback();
286293
}.bind(this));
287294
}.bind(this));
288-
compilation.plugin("additional-assets", function(callback) {
295+
compilation.plugin("before-chunk-assets", function() {
289296
extractedChunks.forEach(function(extractedChunk) {
290297
if(extractedChunk.modules.length) {
291298
extractedChunk.modules.sort(function(a, b) {
@@ -304,9 +311,15 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
304311
});
305312
compilation.assets[file] = source;
306313
chunk.files.push(file);
314+
315+
// Hot module replacement
316+
extractedChunk.modules.forEach(function(module){
317+
var originalModule = module.getOriginalModule();
318+
originalModule._source._value = originalModule._source._value.replace('%%extracted-file%%', file);
319+
originalModule._source._value = originalModule._source._value.replace('%%extracted-hash%%', compilation.hash);
320+
});
307321
}
308322
}, this);
309-
callback();
310323
}.bind(this));
311324
}.bind(this));
312325
};

loader.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ module.exports.pitch = function(request) {
105105
});
106106
});
107107
this[__dirname](text, query);
108-
if(text.locals && typeof resultSource !== "undefined") {
109-
resultSource += "\nmodule.exports = " + JSON.stringify(text.locals) + ";";
108+
if(typeof resultSource !== "undefined") {
109+
if (text.locals) {
110+
resultSource += "\nmodule.exports = " + JSON.stringify(text.locals) + ";";
111+
}
112+
resultSource += "\nfunction replaceStylesheet(styleSheet,url){styleSheet.href=url;console.log('[HMR]','Reload css: ',url)}\nfunction cssReload(compilationHash,publicPath,outputFilename){if(document){var styleSheets=document.getElementsByTagName('link');for(var i=0;i<styleSheets.length;i++){if(styleSheets[i].href){var hrefUrl=styleSheets[i].href.split('?');var href=hrefUrl[0];var hash=hrefUrl[1];if(hash!==compilationHash&&href===document.location.origin+publicPath+outputFilename){replaceStylesheet(styleSheets[i],href+'?'+compilationHash);break}}}}}\nif(module.hot){\nmodule.hot.accept();\ncssReload('%%extracted-hash%%','" + publicPath + "','%%extracted-file%%');}";
110113
}
111114
} catch(e) {
112115
return callback(e);

0 commit comments

Comments
 (0)