Skip to content

Commit 49ed6c8

Browse files
committed
feature #963 feat: add Encore.when() (Kocal)
This PR was squashed before being merged into the main branch. Discussion ---------- feat: add Encore.when() Replace #900 by adding a more generic and flexible API than `Encore.isDev()`/`Encore.isProd()`, see #900 (comment). `Encore.when()` takes two parameters: - `condition`: can be a callback (where the current instance of Encore is passed as first parameter) or a boolean. If this results to `true`, the parameter `callback` is called - `callback`: executed when parameter `condition` results to `true`, it takes the current instance of Encore as first parameter WDYT? Thanks! Commits ------- 8082c61 feat: add Encore.when()
2 parents 7a7d3a1 + 8082c61 commit 49ed6c8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,29 @@ class Encore {
15961596
return webpackConfig.isDevServer();
15971597
}
15981598

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+
15991622
/**
16001623
* Use this at the bottom of your webpack.config.js file:
16011624
*

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'use strict';
1111

1212
const expect = require('chai').expect;
13+
const sinon = require('sinon');
1314
const api = require('../index');
1415
const path = require('path');
1516

@@ -454,6 +455,21 @@ describe('Public API', () => {
454455

455456
});
456457

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+
457473
describe('isRuntimeEnvironmentConfigured', () => {
458474

459475
it('should return true if the runtime environment has been configured', () => {

0 commit comments

Comments
 (0)