Skip to content

Replaces static property assignments on React components so that tree-shaking will work correctly

Notifications You must be signed in to change notification settings

mbrowne/babel-plugin-pure-static-props

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-pure-static-props

Fixes an issue with tree shaking that can occur when using static properties on React components using styled-components.

This plugin replaces static property assignments on React components (e.g. MyComponent.defaultProps = {...}) with Object.assign() statements annotated with /*#__PURE__*/ comments so that tree-shaking will work correctly.

Install

npm i -D babel-plugin-pure-static-props

Or:

yarn add -D babel-plugin-pure-static-props

Then add the plugin to your Babel config as explained in the Babel documentation.

Example babel.config.js:

module.exports = {
  presets: ['@babel/preset-env'],
  plugins: [
    '@babel/plugin-proposal-class-properties',
    'babel-plugin-pure-static-props',
  ],
}

Note on styled components

The tree-shaking issue with static properties on React components also affects styled components. This plugin only addresses tree-shaking for regular React components; it will not work on components created using the styled helper.

A fix for styled components is in this PR: styled-components/babel-plugin-styled-components#248.

About

Replaces static property assignments on React components so that tree-shaking will work correctly

Resources

Stars

Watchers

Forks

Packages

No packages published