11/* 
2- 	 MIT License http://www.opensource.org/licenses/mit-license.php
3- 	 Author Tobias Koppers @sokra
2+    MIT License http://www.opensource.org/licenses/mit-license.php
3+    Author Tobias Koppers @sokra
44*/ 
5- var  loaderUtils  =  require ( "loader-utils" ) ; 
6- var  postcss  =  require ( "postcss" ) ; 
7- var  plugin  =  require ( "./plugin" ) ; 
8- var  getImportPrefix  =  require ( "./getImportPrefix" ) ; 
9- var  CssLoaderError  =  require ( "./CssLoaderError" ) ; 
5+ const  loaderUtils  =  require ( "loader-utils" ) ; 
6+ const  postcss  =  require ( "postcss" ) ; 
7+ const  plugin  =  require ( "./plugin" ) ; 
8+ const  getImportPrefix  =  require ( "./getImportPrefix" ) ; 
9+ const  CssLoaderError  =  require ( "./CssLoaderError" ) ; 
1010
1111module . exports  =  function ( content ,  map )  { 
12-   var  callback  =  this . async ( ) ; 
13-   var  query  =  loaderUtils . getOptions ( this )  ||  { } ; 
14-   var  sourceMap  =  query . sourceMap  ||  false ; 
15-   var  loaderContext  =  this ; 
16- 
17-   if  ( sourceMap )  { 
18-     if  ( map )  { 
19-       if  ( typeof  map  ===  "string" )  { 
20-         map  =  JSON . stringify ( map ) ; 
21-       } 
12+   const  options  =  loaderUtils . getOptions ( this )  ||  { } ; 
2213
23-       if  ( map . sources )  { 
24-         map . sources  =  map . sources . map ( function ( source )  { 
25-           return  source . replace ( / \\ / g,  "/" ) ; 
26-         } ) ; 
27-         map . sourceRoot  =  "" ; 
28-       } 
14+   // Todo validate options 
15+ 
16+   const  cb  =  this . async ( ) ; 
17+   const  sourceMap  =  options . sourceMap ; 
18+ 
19+   var  parserOptions  =  { 
20+     url : options . url  !==  false , 
21+     import : options . import  !==  false 
22+   } ; 
23+ 
24+   if  ( sourceMap  &&  map )  { 
25+     if  ( typeof  map  ===  "string" )  { 
26+       map  =  JSON . parse ( map ) ; 
27+     } 
28+ 
29+     if  ( map . sources )  { 
30+       map . sources  =  map . sources . map ( source  =>  source . replace ( / \\ / g,  "/" ) ) ; 
31+       map . sourceRoot  =  "" ; 
2932    } 
3033  }  else  { 
3134    // Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it 
3235    map  =  null ; 
3336  } 
3437
35-   var  parserOptions  =  { 
36-     url : query . url  !==  false , 
37-     import : query . import  !==  false , 
38-     resolve : loaderContext . resolve 
39-   } ; 
40- 
4138  const  from  = 
4239    "/css-loader!"  + 
4340    loaderUtils 
44-       . getRemainingRequest ( loaderContext ) 
41+       . getRemainingRequest ( this ) 
4542      . split ( "!" ) 
4643      . pop ( ) ; 
4744  const  to  =  loaderUtils 
48-     . getCurrentRequest ( loaderContext ) 
45+     . getCurrentRequest ( this ) 
4946    . split ( "!" ) 
5047    . pop ( ) ; 
5148
@@ -63,14 +60,14 @@ module.exports = function(content, map) {
6360          } 
6461        : null 
6562    } ) 
66-     . then ( function ( result )  { 
63+     . then ( result   =>  { 
6764      var  cssAsString  =  JSON . stringify ( result . css ) ; 
6865      var  importItems  =  parserOptions . importItems ; 
6966
70-       if  ( query . import  !==  false  &&  importItems . length  >  0 )  { 
67+       if  ( options . import  !==  false  &&  importItems . length  >  0 )  { 
7168        var  alreadyImported  =  { } ; 
7269        var  importJs  =  importItems 
73-           . filter ( function ( imp )  { 
70+           . filter ( imp   =>  { 
7471            if  ( ! imp . mediaQuery )  { 
7572              if  ( alreadyImported [ imp . url ] )  { 
7673                return  false ; 
@@ -81,7 +78,7 @@ module.exports = function(content, map) {
8178
8279            return  true ; 
8380          } ) 
84-           . map ( function ( imp )  { 
81+           . map ( imp   =>  { 
8582            if  ( ! loaderUtils . isUrlRequest ( imp . url ) )  { 
8683              return  ( 
8784                "exports.push([module.id, "  + 
@@ -93,7 +90,7 @@ module.exports = function(content, map) {
9390            } 
9491
9592            // for importing CSS 
96-             var  importUrlPrefix  =  getImportPrefix ( loaderContext ,   query ) ; 
93+             var  importUrlPrefix  =  getImportPrefix ( this ,   options ) ; 
9794            var  importUrl  =  importUrlPrefix  +  imp . url ; 
9895
9996            return  ( 
@@ -103,61 +100,36 @@ module.exports = function(content, map) {
103100              JSON . stringify ( imp . mediaQuery )  + 
104101              ");" 
105102            ) ; 
106-           } ,   loaderContext ) 
103+           } ) 
107104          . join ( "\n" ) ; 
108105      } 
109106
110-       // helper for ensuring valid CSS strings from requires 
111-       var  urlEscapeHelper  =  "" ; 
112-       var  urlItems  =  parserOptions . urlItems ; 
107+       // Helper for ensuring valid CSS strings from requires 
108+       let  urlEscapeHelper  =  "" ; 
113109
114-       if  ( query . url  !==  false   &&   urlItems . length   >   0 )  { 
110+       if  ( options . url  !==  false )  { 
115111        urlEscapeHelper  = 
116112          "var runtimeEscape = require("  + 
117113          loaderUtils . stringifyRequest ( 
118-             loaderContext , 
114+             this , 
119115            require . resolve ( "./runtimeEscape.js" ) 
120116          )  + 
121117          ");\n" ; 
122118
123-         cssAsString  =  cssAsString . replace ( 
124-           / _ _ _ C S S _ L O A D E R _ U R L _ _ _ ( [ 0 - 9 ] + ) _ _ _ / g, 
125-           function ( item )  { 
126-             var  match  =  / _ _ _ C S S _ L O A D E R _ U R L _ _ _ ( [ 0 - 9 ] + ) _ _ _ / . exec ( item ) ; 
127-             var  idx  =  + match [ 1 ] ; 
128-             var  urlItem  =  urlItems [ idx ] ; 
129-             var  url  =  urlItem . url ; 
130- 
131-             idx  =  url . indexOf ( "?#" ) ; 
132- 
133-             if  ( idx  <  0 )  { 
134-               idx  =  url . indexOf ( "#" ) ; 
135-             } 
136- 
137-             var  urlRequest ; 
138- 
139-             if  ( idx  >  0 )  { 
140-               // idx === 0 is catched by isUrlRequest 
141-               // in cases like url('webfont.eot?#iefix') 
142-               urlRequest  =  url . substr ( 0 ,  idx ) ; 
143- 
144-               return  ( 
145-                 '" + runtimeEscape(require('  + 
146-                 loaderUtils . stringifyRequest ( loaderContext ,  urlRequest )  + 
147-                 ')) + "'  + 
148-                 url . substr ( idx ) 
149-               ) ; 
150-             } 
151- 
152-             urlRequest  =  url ; 
153- 
154-             return  ( 
119+         result . messages 
120+           . filter ( message  =>  message . type  ===  "css-loader-import-url" ) 
121+           . forEach ( message  =>  { 
122+             const  {  placeholder,  url }  =  message ; 
123+             const  splittedURL  =  url . split ( / ( \? ) ? # / ) ; 
124+             const  importURLString  = 
155125              '" + runtimeEscape(require('  + 
156-               loaderUtils . stringifyRequest ( loaderContext ,  urlRequest )  + 
157-               ')) + "' 
158-             ) ; 
159-           } 
160-         ) ; 
126+               loaderUtils . stringifyRequest ( this ,  splittedURL [ 0 ] )  + 
127+               ')) + "'  + 
128+               ( splittedURL [ 1 ]  ? splittedURL [ 1 ]  : "" )  + 
129+               ( splittedURL [ 2 ]  ? `#${ splittedURL [ 2 ] }   : "" ) ; 
130+ 
131+             cssAsString  =  cssAsString . replace ( placeholder ,  importURLString ) ; 
132+           } ) ; 
161133      } 
162134
163135      // Todo need save backward compatibility with old `style-loader` 
@@ -170,16 +142,15 @@ module.exports = function(content, map) {
170142      var  moduleJs ; 
171143
172144      if  ( sourceMap  &&  result . map )  { 
173-         // add a SourceMap 
174145        map  =  result . map . toJSON ( ) ; 
175146
176147        if  ( map . sources )  { 
177-           map . sources  =  map . sources . map ( function ( source )   { 
178-             return   source 
148+           map . sources  =  map . sources . map ( source   => 
149+             source 
179150              . split ( "!" ) 
180151              . pop ( ) 
181-               . replace ( / \\ / g,  "/" ) ; 
182-           } ,   loaderContext ) ; 
152+               . replace ( / \\ / g,  "/" ) 
153+           ) ; 
183154          map . sourceRoot  =  "" ; 
184155        } 
185156
@@ -196,14 +167,11 @@ module.exports = function(content, map) {
196167      } 
197168
198169      // embed runtime 
199-       callback ( 
170+       cb ( 
200171        null , 
201172        urlEscapeHelper  + 
202173          "exports = module.exports = require("  + 
203-           loaderUtils . stringifyRequest ( 
204-             loaderContext , 
205-             require . resolve ( "./runtime.js" ) 
206-           )  + 
174+           loaderUtils . stringifyRequest ( this ,  require . resolve ( "./runtime.js" ) )  + 
207175          ")("  + 
208176          sourceMap  + 
209177          ");\n"  + 
@@ -217,18 +185,20 @@ module.exports = function(content, map) {
217185          exportJs 
218186      ) ; 
219187    } ) 
220-     . catch ( function ( error )  { 
221-       callback ( 
222-         error . name  ===  "CssSyntaxError" 
188+     . catch ( err  =>  { 
189+       // Todo if (err.file) this.addDependency(err.file) 
190+ 
191+       cb ( 
192+         err . name  ===  "CssSyntaxError" 
223193          ? new  CssLoaderError ( 
224194              "Syntax Error" , 
225-               error . reason , 
226-               error . line  !=  null  &&  error . column  !=  null 
227-                 ? {  line : error . line ,  column : error . column  } 
195+               err . reason , 
196+               err . line  !=  null  &&  err . column  !=  null 
197+                 ? {  line : err . line ,  column : err . column  } 
228198                : null , 
229-               error . input . source 
199+               err . input . source 
230200            ) 
231-           : error 
201+           : err 
232202      ) ; 
233203    } ) ; 
234204} ; 
0 commit comments