forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hot module replacement with code splitting webpack#26
- Loading branch information
Showing
6 changed files
with
133 additions
and
77 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
if(module.hot) { | ||
window.onmessage = function(event) { | ||
if(event.data === "webpackHotUpdate" && module.hot.status() === "idle") { | ||
module.hot.check(function(err, updatedModules) { | ||
if(err) { | ||
if(module.hot.status() in {abort:1,fail:1}) | ||
window.location.reload(); | ||
else | ||
console.warn("Update failed: " + err); | ||
return; | ||
} | ||
function check() { | ||
module.hot.check(function(err, updatedModules) { | ||
if(err) { | ||
if(module.hot.status() in {abort:1,fail:1}) | ||
window.location.reload(); | ||
else | ||
console.warn("Update failed: " + err); | ||
return; | ||
} | ||
|
||
if(!updatedModules) | ||
return console.log("No Update found."); | ||
|
||
check(); | ||
|
||
if(!updatedModules || updatedModules.length === 0) | ||
return console.log("Update is empty."); | ||
console.log("Updated modules:"); | ||
updatedModules.forEach(function(moduleId) { | ||
console.log(" - " + moduleId); | ||
}); | ||
if(!updatedModules || updatedModules.length === 0) | ||
return console.log("Update is empty."); | ||
console.log("Updated modules:"); | ||
updatedModules.forEach(function(moduleId) { | ||
console.log(" - " + moduleId); | ||
}); | ||
|
||
}); | ||
} | ||
window.onmessage = function(event) { | ||
if(event.data === "webpackHotUpdate" && module.hot.status() === "idle") { | ||
check(); | ||
} | ||
}; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Tobias Koppers @sokra | ||
*/ | ||
module.exports = function(cssCode) { | ||
var styleElement = document.createElement("style"); | ||
styleElement.type = "text/css"; | ||
if (styleElement.styleSheet) { | ||
styleElement.styleSheet.cssText = cssCode; | ||
} else { | ||
styleElement.appendChild(document.createTextNode(cssCode)); | ||
} | ||
var head = document.getElementsByTagName("head")[0]; | ||
head.appendChild(styleElement); | ||
return function() { | ||
head.removeChild(styleElement); | ||
}; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,11 @@ | ||
// This file can update, because it accept itself. | ||
// A dispose handler removes the old <style> element. | ||
|
||
var cssCode = "body { background: green; }"; | ||
var addStyle = require("./addStyle"); | ||
|
||
var head = document.getElementsByTagName("head")[0]; | ||
|
||
var styleElement = document.createElement("style"); | ||
styleElement.type = "text/css"; | ||
if (styleElement.styleSheet) { | ||
styleElement.styleSheet.cssText = cssCode; | ||
} else { | ||
styleElement.appendChild(document.createTextNode(cssCode)); | ||
} | ||
head.appendChild(styleElement); | ||
var dispose = addStyle("body { background: green; }"); | ||
|
||
if(module.hot) { | ||
module.hot.accept(); | ||
module.hot.dispose(function() { | ||
head.removeChild(styleElement); | ||
}); | ||
module.hot.dispose(dispose); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// This file can update, because it accept itself. | ||
// A dispose handler removes the old <style> element. | ||
|
||
var addStyle = require("./addStyle"); | ||
|
||
var dispose = addStyle("body { color: blue; }"); | ||
|
||
if(module.hot) { | ||
module.hot.accept(); | ||
module.hot.dispose(dispose); | ||
} |