File tree Expand file tree Collapse file tree 5 files changed +80
-6
lines changed Expand file tree Collapse file tree 5 files changed +80
-6
lines changed Original file line number Diff line number Diff line change @@ -330,10 +330,21 @@ module.exports = {
330
330
/**
331
331
* Call this if you plan on loading less files.
332
332
*
333
+ * Encore.enableLessLoader();
334
+ *
335
+ * Or pass options to the loader
336
+ *
337
+ * Encore.enableLessLoader(function(options) {
338
+ * // https://github.com/webpack-contrib/less-loader#examples
339
+ * // http://lesscss.org/usage/#command-line-usage-options
340
+ * // options.relativeUrls = false;
341
+ * });
342
+ *
343
+ * @param {function } lessLoaderOptionsCallback
333
344
* @return {exports }
334
345
*/
335
- enableLessLoader ( ) {
336
- webpackConfig . enableLessLoader ( ) ;
346
+ enableLessLoader ( lessLoaderOptionsCallback = ( ) => { } ) {
347
+ webpackConfig . enableLessLoader ( lessLoaderOptionsCallback ) ;
337
348
338
349
return this ;
339
350
} ,
Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ class WebpackConfig {
47
47
resolve_url_loader : true
48
48
} ;
49
49
this . useLessLoader = false ;
50
+ this . lessLoaderOptionsCallback = function ( ) { } ;
50
51
this . cleanupOutput = false ;
51
52
this . sharedCommonsEntryName = null ;
52
53
this . providedVariables = { } ;
@@ -231,8 +232,14 @@ class WebpackConfig {
231
232
}
232
233
}
233
234
234
- enableLessLoader ( ) {
235
+ enableLessLoader ( lessLoaderOptionsCallback = ( ) => { } ) {
235
236
this . useLessLoader = true ;
237
+
238
+ if ( typeof lessLoaderOptionsCallback !== 'function' ) {
239
+ throw new Error ( 'Argument 1 to enableLessLoader() must be a callback function.' ) ;
240
+ }
241
+
242
+ this . lessLoaderOptionsCallback = lessLoaderOptionsCallback ;
236
243
}
237
244
238
245
enableReactPreset ( ) {
Original file line number Diff line number Diff line change @@ -21,13 +21,22 @@ module.exports = {
21
21
getLoaders ( webpackConfig , ignorePostCssLoader = false ) {
22
22
loaderFeatures . ensureLoaderPackagesExist ( 'less' ) ;
23
23
24
+ const config = {
25
+ sourceMap : webpackConfig . useSourceMaps
26
+ } ;
27
+
28
+ // allow options to be configured
29
+ webpackConfig . lessLoaderOptionsCallback . apply (
30
+ // use config as the this variable
31
+ config ,
32
+ [ config ]
33
+ ) ;
34
+
24
35
return [
25
36
...cssLoader . getLoaders ( webpackConfig , ignorePostCssLoader ) ,
26
37
{
27
38
loader : 'less-loader' ,
28
- options : {
29
- sourceMap : webpackConfig . useSourceMaps
30
- }
39
+ options : config
31
40
} ,
32
41
] ;
33
42
}
Original file line number Diff line number Diff line change @@ -339,6 +339,31 @@ describe('WebpackConfig object', () => {
339
339
} ) ;
340
340
} ) ;
341
341
342
+ describe ( 'enableLessLoader' , ( ) => {
343
+ it ( 'Calling method sets it' , ( ) => {
344
+ const config = createConfig ( ) ;
345
+ config . enableLessLoader ( ) ;
346
+
347
+ expect ( config . useLessLoader ) . to . be . true ;
348
+ } ) ;
349
+
350
+ it ( 'Calling with callback' , ( ) => {
351
+ const config = createConfig ( ) ;
352
+ const callback = ( lessOptions ) => { } ;
353
+ config . enableLessLoader ( callback ) ;
354
+
355
+ expect ( config . lessLoaderOptionsCallback ) . to . equal ( callback ) ;
356
+ } ) ;
357
+
358
+ it ( 'Calling with non-callback throws an error' , ( ) => {
359
+ const config = createConfig ( ) ;
360
+
361
+ expect ( ( ) => {
362
+ config . enableLessLoader ( 'FOO' ) ;
363
+ } ) . to . throw ( 'must be a callback function' ) ;
364
+ } ) ;
365
+ } ) ;
366
+
342
367
describe ( 'enableTypeScriptLoader' , ( ) => {
343
368
it ( 'Calling method sets it' , ( ) => {
344
369
const config = createConfig ( ) ;
Original file line number Diff line number Diff line change @@ -39,4 +39,26 @@ describe('loaders/less', () => {
39
39
40
40
cssLoader . getLoaders . restore ( ) ;
41
41
} ) ;
42
+
43
+ it ( 'getLoaders() with options callback' , ( ) => {
44
+ const config = createConfig ( ) ;
45
+ config . enableSourceMaps ( true ) ;
46
+
47
+ // make the cssLoader return nothing
48
+ sinon . stub ( cssLoader , 'getLoaders' )
49
+ . callsFake ( ( ) => [ ] ) ;
50
+
51
+ config . enableLessLoader ( function ( lessOptions ) {
52
+ lessOptions . custom_option = 'foo' ;
53
+ lessOptions . other_option = true ;
54
+ } ) ;
55
+
56
+ const actualLoaders = lessLoader . getLoaders ( config ) ;
57
+ expect ( actualLoaders [ 0 ] . options ) . to . deep . equals ( {
58
+ sourceMap : true ,
59
+ custom_option : 'foo' ,
60
+ other_option : true
61
+ } ) ;
62
+ cssLoader . getLoaders . restore ( ) ;
63
+ } ) ;
42
64
} ) ;
You can’t perform that action at this time.
0 commit comments