Skip to content

Add Preact preset (with preact-compat support) #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,26 @@ const publicApi = {
return this;
},

/**
* If enabled, a Preact preset will be applied to
* the generated Webpack configuration.
*
* Encore.enablePreactPreset()
*
* If you wish to also use preact-compat (https://github.com/developit/preact-compat)
* you can enable it by setting the "preactCompat" option to true:
*
* Encore.enablePreactPreset({ preactCompat: true })
*
* @param {object} options
* @returns {exports}
*/
enablePreactPreset(options = {}) {
webpackConfig.enablePreactPreset(options);

return this;
},

/**
* Call this if you plan on loading TypeScript files.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class WebpackConfig {
this.providedVariables = {};
this.babelConfigurationCallback = function() {};
this.useReact = false;
this.usePreact = false;
this.preactOptions = {
preactCompat: false
};
this.useVueLoader = false;
this.vueLoaderOptionsCallback = () => {};
this.loaders = [];
Expand Down Expand Up @@ -247,6 +251,18 @@ class WebpackConfig {
this.useReact = true;
}

enablePreactPreset(options = {}) {
this.usePreact = true;

for (const optionKey of Object.keys(options)) {
if (!(optionKey in this.preactOptions)) {
throw new Error(`Invalid option "${optionKey}" passed to enablePreactPreset(). Valid keys are ${Object.keys(this.preactOptions).join(', ')}`);
}

this.preactOptions[optionKey] = options[optionKey];
}
}

enableTypeScriptLoader(callback = () => {}) {
this.useTypeScriptLoader = true;

Expand Down
5 changes: 5 additions & 0 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class ConfigGenerator {
config.resolve.alias['vue$'] = 'vue/dist/vue.esm.js';
}

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

return config;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ const features = {
packages: ['babel-preset-react'],
description: 'process React JS files'
},
preact: {
method: 'enablePreactPreset()',
packages: ['babel-plugin-transform-react-jsx'],
description: 'process Preact JS files'
},
typescript: {
method: 'enableTypeScriptLoader()',
packages: ['typescript', 'ts-loader'],
Expand Down
17 changes: 17 additions & 0 deletions lib/loaders/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ module.exports = {
babelConfig.presets.push('react');
}

if (webpackConfig.usePreact) {
loaderFeatures.ensurePackagesExist('preact');

if (webpackConfig.preactOptions.preactCompat) {
// If preact-compat is enabled tell babel to
// transform JSX into React.createElement calls.
babelConfig.plugins.push(['transform-react-jsx']);
} else {
// If preact-compat is disabled tell babel to
// transform JSX into Preact h() calls.
babelConfig.plugins.push([
'transform-react-jsx',
{ 'pragma': 'h' }
]);
}
}

// allow for babel config to be controlled
webpackConfig.babelConfigurationCallback.apply(
// use babelConfig as the this variable
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"autoprefixer": "^6.7.7",
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-preset-react": "^6.23.0",
"chai": "^3.5.0",
"chai-fs": "^1.0.0",
Expand All @@ -63,6 +64,8 @@
"node-sass": "^4.5.3",
"nsp": "^2.6.3",
"postcss-loader": "^1.3.3",
"preact": "^8.2.1",
"preact-compat": "^3.17.0",
"sass-loader": "^6.0.3",
"sinon": "^2.3.4",
"ts-loader": "^2.1.0",
Expand Down
29 changes: 29 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,35 @@ describe('WebpackConfig object', () => {
});
});

describe('enablePreactPreset', () => {
it('Without preact-compat', () => {
const config = createConfig();
config.enablePreactPreset();

expect(config.usePreact).to.be.true;
expect(config.preactOptions.preactCompat).to.be.false;
});

it('With preact-compat', () => {
const config = createConfig();
config.enablePreactPreset({
preactCompat: true
});

expect(config.usePreact).to.be.true;
expect(config.preactOptions.preactCompat).to.be.true;
});

it('With an invalid option', () => {
const config = createConfig();
expect(() => {
config.enablePreactPreset({
foo: true
});
}).to.throw('Invalid option "foo"');
});
});

describe('enableTypeScriptLoader', () => {
it('Calling method sets it', () => {
const config = createConfig();
Expand Down
28 changes: 28 additions & 0 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,32 @@ describe('The config-generator function', () => {
expect(fontsRule.options.name).to.equal('[name].bar.[ext]');
});
});

describe('Test preact preset', () => {
describe('Without preact-compat', () => {
it('enablePreactPreset() does not add aliases to use preact-compat', () => {
const config = createConfig();
config.outputPath = '/tmp/public/build';
config.setPublicPath('/build/');
config.enablePreactPreset();

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

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

const actualConfig = configGenerator(config);
expect(actualConfig.resolve.alias).to.include.keys('react', 'react-dom');
expect(actualConfig.resolve.alias['react']).to.equal('preact-compat');
expect(actualConfig.resolve.alias['react-dom']).to.equal('preact-compat');
});
});
});
});
34 changes: 34 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,40 @@ module.exports = {
});
});

it('When enabled, preact JSX is transformed without preact-compat!', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
config.addEntry('main', './js/CoolReactComponent.jsx');
config.enablePreactPreset();

testSetup.runWebpack(config, (webpackAssert) => {
// check that babel transformed the JSX
webpackAssert.assertOutputFileContains(
'main.js',
'var hiGuys = h('
);

done();
});
});

it('When enabled, preact JSX is transformed with preact-compat!', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
config.addEntry('main', './js/CoolReactComponent.jsx');
config.enablePreactPreset({ preactCompat: true });

testSetup.runWebpack(config, (webpackAssert) => {
// check that babel transformed the JSX
webpackAssert.assertOutputFileContains(
'main.js',
'React.createElement'
);

done();
});
});

it('When configured, TypeScript is compiled!', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ describe('Public API', () => {

});

describe('enablePreactPreset', () => {

it('must return the API object', () => {
const returnedValue = api.enablePreactPreset();
expect(returnedValue).to.equal(api);
});

});

describe('enableTypeScriptLoader', () => {

it('must return the API object', () => {
Expand Down
36 changes: 36 additions & 0 deletions test/loaders/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,40 @@ describe('loaders/babel', () => {
// foo is also still there, not overridden
expect(actualLoaders[0].options.presets).to.include('foo');
});

it('getLoaders() with preact', () => {
const config = createConfig();
config.enablePreactPreset();

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

const actualLoaders = babelLoader.getLoaders(config);

// transform-react-jsx & foo
expect(actualLoaders[0].options.plugins).to.have.lengthOf(2);
expect(actualLoaders[0].options.plugins).to.deep.include.members([
['transform-react-jsx', { pragma: 'h' }],
'foo'
]);
});

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

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

const actualLoaders = babelLoader.getLoaders(config);

// transform-react-jsx & foo
expect(actualLoaders[0].options.plugins).to.have.lengthOf(2);
expect(actualLoaders[0].options.plugins).to.deep.include.members([
['transform-react-jsx'],
'foo'
]);
});
});
Loading