Skip to content

Commit

Permalink
Configure Babel plugin to remove React propTypes for production builds
Browse files Browse the repository at this point in the history
Configured the `babel-plugin-transform-react-remove-prop-types` Babel
plugin (1) to remove all React `propTypes` when building a bundle in
production mode. It is implemented through Gatsby's Node API (1) using
the provided `setBabelOptions` action (3).

References:

  (1) https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types
  (2) https://www.gatsbyjs.org/docs/node-apis
  (3) https://www.gatsbyjs.org/docs/actions

GH-29
  • Loading branch information
arcticicestudio committed Nov 19, 2018
1 parent 9e8aea6 commit 8c4d81d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gatsby/onCreateBabelConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* @since 0.1.0
*/
const onCreateBabelConfig = ({ actions }) => {
const r = m => require.resolve(m);

/*
* Allows to use the "ES Class Fields & Static Properties" proposal to transforms static class properties as well as
* properties declared with the experimental property initializer syntax.
Expand Down Expand Up @@ -80,6 +82,30 @@ const onCreateBabelConfig = ({ actions }) => {
loose: false
}
});

/*
* Removes unnecessary React `propTypes` from production builds.
*
* @see https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types
*/
actions.setBabelOptions({
options: {},
config: {
env: {
production: {
plugins: [
[
r("babel-plugin-transform-react-remove-prop-types"),
{
removeImport: true,
ignoreFilenames: ["node_modules"]
}
]
]
}
}
}
});
};

module.exports = onCreateBabelConfig;

0 comments on commit 8c4d81d

Please sign in to comment.