1
1
/*
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
4
4
*/
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" ) ;
10
10
11
11
module . 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 ) || { } ;
22
13
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 = "" ;
29
32
}
30
33
} else {
31
34
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
32
35
map = null ;
33
36
}
34
37
35
- var parserOptions = {
36
- url : query . url !== false ,
37
- import : query . import !== false ,
38
- resolve : loaderContext . resolve
39
- } ;
40
-
38
+ // We need a prefix to avoid path rewriting of PostCSS
41
39
const from =
42
40
"/css-loader!" +
43
41
loaderUtils
44
- . getRemainingRequest ( loaderContext )
42
+ . getRemainingRequest ( this )
45
43
. split ( "!" )
46
44
. pop ( ) ;
47
45
const to = loaderUtils
48
- . getCurrentRequest ( loaderContext )
46
+ . getCurrentRequest ( this )
49
47
. split ( "!" )
50
48
. pop ( ) ;
51
49
52
50
postcss ( [ plugin ( parserOptions ) ] )
53
51
. process ( content , {
54
- // we need a prefix to avoid path rewriting of PostCSS
55
52
from,
56
53
to,
57
54
map : sourceMap
@@ -63,14 +60,14 @@ module.exports = function(content, map) {
63
60
}
64
61
: null
65
62
} )
66
- . then ( function ( result ) {
63
+ . then ( result => {
67
64
var cssAsString = JSON . stringify ( result . css ) ;
68
- var importItems = parserOptions . importItems ;
69
65
70
- if ( query . import !== false && importItems . length > 0 ) {
66
+ if ( options . import !== false ) {
71
67
var alreadyImported = { } ;
72
- var importJs = importItems
73
- . filter ( function ( imp ) {
68
+ var importJs = result . messages
69
+ . filter ( message => message . type === "at-rule-import" )
70
+ . filter ( imp => {
74
71
if ( ! imp . mediaQuery ) {
75
72
if ( alreadyImported [ imp . url ] ) {
76
73
return false ;
@@ -81,7 +78,7 @@ module.exports = function(content, map) {
81
78
82
79
return true ;
83
80
} )
84
- . map ( function ( imp ) {
81
+ . map ( imp => {
85
82
if ( ! loaderUtils . isUrlRequest ( imp . url ) ) {
86
83
return (
87
84
"exports.push([module.id, " +
@@ -93,7 +90,7 @@ module.exports = function(content, map) {
93
90
}
94
91
95
92
// for importing CSS
96
- var importUrlPrefix = getImportPrefix ( loaderContext , query ) ;
93
+ var importUrlPrefix = getImportPrefix ( this , options ) ;
97
94
var importUrl = importUrlPrefix + imp . url ;
98
95
99
96
return (
@@ -103,61 +100,36 @@ module.exports = function(content, map) {
103
100
JSON . stringify ( imp . mediaQuery ) +
104
101
");"
105
102
) ;
106
- } , loaderContext )
103
+ } )
107
104
. join ( "\n" ) ;
108
105
}
109
106
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 = "" ;
113
109
114
- if ( query . url !== false && urlItems . length > 0 ) {
110
+ if ( options . url !== false ) {
115
111
urlEscapeHelper =
116
112
"var runtimeEscape = require(" +
117
113
loaderUtils . stringifyRequest (
118
- loaderContext ,
114
+ this ,
119
115
require . resolve ( "./runtimeEscape.js" )
120
116
) +
121
117
");\n" ;
122
118
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 =
155
125
'" + 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
+ } ) ;
161
133
}
162
134
163
135
// Todo need save backward compatibility with old `style-loader`
@@ -170,16 +142,15 @@ module.exports = function(content, map) {
170
142
var moduleJs ;
171
143
172
144
if ( sourceMap && result . map ) {
173
- // add a SourceMap
174
145
map = result . map . toJSON ( ) ;
175
146
176
147
if ( map . sources ) {
177
- map . sources = map . sources . map ( function ( source ) {
178
- return source
148
+ map . sources = map . sources . map ( source =>
149
+ source
179
150
. split ( "!" )
180
151
. pop ( )
181
- . replace ( / \\ / g, "/" ) ;
182
- } , loaderContext ) ;
152
+ . replace ( / \\ / g, "/" )
153
+ ) ;
183
154
map . sourceRoot = "" ;
184
155
}
185
156
@@ -196,14 +167,11 @@ module.exports = function(content, map) {
196
167
}
197
168
198
169
// embed runtime
199
- callback (
170
+ cb (
200
171
null ,
201
172
urlEscapeHelper +
202
173
"exports = module.exports = require(" +
203
- loaderUtils . stringifyRequest (
204
- loaderContext ,
205
- require . resolve ( "./runtime.js" )
206
- ) +
174
+ loaderUtils . stringifyRequest ( this , require . resolve ( "./runtime.js" ) ) +
207
175
")(" +
208
176
sourceMap +
209
177
");\n" +
@@ -217,18 +185,20 @@ module.exports = function(content, map) {
217
185
exportJs
218
186
) ;
219
187
} )
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"
223
193
? new CssLoaderError (
224
194
"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 }
228
198
: null ,
229
- error . input . source
199
+ err . input . source
230
200
)
231
- : error
201
+ : err
232
202
) ;
233
203
} ) ;
234
204
} ;
0 commit comments