Skip to content
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

babel-plugin-lodash #1088

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
43 changes: 30 additions & 13 deletions packages/babel-preset-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const plugins = [
// class { handleClick = () => { } }
require.resolve('babel-plugin-transform-class-properties'),
// The following two plugins use Object.assign directly, instead of Babel's
// extends helper. Note that this assumes `Object.assign` is available.
// extends helper. Note that this assumes `Object.assign` is available.
// { ...todo, completed: true }
[require.resolve('babel-plugin-transform-object-rest-spread'), {
useBuiltIns: true
Expand Down Expand Up @@ -82,6 +82,35 @@ if (env === 'test') {
],
plugins: plugins
};
} else if (env ==='production') {
// Optimization: hoist JSX that never changes out of render()
// Disabled because of issues:
// * https://github.com/facebookincubator/create-react-app/issues/525
// * https://phabricator.babeljs.io/search/query/pCNlnC2xzwzx/
// * https://github.com/babel/babel/issues/4516
// TODO: Enable again when these issues are resolved.
// plugins.push.apply(plugins, [
// require.resolve('babel-plugin-transform-react-constant-elements')
// ]);

module.exports = function(context, opts) {
// A simple transform to cherry-pick Lodash modules so you don’t have to.
if (opts && opts.isUsingLodash === true) {
plugins.push.apply(plugins, [
require.resolve('babel-plugin-lodash')
]);
}

return {
presets: [
// Latest stable ECMAScript features
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to move the whole block to provide a function to module.exports in order to have access to options object

require.resolve('babel-preset-latest'),
// JSX, Flow
require.resolve('babel-preset-react')
],
plugins: plugins
};
}
} else {
module.exports = {
presets: [
Expand All @@ -92,16 +121,4 @@ if (env === 'test') {
],
plugins: plugins
};

if (env === 'production') {
// Optimization: hoist JSX that never changes out of render()
// Disabled because of issues:
// * https://github.com/facebookincubator/create-react-app/issues/525
// * https://phabricator.babeljs.io/search/query/pCNlnC2xzwzx/
// * https://github.com/babel/babel/issues/4516
// TODO: Enable again when these issues are resolved.
// plugins.push.apply(plugins, [
// require.resolve('babel-plugin-transform-react-constant-elements')
// ]);
}
}
1 change: 1 addition & 0 deletions packages/babel-preset-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"index.js"
],
"dependencies": {
"babel-plugin-lodash": "3.2.10",
"babel-plugin-transform-class-properties": "6.16.0",
"babel-plugin-transform-object-rest-spread": "6.19.0",
"babel-plugin-transform-react-constant-elements": "6.9.1",
Expand Down
6 changes: 4 additions & 2 deletions packages/react-scripts/.babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"presets": ["react-app"]
}
"presets": [
["react-app", { "isUsingLodash": false } ]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if it's really needed, followed @gaearon specs:

It should be false by default.

]
}
9 changes: 7 additions & 2 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
var url = require('url');
var paths = require('./paths');
var getClientEnvironment = require('./env');
var appPackageJson = require(paths.appPackageJson)

function ensureSlash(path, needsSlash) {
var hasSlash = path.endsWith('/');
Expand All @@ -31,12 +32,16 @@ function ensureSlash(path, needsSlash) {
}
}

// If `lodash` is present into app package.json we apply `babel-plugin-lodash`
// through the `isUsingLodash` option of `babel-preset-react-app` to reduce build size
var isUsingLodash = !!appPackageJson.dependencies.lodash

// We use "homepage" field to infer "public path" at which the app is served.
// Webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
var homepagePath = require(paths.appPackageJson).homepage;
var homepagePath = appPackageJson.homepage;
var homepagePathname = homepagePath ? url.parse(homepagePath).pathname : '/';
// Webpack uses `publicPath` to determine where the app is being served from.
// It requires a trailing slash, or the file assets will get an incorrect path.
Expand Down Expand Up @@ -149,7 +154,7 @@ module.exports = {
// @remove-on-eject-begin
query: {
babelrc: false,
presets: [require.resolve('babel-preset-react-app')],
presets: [[require.resolve('babel-preset-react-app'), {isUsingLodash: isUsingLodash}]],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to write only { isUsingLodash } but I'm not sure of which node versions are supported by react-create-app

},
// @remove-on-eject-end
},
Expand Down