From d736625ed2d60c67734c748699d519425fdd6b4d Mon Sep 17 00:00:00 2001 From: Lyrkan <850046+Lyrkan@users.noreply.github.com> Date: Wed, 20 Nov 2019 21:44:25 +0100 Subject: [PATCH] Add Encore.configureBabelPresetEnv() to the public API --- index.js | 25 +++++++++++++++++++++++++ test/config-generator.js | 33 +++++++++++++++++++++++++++++++++ test/index.js | 9 +++++++++ 3 files changed, 67 insertions(+) diff --git a/index.js b/index.js index 26e8afbd..fe24f50f 100644 --- a/index.js +++ b/index.js @@ -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. * diff --git a/test/config-generator.js b/test/config-generator.js index 741fb4c4..4f2e5767 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -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(); diff --git a/test/index.js b/test/index.js index cf2f72fd..8d711eb1 100644 --- a/test/index.js +++ b/test/index.js @@ -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', () => {