Skip to content

Commit 8fc1cb1

Browse files
committed
minor #671 Add Encore.configureBabelPresetEnv() to the public API (Lyrkan)
This PR was merged into the master branch. Discussion ---------- Add Encore.configureBabelPresetEnv() to the public API This PR adds the missing method `configureBabelPresetEnv()` to the `index.js` file (fixes #666). Commits ------- d736625 Add Encore.configureBabelPresetEnv() to the public API
2 parents c28e360 + d736625 commit 8fc1cb1

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,31 @@ class Encore {
895895
return this;
896896
}
897897

898+
/**
899+
* Configure @babel/preset-env
900+
*
901+
* https://babeljs.io/docs/en/babel-preset-env
902+
*
903+
* ```
904+
* Encore.configureBabelPresetEnv(function(options) {
905+
* // change the @babel/preset-env config
906+
* // if you use an external Babel configuration
907+
* // this callback will NOT be used
908+
* // options.corejs = 3;
909+
* // options.useBuiltIns = 'usage';
910+
* // ...
911+
* });
912+
* ```
913+
*
914+
* @param {function} callback
915+
* @returns {Encore}
916+
*/
917+
configureBabelPresetEnv(callback) {
918+
webpackConfig.configureBabelPresetEnv(callback);
919+
920+
return this;
921+
}
922+
898923
/**
899924
* Configure the css-loader.
900925
*

test/config-generator.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,39 @@ describe('The config-generator function', () => {
933933
});
934934
});
935935

936+
describe('Test configureBabelPresetEnv()', () => {
937+
it('without configureBabelPresetEnv()', () => {
938+
const config = createConfig();
939+
config.outputPath = '/tmp/output/public-path';
940+
config.publicPath = '/public-path';
941+
config.addEntry('main', './main');
942+
943+
const actualConfig = configGenerator(config);
944+
945+
const jsRule = findRule(/\.jsx?$/, actualConfig.module.rules);
946+
const babelLoader = jsRule.use.find(loader => loader.loader === 'babel-loader');
947+
const babelEnvPreset = babelLoader.options.presets.find(([name]) => name === '@babel/preset-env');
948+
expect(babelEnvPreset[1].useBuiltIns).to.equal(false);
949+
});
950+
951+
it('with configureBabelPresetEnv()', () => {
952+
const config = createConfig();
953+
config.outputPath = '/tmp/output/public-path';
954+
config.publicPath = '/public-path';
955+
config.addEntry('main', './main');
956+
config.configureBabelPresetEnv(options => {
957+
options.useBuiltIns = 'usage';
958+
});
959+
960+
const actualConfig = configGenerator(config);
961+
962+
const jsRule = findRule(/\.jsx?$/, actualConfig.module.rules);
963+
const babelLoader = jsRule.use.find(loader => loader.loader === 'babel-loader');
964+
const babelEnvPreset = babelLoader.options.presets.find(([name]) => name === '@babel/preset-env');
965+
expect(babelEnvPreset[1].useBuiltIns).to.equal('usage');
966+
});
967+
});
968+
936969
describe('Test shouldSplitEntryChunks', () => {
937970
it('Not production', () => {
938971
const config = createConfig();

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ describe('Public API', () => {
243243

244244
});
245245

246+
describe('configureBabelPresetEnv', () => {
247+
248+
it('must return the API object', () => {
249+
const returnedValue = api.configureBabelPresetEnv(() => {});
250+
expect(returnedValue).to.equal(api);
251+
});
252+
253+
});
254+
246255
describe('enableReactPreset', () => {
247256

248257
it('must return the API object', () => {

0 commit comments

Comments
 (0)