Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Encore.configureBabelPresetEnv() to the public API #671

Merged
merged 1 commit into from Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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