Skip to content

Commit 8c4d81d

Browse files
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
1 parent 9e8aea6 commit 8c4d81d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

.gatsby/onCreateBabelConfig.js

+26
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* @since 0.1.0
2828
*/
2929
const onCreateBabelConfig = ({ actions }) => {
30+
const r = m => require.resolve(m);
31+
3032
/*
3133
* Allows to use the "ES Class Fields & Static Properties" proposal to transforms static class properties as well as
3234
* properties declared with the experimental property initializer syntax.
@@ -80,6 +82,30 @@ const onCreateBabelConfig = ({ actions }) => {
8082
loose: false
8183
}
8284
});
85+
86+
/*
87+
* Removes unnecessary React `propTypes` from production builds.
88+
*
89+
* @see https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types
90+
*/
91+
actions.setBabelOptions({
92+
options: {},
93+
config: {
94+
env: {
95+
production: {
96+
plugins: [
97+
[
98+
r("babel-plugin-transform-react-remove-prop-types"),
99+
{
100+
removeImport: true,
101+
ignoreFilenames: ["node_modules"]
102+
}
103+
]
104+
]
105+
}
106+
}
107+
}
108+
});
83109
};
84110

85111
module.exports = onCreateBabelConfig;

0 commit comments

Comments
 (0)