8000 Add options configuration callback to Encore.enableReactPreset() · symfony/webpack-encore@805915e · GitHub
[go: up one dir, main page]

Skip to content

Commit 805915e

Browse files
committed
Add options configuration callback to Encore.enableReactPreset()
1 parent 524aa92 commit 805915e

File tree

6 files changed

+84
-7
lines changed

6 files changed

+84
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
This is a new major version that contains several backwards-compatibility breaks.
66

7+
### Features
8+
9+
* #1344 Add options configuration callback to `Encore.enableReactPreset()` (@Kocal)
10+
711
### BC Breaks
812

913
* #1321 Drop support of Node.js 19 and 21 (@Kocal)

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,19 @@ class Encore {
11061106
*
11071107
* https://babeljs.io/docs/plugins/preset-react/
11081108
*
1109+
* You can configure the preset by passing a callback:
1110+
* ```
1111+
* Encore.enableReactPreset(function(options) {
1112+
* // https://babeljs.io/docs/babel-preset-react/#options
1113+
* options.development = !Encore.isProduction();
1114+
* });
1115+
* ```
1116+
*
1117+
* @param {OptionsCallback<object>} callback
11091118
* @returns {Encore}
11101119
*/
1111-
enableReactPreset() {
1112-
webpackConfig.enableReactPreset();
1120+
enableReactPreset(callback = () => {}) {
1121+
webpackConfig.enableReactPreset(callback);
11131122

11141123
return this;
11151124
}

lib/WebpackConfig.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ class WebpackConfig {
159159
/** @type {OptionsCallback<object>} */
160160
this.babelPresetEnvOptionsCallback = () => {};
161161
/** @type {OptionsCallback<object>} */
162+
this.babelReactPresetOptionsCallback = () => {};
163+
/** @type {OptionsCallback<object>} */
162164
this.cssLoaderConfigurationCallback = () => {};
163165
/** @type {OptionsCallback<object>} */
164166
this.styleLoaderConfigurationCallback = () => {};
@@ -793,8 +795,16 @@ class WebpackConfig {
793795
this.persistentCacheCallback = callback;
794796
}
795797

796-
enableReactPreset() {
798+
/**
799+
* @param {OptionsCallback<object>} callback
800+
*/
801+
enableReactPreset(callback = () => {}) {
802+
if (typeof callback !== 'function') {
803+
throw new Error('Argument 1 to enableForkedTypeScriptTypesChecking() must be a callback function.');
804+
}
805+
797806
this.useReact = true;
807+
this.babelReactPresetOptionsCallback = callback;
798808
}
799809

800810
enablePreactPreset(options = {}) {

lib/loaders/babel.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ module.exports = {
7070
if (webpackConfig.useReact) {
7171
loaderFeatures.ensurePackagesExistAndAreCorrectVersion('react');
7272

73-
babelConfig.presets.push([require.resolve('@babel/preset-react'), {
74-
// TODO: To remove when Babel 8, "automatic" will become the default value
75-
runtime: 'automatic',
76-
}]);
73+
babelConfig.presets.push([
74+
require.resolve('@babel/preset-react'),
75+
applyOptionsCallback(webpackConfig.babelReactPresetOptionsCallback, {
76+
// TODO: To remove when Babel 8, "automatic" will become the default value
77+
runtime: 'automatic',
78+
})
79+
]);
7780
}
7881

7982
if (webpackConfig.usePreact) {

test/WebpackConfig.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,23 @@ describe('WebpackConfig object', () => {
982982
});
983983
});
984984

985+
describe('enableReactPreset', () => {
986+
it('Calling method sets it', () => {
987+
const config = createConfig();
988+
const testCallback = () => {};
989+
config.enableReactPreset(testCallback);
990+
expect(config.babelReactPresetOptionsCallback).to.equal(testCallback);
991+
});
992+
993+
it('Calling with non-callback throws an error', () => {
994+
const config = createConfig();
995+
996+
expect(() => {
997+
config.enableReactPreset('FOO');
998+
}).to.throw('must be a callback function');
999+
});
1000+
});
1001+
9851002
describe('enablePreactPreset', () => {
9861003
it('Without preact-compat', () => {
9871004
const config = createConfig();

test/loaders/babel.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,40 @@ describe('loaders/babel', () => {
9494
expect(actualLoaders[0].options.presets[2]).to.equal('foo');
9595
});
9696

97+
it('getLoaders() with react and callback', () => {
98+
const config = createConfig();
99+
config.enableReactPreset((options) => {
100+
options.development = !config.isProduction();
101+
});
102+
103+
config.configureBabel(function(babelConfig) {
104+
babelConfig.presets.push('foo');
105+
});
106+
107+
const actualLoaders = babelLoader.getLoaders(config);
108+
109+
// env, react & foo
110+
expect(actualLoaders[0].options.presets).to.have.lengthOf(3);
111+
expect(actualLoaders[0].options.presets[0]).to.deep.equal([
112+
require.resolve('@babel/preset-env'),
113+
{
114+
corejs: null,
115+
modules: false,
116+
targets: {},
117+
useBuiltIns: false,
118+
},
119+
]);
120+
expect(actualLoaders[0].options.presets[1]).to.deep.equal([
121+
require.resolve('@babel/preset-react'),
122+
{
123+
runtime: 'automatic',
124+
development: true,
125+
}
126+
]);
127+
// foo is also still there, not overridden
128+
expect(actualLoaders[0].options.presets[2]).to.equal('foo');
129+
});
130+
97131
it('getLoaders() with preact', () => {
98132
const config = createConfig();
99133
config.enablePreactPreset();

0 commit comments

Comments
 (0)
0