From 2652b249686b0918c0fdeef2fcd5d9db9f4c4289 Mon Sep 17 00:00:00 2001 From: Arctic Ice Studio Date: Mon, 19 Nov 2018 13:48:30 +0100 Subject: [PATCH] Configure Babel plugin to remove React `propTypes` for production builds 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 --- .gatsby/onCreateBabelConfig.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/.gatsby/onCreateBabelConfig.js b/.gatsby/onCreateBabelConfig.js index 61c7293d..c8d6c80f 100644 --- a/.gatsby/onCreateBabelConfig.js +++ b/.gatsby/onCreateBabelConfig.js @@ -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. @@ -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;