Skip to content

Commit 6c8f073

Browse files
committed
Allow to configure the JS exclude rules through configureBabel
1 parent eedc8ca commit 6c8f073

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,23 @@ class Encore {
639639
*
640640
* Encore.configureBabel(function(babelConfig) {
641641
* // change the babelConfig
642+
* }, {
643+
* // set optional Encore-specific options
644+
* // exclude: /(node_modules|bower_components)/
642645
* });
643646
*
647+
* Supported options:
648+
* * {Condition} exclude (default=/(node_modules|bower_components)/)
649+
* A Webpack Condition passed to the JS/JSX rule that
650+
* determines which files and folders should not be
651+
* processed by Babel (https://webpack.js.org/configuration/module/#condition).
652+
*
644653
* @param {function} callback
654+
* @param {object} encoreOptions
645655
* @returns {Encore}
646656
*/
647-
configureBabel(callback) {
648-
webpackConfig.configureBabel(callback);
657+
configureBabel(callback, encoreOptions = {}) {
658+
webpackConfig.configureBabel(callback, encoreOptions);
649659

650660
return this;
651661
}

lib/WebpackConfig.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ class WebpackConfig {
7979
images: false,
8080
fonts: false
8181
};
82+
this.babelOptions = {
83+
exclude: /(node_modules|bower_components)/,
84+
};
8285

8386
// Features/Loaders options callbacks
8487
this.postCssLoaderOptionsCallback = () => {};
@@ -301,7 +304,7 @@ class WebpackConfig {
301304
this.useSourceMaps = enabled;
302305
}
303306

304-
configureBabel(callback) {
307+
configureBabel(callback, options = {}) {
305308
if (typeof callback !== 'function') {
306309
throw new Error('Argument 1 to configureBabel() must be a callback function.');
307310
}
@@ -311,6 +314,14 @@ class WebpackConfig {
311314
}
312315

313316
this.babelConfigurationCallback = callback;
317+
318+
for (const optionKey of Object.keys(options)) {
319+
if (!(optionKey in this.babelOptions)) {
320+
throw new Error(`Invalid option "${optionKey}" passed to configureBabel(). Valid keys are ${Object.keys(this.babelOptions).join(', ')}`);
321+
}
322+
323+
this.babelOptions[optionKey] = options[optionKey];
324+
}
314325
}
315326

316327
configureCssLoader(callback) {

lib/config-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class ConfigGenerator {
165165
{
166166
// match .js and .jsx
167167
test: /\.jsx?$/,
168-
exclude: /(node_modules|bower_components)/,
168+
exclude: this.webpackConfig.babelOptions.exclude,
169169
use: babelLoaderUtil.getLoaders(this.webpackConfig)
170170
},
171171
{

test/WebpackConfig.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ describe('WebpackConfig object', () => {
407407
const testCallback = () => {};
408408
config.configureBabel(testCallback);
409409
expect(config.babelConfigurationCallback).to.equal(testCallback);
410+
expect(String(config.babelOptions.exclude)).to.equal(String(/(node_modules|bower_components)/));
410411
});
411412

412413
it('Calling with non-callback throws an error', () => {
@@ -425,6 +426,21 @@ describe('WebpackConfig object', () => {
425426
config.configureBabel(() => {});
426427
}).to.throw('configureBabel() cannot be called because your app already has Babel configuration');
427428
});
429+
430+
it('Pass valid config', () => {
431+
const config = createConfig();
432+
config.configureBabel(() => {}, { exclude: 'foo' });
433+
434+
expect(config.babelOptions.exclude).to.equal('foo');
435+
});
436+
437+
it('Pass invalid config', () => {
438+
const config = createConfig();
439+
440+
expect(() => {
441+
config.configureBabel(() => {}, { fake_option: 'foo' });
442+
}).to.throw('Invalid option "fake_option" passed to configureBabel()');
443+
});
428444
});
429445

430446
describe('configureCssLoader', () => {

test/config-generator.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,35 @@ describe('The config-generator function', () => {
826826
});
827827
});
828828

829+
describe('Test configureBabel()', () => {
830+
it('without configureBabel()', () => {
831+
const config = createConfig();
832+
config.outputPath = '/tmp/output/public-path';
833+
config.publicPath = '/public-path';
834+
config.addEntry('main', './main');
835+
836+
const actualConfig = configGenerator(config);
837+
838+
const jsRule = findRule(/\.jsx?$/, actualConfig.module.rules);
839+
expect(String(jsRule.exclude)).to.equal(String(/(node_modules|bower_components)/));
840+
});
841+
842+
it('with configureBabel() and a different exclude rule', () => {
843+
const config = createConfig();
844+
config.outputPath = '/tmp/output/public-path';
845+
config.publicPath = '/public-path';
846+
config.addEntry('main', './main');
847+
config.configureBabel(() => {}, {
848+
exclude: /foo/
849+
});
850+
851+
const actualConfig = configGenerator(config);
852+
853+
const jsRule = findRule(/\.jsx?$/, actualConfig.module.rules);
854+
expect(String(jsRule.exclude)).to.equal(String(/foo/));
855+
});
856+
});
857+
829858
describe('Test shouldSplitEntryChunks', () => {
830859
it('Not production', () => {
831860
const config = createConfig();

0 commit comments

Comments
 (0)