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 = {
330330 /**
331331 * Call this if you plan on loading less files.
332332 *
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
333344 * @return {exports }
334345 */
335- enableLessLoader ( ) {
336- webpackConfig . enableLessLoader ( ) ;
346+ enableLessLoader ( lessLoaderOptionsCallback = ( ) => { } ) {
347+ webpackConfig . enableLessLoader ( lessLoaderOptionsCallback ) ;
337348
338349 return this ;
339350 } ,
Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ class WebpackConfig {
4747 resolve_url_loader : true
4848 } ;
4949 this . useLessLoader = false ;
50+ this . lessLoaderOptionsCallback = function ( ) { } ;
5051 this . cleanupOutput = false ;
5152 this . sharedCommonsEntryName = null ;
5253 this . providedVariables = { } ;
@@ -231,8 +232,14 @@ class WebpackConfig {
231232 }
232233 }
233234
234- enableLessLoader ( ) {
235+ enableLessLoader ( lessLoaderOptionsCallback = ( ) => { } ) {
235236 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 ;
236243 }
237244
238245 enableReactPreset ( ) {
Original file line number Diff line number Diff line change @@ -21,13 +21,22 @@ module.exports = {
2121 getLoaders ( webpackConfig , ignorePostCssLoader = false ) {
2222 loaderFeatures . ensureLoaderPackagesExist ( 'less' ) ;
2323
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+
2435 return [
2536 ...cssLoader . getLoaders ( webpackConfig , ignorePostCssLoader ) ,
2637 {
2738 loader : 'less-loader' ,
28- options : {
29- sourceMap : webpackConfig . useSourceMaps
30- }
39+ options : config
3140 } ,
3241 ] ;
3342 }
Original file line number Diff line number Diff line change @@ -339,6 +339,31 @@ describe('WebpackConfig object', () => {
339339 } ) ;
340340 } ) ;
341341
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+
342367 describe ( 'enableTypeScriptLoader' , ( ) => {
343368 it ( 'Calling method sets it' , ( ) => {
344369 const config = createConfig ( ) ;
Original file line number Diff line number Diff line change @@ -39,4 +39,26 @@ describe('loaders/less', () => {
3939
4040 cssLoader . getLoaders . restore ( ) ;
4141 } ) ;
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+ } ) ;
4264} ) ;
You can’t perform that action at this time.
0 commit comments