File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -1596,6 +1596,29 @@ class Encore {
1596
1596
return webpackConfig . isDevServer ( ) ;
1597
1597
}
1598
1598
1599
+ /**
1600
+ * Use to conditionally configure or enable features only in when the first parameter results to "true".
1601
+ *
1602
+ * ```
1603
+ * Encore
1604
+ * // passing a callback
1605
+ * .when((Encore) => Encore.isProduction(), (Encore) => Encore.enableVersioning())
1606
+ * // passing a boolean
1607
+ * .when(process.argv.includes('--analyze'), (Encore) => Encore.addPlugin(new BundleAnalyzerPlugin()))
1608
+ * ```
1609
+ *
1610
+ * @param {(function(Encore): boolean) | boolean } condition
1611
+ * @param {function(Encore): void } callback
1612
+ * @return {Encore }
1613
+ */
1614
+ when ( condition , callback ) {
1615
+ if ( typeof condition === 'function' && condition ( this ) || typeof condition === 'boolean' && condition ) {
1616
+ callback ( this ) ;
1617
+ }
1618
+
1619
+ return this ;
1620
+ }
1621
+
1599
1622
/**
1600
1623
* Use this at the bottom of your webpack.config.js file:
1601
1624
*
Original file line number Diff line number Diff line change 10
10
'use strict' ;
11
11
12
12
const expect = require ( 'chai' ) . expect ;
13
+ const sinon = require ( 'sinon' ) ;
13
14
const api = require ( '../index' ) ;
14
15
const path = require ( 'path' ) ;
15
16
@@ -454,6 +455,21 @@ describe('Public API', () => {
454
455
455
456
} ) ;
456
457
458
+ describe ( 'when' , ( ) => {
459
+ it ( 'should call or not callbacks depending of the conditions' , ( ) => {
460
+ api . configureRuntimeEnvironment ( 'dev' , { } , false ) ;
461
+
462
+ const spy = sinon . spy ( ) ;
463
+ api
464
+ . when ( ( Encore ) => Encore . isDev ( ) , ( Encore ) => spy ( 'is dev' ) )
465
+ . when ( ( Encore ) => Encore . isProduction ( ) , ( Encore ) => spy ( 'is production' ) )
466
+ . when ( true , ( Encore ) => spy ( 'true' ) ) ;
467
+ expect ( spy . calledWith ( 'is dev' ) , 'callback for "is dev" should be called' ) . to . be . true ;
468
+ expect ( spy . calledWith ( 'is production' ) , 'callback for "is production" should NOT be called' ) . to . be . false ;
469
+ expect ( spy . calledWith ( 'true' ) , 'callback for "true" should be called' ) . to . be . true ;
470
+ } ) ;
471
+ } ) ;
472
+
457
473
describe ( 'isRuntimeEnvironmentConfigured' , ( ) => {
458
474
459
475
it ( 'should return true if the runtime environment has been configured' , ( ) => {
You can’t perform that action at this time.
0 commit comments