Skip to content

Commit 28f603f

Browse files
committedJan 26, 2018
refactor: remove comments from bundle source code
1 parent dda8b89 commit 28f603f

File tree

3 files changed

+74
-46
lines changed

3 files changed

+74
-46
lines changed
 

‎index.js

+48-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
65
var path = require("path");
76

87
var loaderUtils = require("loader-utils");
@@ -16,63 +15,82 @@ module.exports.pitch = function (request) {
1615
var options = loaderUtils.getOptions(this) || {};
1716

1817
validateOptions(require('./options.json'), options, 'Style Loader')
18+
19+
options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;
1920

20-
options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;
21-
22-
// need to use this variable, because function should be inlined
23-
// if just store it in options, then after JSON.stringify
24-
// function will be quoted and then in runtime will be just string
25-
var insertInto;
26-
if (typeof options.insertInto === "function") {
27-
insertInto = options.insertInto.toString();
28-
}
29-
// we need to check if it string, or variable will be "undefined" and loader crash then
30-
if (typeof options.insertInto === "string") {
31-
insertInto = '"' + options.insertInto + '"';
32-
}
21+
// The variable is needed, because the function should be inlined.
22+
// If is just stored it in options, JSON.stringify will quote
23+
// the function and it would be just a string at runtime
24+
var insertInto;
25+
26+
if (typeof options.insertInto === "function") {
27+
insertInto = options.insertInto.toString();
28+
}
29+
30+
// We need to check if it a string, or variable will be "undefined"
31+
// and the loader crashes
32+
if (typeof options.insertInto === "string") {
33+
insertInto = '"' + options.insertInto + '"';
34+
}
3335

34-
var hmrCode = [
35-
"// Hot Module Replacement",
36+
var hmr = [
37+
// Hot Module Replacement,
3638
"if(module.hot) {",
37-
" // When the styles change, update the <style> tags",
39+
// When the styles change, update the <style> tags
3840
" module.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {",
3941
" var newContent = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
42+
"",
4043
" if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];",
44+
"",
4145
" var locals = (function(a, b) {",
4246
" var key, idx = 0;",
47+
"",
4348
" for(key in a) {",
4449
" if(!b || a[key] !== b[key]) return false;",
4550
" idx++;",
4651
" }",
52+
"",
4753
" for(key in b) idx--;",
54+
"",
4855
" return idx === 0;",
4956
" }(content.locals, newContent.locals));",
50-
" // This error is caught and not shown and causes a full reload.",
57+
"",
58+
// This error is caught and not shown and causes a full reload
5159
" if(!locals) throw new Error('Aborting CSS HMR due to changed css-modules locals.');",
60+
"",
5261
" update(newContent);",
5362
" });",
54-
" // When the module is disposed, remove the <style> tags",
63+
"",
64+
// When the module is disposed, remove the <style> tags
5565
" module.hot.dispose(function() { update(); });",
5666
"}"
5767
].join("\n");
5868

5969
return [
60-
"// style-loader: Adds some css to the DOM by adding a <style> tag",
70+
// Style Loader
71+
// Adds CSS to the DOM by adding a <style> tag
6172
"",
62-
"// load the styles",
73+
// Load styles
6374
"var content = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
75+
"",
6476
"if(typeof content === 'string') content = [[module.id, content, '']];",
65-
"// Prepare cssTransformation",
77+
"",
78+
// Transform styles",
6679
"var transform;",
67-
"var insertInto;",
68-
options.transform ? "transform = require(" + loaderUtils.stringifyRequest(this, "!" + path.resolve(options.transform)) + ");" : "",
69-
"insertInto = " + insertInto + ";" ,
70-
"var options = " + JSON.stringify(options),
71-
"options.transform = transform",
72-
"options.insertInto = insertInto;",
73-
"// add the styles to the DOM",
80+
"var insertInto;",
81+
"",
82+
options.transform ? "transform = require(" + loaderUtils.stringifyRequest(this, "!" + path.resolve(options.transform)) + ");" : "",
83+
"",
84+
"var options = " + JSON.stringify(options),
85+
"",
86+
"options.transform = transform",
87+
"options.insertInto = " + insertInto + ";",
88+
"",
89+
// Add styles to the DOM
7490
"var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyles.js")) + ")(content, options);",
91+
"",
7592
"if(content.locals) module.exports = content.locals;",
76-
options.hmr ? hmrCode : ""
93+
"",
94+
options.hmr ? hmr : ""
7795
].join("\n");
7896
};

‎url.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@ module.exports.pitch = function (request) {
1818

1919
options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;
2020

21-
var hmrCode = [
22-
"// Hot Module Replacement",
21+
var hmr = [
22+
// Hot Module Replacement
2323
"if(module.hot) {",
24-
"\tmodule.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {",
25-
"\t\tupdate(require(" + loaderUtils.stringifyRequest(this, "!!" + request) + "));",
26-
"\t});",
27-
"\tmodule.hot.dispose(function() { update(); });",
24+
" module.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {",
25+
" update(require(" + loaderUtils.stringifyRequest(this, "!!" + request) + "));",
26+
" });",
27+
"",
28+
" module.hot.dispose(function() { update(); });",
2829
"}"
2930
].join("\n");
3031

3132
return [
32-
"// style-loader: Adds some reference to a css file to the DOM by adding a <link> tag",
33+
// Adds some reference to a CSS file to the DOM by adding a <link> tag
3334
"var update = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyleUrl.js")) + ")(",
34-
"\trequire(" + loaderUtils.stringifyRequest(this, "!!" + request) + ")",
35+
" require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ")",
3536
", " + JSON.stringify(options) + ");",
36-
options.hmr ? hmrCode : ""
37+
options.hmr ? hmr : ""
3738
].join("\n");
3839
};

‎useable.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,25 @@ module.exports.pitch = function (request) {
1818

1919
options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;
2020

21-
var hmrCode = [
22-
"// Hot Module Replacement",
21+
var hmr = [
22+
// Hot Module Replacement
2323
"if(module.hot) {",
2424
" var lastRefs = module.hot.data && module.hot.data.refs || 0;",
25+
"",
2526
" if(lastRefs) {",
2627
" exports.ref();",
2728
" if(!content.locals) {",
2829
" refs = lastRefs;",
2930
" }",
3031
" }",
32+
"",
3133
" if(!content.locals) {",
3234
" module.hot.accept();",
3335
" }",
36+
"",
3437
" module.hot.dispose(function(data) {",
3538
" data.refs = content.locals ? 0 : refs;",
39+
"",
3640
" if(dispose) {",
3741
" dispose();",
3842
" }",
@@ -44,20 +48,25 @@ module.exports.pitch = function (request) {
4448
"var refs = 0;",
4549
"var dispose;",
4650
"var content = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");",
51+
"",
4752
"if(typeof content === 'string') content = [[module.id, content, '']];",
53+
// Export CSS Modules
4854
"if(content.locals) exports.locals = content.locals;",
55+
"",
4956
"exports.use = exports.ref = function() {",
5057
" if(!(refs++)) {",
5158
" dispose = require(" + loaderUtils.stringifyRequest(this, "!" + path.join(__dirname, "lib", "addStyles.js")) + ")(content, " + JSON.stringify(options) + ");",
5259
" }",
60+
"",
5361
" return exports;",
5462
"};",
63+
"",
5564
"exports.unuse = exports.unref = function() {",
56-
" if(refs > 0 && !(--refs)) {",
57-
" dispose();",
58-
" dispose = null;",
59-
" }",
65+
" if(refs > 0 && !(--refs)) {",
66+
" dispose();",
67+
" dispose = null;",
68+
" }",
6069
"};",
61-
options.hmr ? hmrCode : ""
70+
options.hmr ? hmr : ""
6271
].join("\n");
6372
};

0 commit comments

Comments
 (0)
Please sign in to comment.