File tree Expand file tree Collapse file tree 6 files changed +85
-18
lines changed Expand file tree Collapse file tree 6 files changed +85
-18
lines changed Original file line number Diff line number Diff line change @@ -232,10 +232,20 @@ module.exports = {
232
232
/**
233
233
* Call this if you plan on loading SASS files.
234
234
*
235
+ * Supported options:
236
+ * * {bool} resolve_url_loader (default=true)
237
+ * Whether or not to use the resolve-url-loader.
238
+ * Setting to false can increase performance in some
239
+ * cases, especially when using bootstrap_sass. But,
240
+ * when disabled, all url()'s are resolved relative
241
+ * to the original entry file... not whatever file
242
+ * the url() appears in.
243
+ *
244
+ * @param {object } options
235
245
* @return {exports }
236
246
*/
237
- enableSassLoader ( ) {
238
- webpackConfig . enableSassLoader ( ) ;
247
+ enableSassLoader ( options = { } ) {
248
+ webpackConfig . enableSassLoader ( options ) ;
239
249
240
250
return this ;
241
251
} ,
Original file line number Diff line number Diff line change @@ -40,6 +40,9 @@ class WebpackConfig {
40
40
this . useSourceMaps = false ;
41
41
this . usePostCssLoader = false ;
42
42
this . useSassLoader = false ;
43
+ this . sassOptions = {
44
+ resolve_url_loader : true
45
+ } ;
43
46
this . useLessLoader = false ;
44
47
this . cleanupOutput = false ;
45
48
this . sharedCommonsEntryName = null ;
@@ -189,8 +192,16 @@ class WebpackConfig {
189
192
this . usePostCssLoader = true ;
190
193
}
191
194
192
- enableSassLoader ( ) {
195
+ enableSassLoader ( options = { } ) {
193
196
this . useSassLoader = true ;
197
+
198
+ for ( const optionKey of Object . keys ( options ) ) {
199
+ if ( ! ( optionKey in this . sassOptions ) ) {
200
+ throw new Error ( `Invalid option "${ optionKey } " passed to enableSassLoader(). Valid keys are ${ Object . keys ( this . sassOptions ) . join ( ', ' ) } ` ) ;
201
+ }
202
+
203
+ this . sassOptions [ optionKey ] = options [ optionKey ] ;
204
+ }
194
205
}
195
206
196
207
enableLessLoader ( ) {
Original file line number Diff line number Diff line change @@ -191,24 +191,29 @@ class ConfigGenerator {
191
191
if ( this . webpackConfig . useSassLoader ) {
192
192
loaderFeatures . ensureLoaderPackagesExist ( 'sass' ) ;
193
193
194
+ const sassLoaders = [ ...cssLoaders ] ;
195
+ if ( true === this . webpackConfig . sassOptions . resolve_url_loader ) {
196
+ // responsible for resolving SASS url() paths
197
+ // without this, all url() paths must be relative to the
198
+ // entry file, not the file that contains the url()
199
+ sassLoaders . push ( {
200
+ loader : 'resolve-url-loader' + this . getSourceMapOption ( ) ,
201
+ } ) ;
202
+ }
203
+
204
+ sassLoaders . push ( {
205
+ loader : 'sass-loader' ,
206
+ options : {
207
+ // needed by the resolve-url-loader
208
+ sourceMap : ( true === this . webpackConfig . sassOptions . resolve_url_loader ) || this . webpackConfig . useSourceMaps
209
+ }
210
+ } ) ;
211
+
194
212
rules . push ( {
195
213
test : / \. s [ a c ] s s $ / ,
196
214
use : ExtractTextPlugin . extract ( {
197
215
fallback : 'style-loader' + this . getSourceMapOption ( ) ,
198
- use : [
199
- ...cssLoaders ,
200
- {
201
- // responsible for resolving SASS import paths
202
- loader : 'resolve-url-loader' + this . getSourceMapOption ( ) ,
203
- } ,
204
- {
205
- loader : 'sass-loader' ,
206
- options : {
207
- // always enabled, needed by resolve-url-loader
208
- sourceMap : true
209
- }
210
- } ,
211
- ]
216
+ use : sassLoaders
212
217
} )
213
218
} ) ;
214
219
}
Original file line number Diff line number Diff line change 59
59
"less" : " ^2.7.2" ,
60
60
"less-loader" : " ^4.0.2" ,
61
61
"mocha" : " ^3.2.0" ,
62
- "node-sass" : " ^4.5.0 " ,
62
+ "node-sass" : " ^4.5.3 " ,
63
63
"nsp" : " ^2.6.3" ,
64
64
"postcss-loader" : " ^1.3.3" ,
65
65
"sass-loader" : " ^6.0.3" ,
Original file line number Diff line number Diff line change @@ -251,4 +251,29 @@ describe('WebpackConfig object', () => {
251
251
} ) . to . throw ( 'configureBabel() cannot be called because your app has a .babelrc file' ) ;
252
252
} ) ;
253
253
} ) ;
254
+
255
+ describe ( 'enableSassLoader' , ( ) => {
256
+ it ( 'Call with no config' , ( ) => {
257
+ const config = createConfig ( ) ;
258
+ config . enableSassLoader ( ) ;
259
+
260
+ expect ( config . useSassLoader ) . to . be . true ;
261
+ } ) ;
262
+
263
+ it ( 'Pass valid config' , ( ) => {
264
+ const config = createConfig ( ) ;
265
+ config . enableSassLoader ( { resolve_url_loader : false } ) ;
266
+
267
+ expect ( config . useSassLoader ) . to . be . true ;
268
+ expect ( config . sassOptions . resolve_url_loader ) . to . be . false ;
269
+ } ) ;
270
+
271
+ it ( 'Pass invalid config' , ( ) => {
272
+ const config = createConfig ( ) ;
273
+
274
+ expect ( ( ) => {
275
+ config . enableSassLoader ( { fake_option : false } ) ;
276
+ } ) . to . throw ( 'Invalid option "fake_option" passed to enableSassLoader()' ) ;
277
+ } ) ;
278
+ } ) ;
254
279
} ) ;
Original file line number Diff line number Diff line change @@ -305,6 +305,22 @@ describe('The config-generator function', () => {
305
305
const actualConfig = configGenerator ( config ) ;
306
306
307
307
expect ( JSON . stringify ( actualConfig . module . rules ) ) . to . contain ( 'sass-loader' ) ;
308
+ expect ( JSON . stringify ( actualConfig . module . rules ) ) . to . contain ( 'resolve-url-loader' ) ;
309
+ // sourceMap option is needed for resolve-url-loader
310
+ expect ( JSON . stringify ( actualConfig . module . rules ) ) . to . contain ( '"sourceMap":true' ) ;
311
+ } ) ;
312
+
313
+ it ( 'enableSassLoader() without resolve_url_loader' , ( ) => {
314
+ const config = createConfig ( ) ;
315
+ config . outputPath = '/tmp/output/public-path' ;
316
+ config . publicPath = '/public-path' ;
317
+ config . addEntry ( 'main' , './main' ) ;
318
+ config . enableSassLoader ( { resolve_url_loader : false } ) ;
319
+
320
+ const actualConfig = configGenerator ( config ) ;
321
+
322
+ expect ( JSON . stringify ( actualConfig . module . rules ) ) . to . not . contain ( 'resolve-url-loader' ) ;
323
+ expect ( JSON . stringify ( actualConfig . module . rules ) ) . to . contain ( '"sourceMap":false' ) ;
308
324
} ) ;
309
325
} ) ;
310
326
You can’t perform that action at this time.
0 commit comments