Skip to content

Commit e3f5bbe

Browse files
committed
fixed hot handling
it now bubbles when the css failed fixes webpack-contrib#11 fixes webpack-contrib#12 fixes webpack-contrib#13
1 parent 175cc51 commit e3f5bbe

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

addStyle.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ module.exports = function addStyle(cssCode) {
1515
} else {
1616
styleElement.appendChild(document.createTextNode(cssCode));
1717
}
18-
return function() {
19-
head.removeChild(styleElement);
20-
};
18+
if(module.hot) {
19+
return function(cssCode) {
20+
if(typeof cssCode === "string") {
21+
if (styleElement.styleSheet) {
22+
styleElement.styleSheet.cssText = cssCode;
23+
} else {
24+
styleElement.childNodes[0].nodeValue = cssCode;
25+
}
26+
} else {
27+
head.removeChild(styleElement);
28+
}
29+
};
30+
}
2131
}

addStyleUrl.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ module.exports = function addStyleUrl(cssUrl) {
1212
styleElement.href = cssUrl;
1313
var head = document.getElementsByTagName("head")[0];
1414
head.appendChild(styleElement);
15-
return function() {
16-
head.removeChild(styleElement);
17-
};
15+
if(module.hot) {
16+
return function(cssUrl) {
17+
if(typeof cssUrl === "string") {
18+
styleElement.href = cssUrl;
19+
} else {
20+
head.removeChild(styleElement);
21+
}
22+
};
23+
}
1824
}

index.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ var path = require("path");
66
module.exports = function() {};
77
module.exports.pitch = function(remainingRequest) {
88
this.cacheable && this.cacheable();
9-
var comment1 = "// style-loader: Adds some css to the DOM by adding a <style> tag\n";
10-
var addStyleCode = "var dispose = require(" + JSON.stringify("!" + path.join(__dirname, "addStyle.js")) + ")\n";
11-
var comment2 = "\t// The css code:\n";
12-
var cssCodeRequest = "require(" + JSON.stringify("!!" + remainingRequest) + ")";
13-
var comment3 = "// Hot Module Replacement\n";
14-
var hmrCode = "if(module.hot) {\n\tmodule.hot.accept();\n\tmodule.hot.dispose(dispose);\n}";
15-
return comment1 + addStyleCode + comment2 + "\t(" + cssCodeRequest + ");\n" + comment3 + hmrCode;
9+
return [
10+
"// style-loader: Adds some css to the DOM by adding a <style> tag",
11+
"var update = require(" + JSON.stringify("!" + path.join(__dirname, "addStyle.js")) + ")(",
12+
"\trequire(" + JSON.stringify("!!" + remainingRequest) + ")",
13+
");",
14+
"// Hot Module Replacement",
15+
"if(module.hot) {",
16+
"\tmodule.hot.accept(" + JSON.stringify("!!" + remainingRequest) + ", function() {",
17+
"\t\tupdate(require(" + JSON.stringify("!!" + remainingRequest) + "));",
18+
"\t});",
19+
"\tmodule.hot.dispose(function() { update(); });",
20+
"}"
21+
].join("\n");
1622
};

url.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ var path = require("path");
66
module.exports = function() {};
77
module.exports.pitch = function(remainingRequest) {
88
this.cacheable && this.cacheable();
9-
var comment1 = "// style-loader: Adds some css to the DOM by adding a <style> tag\n";
10-
var addStyleCode = "var dispose = require(" + JSON.stringify("!" + path.join(__dirname, "addStyleUrl.js")) + ")\n";
11-
var comment2 = "\t// The url to the css code:\n";
12-
var cssUrlRequest = "require(" + JSON.stringify("!!" + remainingRequest) + ")";
13-
var comment3 = "// Hot Module Replacement\n";
14-
var hmrCode = "if(module.hot) {\n\tmodule.hot.accept();\n\tmodule.hot.dispose(dispose);\n}";
15-
return comment1 + addStyleCode + comment2 + "\t(" + cssUrlRequest + ");\n" + comment3 + hmrCode;
9+
return [
10+
"// style-loader: Adds some reference to a css file to the DOM by adding a <link> tag",
11+
"var update = require(" + JSON.stringify("!" + path.join(__dirname, "addStyleUrl.js")) + ")(",
12+
"\trequire(" + JSON.stringify("!!" + remainingRequest) + ")",
13+
");",
14+
"// Hot Module Replacement",
15+
"if(module.hot) {",
16+
"\tmodule.hot.accept(" + JSON.stringify("!!" + remainingRequest) + ", function() {",
17+
"\t\tupdate(require(" + JSON.stringify("!!" + remainingRequest) + "));",
18+
"\t});",
19+
"\tmodule.hot.dispose(function() { update(); });",
20+
"}"
21+
].join("\n");
1622
};

0 commit comments

Comments
 (0)