Skip to content

Commit

Permalink
minor #671 Add Encore.configureBabelPresetEnv() to the public API (Ly…
Browse files Browse the repository at this point in the history
…rkan)

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
  • Loading branch information
weaverryan committed Nov 23, 2019
2 parents c28e360 + d736625 commit 8fc1cb1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
25 changes: 25 additions & 0 deletions index.js
Expand Up @@ -895,6 +895,31 @@ class Encore {
return this;
}

/**
* Configure @babel/preset-env
*
* https://babeljs.io/docs/en/babel-preset-env
*
* ```
* Encore.configureBabelPresetEnv(function(options) {
* // change the @babel/preset-env config
* // if you use an external Babel configuration
* // this callback will NOT be used
* // options.corejs = 3;
* // options.useBuiltIns = 'usage';
* // ...
* });
* ```
*
* @param {function} callback
* @returns {Encore}
*/
configureBabelPresetEnv(callback) {
webpackConfig.configureBabelPresetEnv(callback);

return this;
}

/**
* Configure the css-loader.
*
Expand Down
33 changes: 33 additions & 0 deletions test/config-generator.js
Expand Up @@ -933,6 +933,39 @@ describe('The config-generator function', () => {
});
});

describe('Test configureBabelPresetEnv()', () => {
it('without configureBabelPresetEnv()', () => {
const config = createConfig();
config.outputPath = '/tmp/output/public-path';
config.publicPath = '/public-path';
config.addEntry('main', './main');

const actualConfig = configGenerator(config);

const jsRule = findRule(/\.jsx?$/, actualConfig.module.rules);
const babelLoader = jsRule.use.find(loader => loader.loader === 'babel-loader');
const babelEnvPreset = babelLoader.options.presets.find(([name]) => name === '@babel/preset-env');
expect(babelEnvPreset[1].useBuiltIns).to.equal(false);
});

it('with configureBabelPresetEnv()', () => {
const config = createConfig();
config.outputPath = '/tmp/output/public-path';
config.publicPath = '/public-path';
config.addEntry('main', './main');
config.configureBabelPresetEnv(options => {
options.useBuiltIns = 'usage';
});

const actualConfig = configGenerator(config);

const jsRule = findRule(/\.jsx?$/, actualConfig.module.rules);
const babelLoader = jsRule.use.find(loader => loader.loader === 'babel-loader');
const babelEnvPreset = babelLoader.options.presets.find(([name]) => name === '@babel/preset-env');
expect(babelEnvPreset[1].useBuiltIns).to.equal('usage');
});
});

describe('Test shouldSplitEntryChunks', () => {
it('Not production', () => {
const config = createConfig();
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Expand Up @@ -243,6 +243,15 @@ describe('Public API', () => {

});

describe('configureBabelPresetEnv', () => {

it('must return the API object', () => {
const returnedValue = api.configureBabelPresetEnv(() => {});
expect(returnedValue).to.equal(api);
});

});

describe('enableReactPreset', () => {

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

0 comments on commit 8fc1cb1

Please sign in to comment.