Skip to content

Commit 4ed6ab6

Browse files
committed
Improve ".configureBabel()" and ".configureBabelPresetEnv()" calls detection while validating Babel configuration
1 parent 8eda3f6 commit 4ed6ab6

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

lib/WebpackConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ class WebpackConfig {
149149
this.stylusLoaderOptionsCallback = () => {};
150150
/** @type {OptionsCallback<object>} */
151151
this.babelConfigurationCallback = () => {};
152+
this._configureBabelCalled = false;
152153
/** @type {OptionsCallback<object>} */
153154
this.babelPresetEnvOptionsCallback = () => {};
155+
this._configureBabelPresetEnvCalled = false;
154156
/** @type {OptionsCallback<object>} */
155157
this.babelReactPresetOptionsCallback = () => {};
156158
/** @type {OptionsCallback<object>} */
@@ -473,6 +475,7 @@ class WebpackConfig {
473475
}
474476

475477
this.babelConfigurationCallback = callback || (() => {});
478+
this._configureBabelCalled = callback !== null && callback !== undefined;
476479

477480
// Whitelist some options that can be used even if there
478481
// is an external Babel config. The other ones won't be
@@ -546,6 +549,7 @@ class WebpackConfig {
546549
}
547550

548551
this.babelPresetEnvOptionsCallback = callback;
552+
this._configureBabelPresetEnvCalled = true;
549553
}
550554

551555
/**

lib/config-generator.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,8 @@ class ConfigGenerator {
163163

164164
// Check from configureBabel(): if a callback was provided and an
165165
// external Babel config exists, that's an error.
166-
if (this.webpackConfig.babelConfigurationCallback &&
167-
this.webpackConfig.babelConfigurationCallback !== (() => {}).toString &&
168-
babelRcFileExists) {
169-
// The callback is non-default only when the user explicitly provided one
170-
// We stored the original callback, so check if it's not the default no-op
171-
const cb = this.webpackConfig.babelConfigurationCallback;
172-
const isDefault = cb.toString() === (() => {}).toString();
173-
if (!isDefault) {
174-
throw new Error('The "callback" argument of configureBabel() will not be used because your app already provides an external Babel configuration (e.g. a ".babelrc" or "babel.config.js" file or "babel" key in "package.json"). Use null as the first argument to remove this error.');
175-
}
166+
if (this.webpackConfig._configureBabelCalled && babelRcFileExists) {
167+
throw new Error('The "callback" argument of configureBabel() will not be used because your app already provides an external Babel configuration (e.g. a ".babelrc" or "babel.config.js" file or "babel" key in "package.json"). Use null as the first argument to remove this error.');
176168
}
177169

178170
// Check from configureBabel(): warn about options that won't be applied
@@ -187,14 +179,10 @@ class ConfigGenerator {
187179
}
188180
}
189181

190-
// Check from configureBabelPresetEnv(): if a non-default callback was
191-
// provided and an external Babel config exists, that's an error.
192-
if (babelRcFileExists) {
193-
const presetEnvCb = this.webpackConfig.babelPresetEnvOptionsCallback;
194-
const isPresetEnvDefault = presetEnvCb.toString() === (() => {}).toString();
195-
if (!isPresetEnvDefault) {
196-
throw new Error('The "callback" argument of configureBabelPresetEnv() will not be used because your app already provides an external Babel configuration (e.g. a ".babelrc" or "babel.config.js" file or "babel" key in "package.json").');
197-
}
182+
// Check from configureBabelPresetEnv(): if configureBabelPresetEnv()
183+
// was called and an external Babel config exists, that's an error.
184+
if (this.webpackConfig._configureBabelPresetEnvCalled && babelRcFileExists) {
185+
throw new Error('The "callback" argument of configureBabelPresetEnv() will not be used because your app already provides an external Babel configuration (e.g. a ".babelrc" or "babel.config.js" file or "babel" key in "package.json").');
198186
}
199187
}
200188

0 commit comments

Comments
 (0)