2
2
MIT License http://www.opensource.org/licenses/mit-license.php
3
3
Author Tobias Koppers @sokra
4
4
*/
5
-
6
5
var path = require ( "path" ) ;
7
6
8
7
var loaderUtils = require ( "loader-utils" ) ;
@@ -16,63 +15,82 @@ module.exports.pitch = function (request) {
16
15
var options = loaderUtils . getOptions ( this ) || { } ;
17
16
18
17
validateOptions ( require ( './options.json' ) , options , 'Style Loader' )
18
+
19
+ options . hmr = typeof options . hmr === 'undefined' ? true : options . hmr ;
19
20
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
+ }
33
35
34
- var hmrCode = [
35
- " // Hot Module Replacement" ,
36
+ var hmr = [
37
+ // Hot Module Replacement,
36
38
"if(module.hot) {" ,
37
- " // When the styles change, update the <style> tags" ,
39
+ // When the styles change, update the <style> tags
38
40
" module.hot.accept(" + loaderUtils . stringifyRequest ( this , "!!" + request ) + ", function() {" ,
39
41
" var newContent = require(" + loaderUtils . stringifyRequest ( this , "!!" + request ) + ");" ,
42
+ "" ,
40
43
" if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];" ,
44
+ "" ,
41
45
" var locals = (function(a, b) {" ,
42
46
" var key, idx = 0;" ,
47
+ "" ,
43
48
" for(key in a) {" ,
44
49
" if(!b || a[key] !== b[key]) return false;" ,
45
50
" idx++;" ,
46
51
" }" ,
52
+ "" ,
47
53
" for(key in b) idx--;" ,
54
+ "" ,
48
55
" return idx === 0;" ,
49
56
" }(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
51
59
" if(!locals) throw new Error('Aborting CSS HMR due to changed css-modules locals.');" ,
60
+ "" ,
52
61
" update(newContent);" ,
53
62
" });" ,
54
- " // When the module is disposed, remove the <style> tags" ,
63
+ "" ,
64
+ // When the module is disposed, remove the <style> tags
55
65
" module.hot.dispose(function() { update(); });" ,
56
66
"}"
57
67
] . join ( "\n" ) ;
58
68
59
69
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
61
72
"" ,
62
- " // load the styles" ,
73
+ // Load styles
63
74
"var content = require(" + loaderUtils . stringifyRequest ( this , "!!" + request ) + ");" ,
75
+ "" ,
64
76
"if(typeof content === 'string') content = [[module.id, content, '']];" ,
65
- "// Prepare cssTransformation" ,
77
+ "" ,
78
+ // Transform styles",
66
79
"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
74
90
"var update = require(" + loaderUtils . stringifyRequest ( this , "!" + path . join ( __dirname , "lib" , "addStyles.js" ) ) + ")(content, options);" ,
91
+ "" ,
75
92
"if(content.locals) module.exports = content.locals;" ,
76
- options . hmr ? hmrCode : ""
93
+ "" ,
94
+ options . hmr ? hmr : ""
77
95
] . join ( "\n" ) ;
78
96
} ;
0 commit comments