styled-components-codemods
Automatic codemods to upgrade your styled-components code to newer versions.
Installation
Thanks to npx
no installation is needed, just run the command below!
Usage
Usage: npx styled-components-codemods [options] [command] Options: -V, --version output the version number -h, --help output usage information Commands: v4 [...files] Run all v4 codemods v4-extendToStyled [...files] Run just the extendToStyled codemod v4-injectGlobalToCreateGlobalStyle [...files] Run just the injectGlobalToCreateGlobalStyle codemod Examples: $ styled-components-codemods v4-extendToStyled src/components/Box.js src/components/Button.js $ styled-components-codemods v4 src/**/*.js
Codemods
v4
In version 4 of styled-components
the Component.extends
API will be removed in favor of only using styled(Component)
. This codemod replaces all .extends
calls to equivalent styled()
calls instead. Furthermore, the injectGlobal API has been upgraded to slot into React's lifecycle more naturally. It refactors all injectGlobal
calls, and warns you where they are, so you can export them and include them when rendering.
Limitations
There is no way to distinguish whether .extend
identifier is related to styled-components
or any other library/prototype etc. If you know that there is another .extend
function in your project that is not related to styled-components
be aware and revert these instances manually.
Be aware that
.extend
used in combination with.withComponent
can give you a different result thanstyled(WithComponentedComponent)
. Refer to this issue to understand the difference.
Example
Code Before
StyledComponentextend``; StyledComponentextend` color: red;`; StyledComponent; StyledComponentextend; StyledComponentextend``extend; StyledComponentextend; styleddiv``extend``; styleddiv` color: red;`extend`color: blue;`; styled; StyledComponentextend``; StyledComponentextend`color: red;`; StyledComponent; StyledComponent; StyledComponent extend``; StyledComponentextend``extend``extend``;
Code after
; ``; ` color: red;`; color: "red" ; ; ; ; ``; ` color: blue;`; color: "blue" ; ``; ` color: red;`; ; color: red ; ``; ``;