Skip to content

Commit c0cb4ea

Browse files
committed
Replace the usePreactCompat boolean by an object in Encore.enablePreactPreset()
1 parent 6b85366 commit c0cb4ea

File tree

8 files changed

+37
-18
lines changed

8 files changed

+37
-18
lines changed

index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,15 @@ const publicApi = {
394394
* Encore.enablePreactPreset()
395395
*
396396
* If you wish to also use preact-compat (https://github.com/developit/preact-compat)
397-
* call that method with its first parameter set
398-
* to true:
397+
* you can enable it by setting the "preactCompat" option to true:
399398
*
400-
* Encore.enablePreactPreset(true)
399+
* Encore.enablePreactPreset({ preactCompat: true })
401400
*
402-
* @param {boolean} usePreactCompat
401+
* @param {object} options
403402
* @returns {exports}
404403
*/
405-
enablePreactPreset(usePreactCompat = false) {
406-
webpackConfig.enablePreactPreset(usePreactCompat);
404+
enablePreactPreset(options = {}) {
405+
webpackConfig.enablePreactPreset(options);
407406

408407
return this;
409408
},

lib/WebpackConfig.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class WebpackConfig {
5454
this.babelConfigurationCallback = function() {};
5555
this.useReact = false;
5656
this.usePreact = false;
57-
this.usePreactCompat = false;
57+
this.preactOptions = {
58+
preactCompat: false
59+
};
5860
this.useVueLoader = false;
5961
this.vueLoaderOptionsCallback = () => {};
6062
this.loaders = [];
@@ -249,9 +251,16 @@ class WebpackConfig {
249251
this.useReact = true;
250252
}
251253

252-
enablePreactPreset(usePreactCompat = false) {
254+
enablePreactPreset(options = {}) {
253255
this.usePreact = true;
254-
this.usePreactCompat = usePreactCompat;
256+
257+
for (const optionKey of Object.keys(options)) {
258+
if (!(optionKey in this.preactOptions)) {
259+
throw new Error(`Invalid option "${optionKey}" passed to enablePreactPreset(). Valid keys are ${Object.keys(this.preactOptions).join(', ')}`);
260+
}
261+
262+
this.preactOptions[optionKey] = options[optionKey];
263+
}
255264
}
256265

257266
enableTypeScriptLoader(callback = () => {}) {

lib/config-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ConfigGenerator {
8181
config.resolve.alias['vue$'] = 'vue/dist/vue.esm.js';
8282
}
8383

84-
if (this.webpackConfig.usePreact && this.webpackConfig.usePreactCompat) {
84+
if (this.webpackConfig.usePreact && this.webpackConfig.preactOptions.preactCompat) {
8585
config.resolve.alias['react'] = 'preact-compat';
8686
config.resolve.alias['react-dom'] = 'preact-compat';
8787
}

lib/loaders/babel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module.exports = {
5353
if (webpackConfig.usePreact) {
5454
loaderFeatures.ensurePackagesExist('preact');
5555

56-
if (webpackConfig.usePreactCompat) {
56+
if (webpackConfig.preactOptions.preactCompat) {
5757
// If preact-compat is enabled tell babel to
5858
// transform JSX into React.createElement calls.
5959
babelConfig.plugins.push(['transform-react-jsx']);

test/WebpackConfig.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,26 @@ describe('WebpackConfig object', () => {
370370
config.enablePreactPreset();
371371

372372
expect(config.usePreact).to.be.true;
373-
expect(config.usePreactCompat).to.be.false;
373+
expect(config.preactOptions.preactCompat).to.be.false;
374374
});
375375

376376
it('With preact-compat', () => {
377377
const config = createConfig();
378-
config.enablePreactPreset(true);
378+
config.enablePreactPreset({
379+
preactCompat: true
380+
});
379381

380382
expect(config.usePreact).to.be.true;
381-
expect(config.usePreactCompat).to.be.true;
383+
expect(config.preactOptions.preactCompat).to.be.true;
384+
});
385+
386+
it('With an invalid option', () => {
387+
const config = createConfig();
388+
expect(() => {
389+
config.enablePreactPreset({
390+
foo: true
391+
});
392+
}).to.throw('Invalid option "foo"');
382393
});
383394
});
384395

test/config-generator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,11 @@ describe('The config-generator function', () => {
544544
});
545545

546546
describe('With preact-compat', () => {
547-
it('enablePreactPreset(true) adds aliases to use preact-compat', () => {
547+
it('enablePreactPreset({ preactCompat: true }) adds aliases to use preact-compat', () => {
548548
const config = createConfig();
549549
config.outputPath = '/tmp/public/build';
550550
config.setPublicPath('/build/');
551-
config.enablePreactPreset(true);
551+
config.enablePreactPreset({ preactCompat: true });
552552

553553
const actualConfig = configGenerator(config);
554554
expect(actualConfig.resolve.alias).to.include.keys('react', 'react-dom');

test/functional.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ module.exports = {
651651
const config = createWebpackConfig('www/build', 'dev');
652652
config.setPublicPath('/build');
653653
config.addEntry('main', './js/CoolReactComponent.jsx');
654-
config.enablePreactPreset(true);
654+
config.enablePreactPreset({ preactCompat: true });
655655

656656
testSetup.runWebpack(config, (webpackAssert) => {
657657
// check that babel transformed the JSX

test/loaders/babel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('loaders/babel', () => {
8686

8787
it('getLoaders() with preact and preact-compat', () => {
8888
const config = createConfig();
89-
config.enablePreactPreset(true);
89+
config.enablePreactPreset({ preactCompat: true });
9090

9191
config.configureBabel(function(babelConfig) {
9292
babelConfig.plugins.push('foo');

0 commit comments

Comments
 (0)