{"id":17274596,"url":"https://github.com/rip21/styled-components-v3-to-v4-codemod","last_synced_at":"2025-04-14T09:03:06.351Z","repository":{"id":57373289,"uuid":"146295548","full_name":"RIP21/styled-components-v3-to-v4-codemod","owner":"RIP21","description":"Codemod to migrate deprecated .extend API in favor of only styled functions.  ","archived":false,"fork":false,"pushed_at":"2018-11-12T16:35:29.000Z","size":38,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-23T13:18:39.329Z","etag":null,"topics":["babel-codemod","codemod","styled-components","v4"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RIP21.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-27T12:38:38.000Z","updated_at":"2023-08-15T16:14:58.000Z","dependencies_parsed_at":"2022-08-30T00:40:47.357Z","dependency_job_id":null,"html_url":"https://github.com/RIP21/styled-components-v3-to-v4-codemod","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RIP21%2Fstyled-components-v3-to-v4-codemod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RIP21%2Fstyled-components-v3-to-v4-codemod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RIP21%2Fstyled-components-v3-to-v4-codemod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RIP21%2Fstyled-components-v3-to-v4-codemod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RIP21","download_url":"https://codeload.github.com/RIP21/styled-components-v3-to-v4-codemod/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219844685,"owners_count":16556483,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["babel-codemod","codemod","styled-components","v4"],"created_at":"2024-10-15T08:54:17.495Z","updated_at":"2024-10-15T08:54:18.324Z","avatar_url":"https://github.com/RIP21.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# styled-components-v3-to-v4-codemod\n\n[![npm version](https://badge.fury.io/js/styled-components-v3-to-v4-codemod.svg)](https://badge.fury.io/js/styled-components-v3-to-v4-codemod)\n\n### This codemod has been adopted by `styled-components`, please use it instead [styled-components-codemods](https://github.com/styled-components/styled-components-codemods)\n\nCodemod to for `styled-components` to migrate from `v3`to `v4`.\n\nIn version 4 of `styled-components` the `.extends` API will be removed.\nThis codemod helps replace all of `.extends` usages to equivalent\nwith `styled()` call instead.\nAlso imports will be added if `styled` import is not present.\n\nBefore:\n```javascript\nStyledComponent.extend``\n\nStyledComponent.extend`color: red;`\n\nStyledComponent.extend({ color: \"red\" })\n\nStyledComponent.extend\n\nStyledComponent.extend``.extend\n\nStyledComponent.extend({ color: red }).extend\n\nstyled.div``.extend``\n\nstyled.div`color: red;`.extend`color: blue;`\n\nstyled.div({ color: \"red\"}).extend({ color: \"blue\"})\n\nStyledComponent.withComponent('div').extend``\n\nStyledComponent.withComponent('div').extend`color: red;`\n\nStyledComponent.withComponent('div').extend()\n\nStyledComponent.withComponent('div').extend({ color: red })\n\nStyledComponent.extend().extend().extend().extend``\n\nStyledComponent.extend``.extend().extend``.extend``\n```\n\nAfter:\n```jsx harmony\nimport styled, { css } from 'styled-components'\n\nstyled(StyledComponent)``\n\nstyled(StyledComponent)`\n  color: red;\n`\n\nstyled(StyledComponent)({ color: 'red' })\n\nstyled(StyledComponent)\n\nstyled(styled(StyledComponent)``)\n\nstyled(styled(StyledComponent)({ color: red }))\n\nstyled(styled.div``)``\n\nstyled(\n  styled.div`\n    color: red;\n  `\n)`\n  color: blue;\n`\n\nstyled(styled.div({ color: 'red' }))({ color: 'blue' })\n\nstyled(StyledComponent.withComponent('div'))``\n\nstyled(StyledComponent.withComponent('div'))`\n  color: red;\n`\n\nstyled(StyledComponent.withComponent('div'))()\n\nstyled(StyledComponent.withComponent('div'))({ color: red })\n\nstyled(styled(styled(styled(StyledComponent)())())())``\n\nstyled(styled(styled(styled(StyledComponent)``)())``)``\n```\n\n## Usage\n\nInstall [babel-codemod](https://github.com/square/babel-codemod) `npm i -g babel-codemod`\n\nThen install in root of your project `npm install styled-components-v3-to-v4-codemod`\n\nRun it like that from `node_modules` hence:\n\n```\ncodemod --plugin ./node_modules/styled-components-v3-to-v4-codemod/src/index.js ./src\n```\n\nAlso you may like to pretty print it using `prettier` instead of `recast`\n\n```\ncodemod --plugin ./node_modules/styled-components-v3-to-v4-codemod/src/index.js ./src --printer prettier\n```\n\nRemove `styled-components-v3-to-v4-codemod` from `package.json`\n\nIf there is any issues, let me know in the issues tab here at GitHub.\n\n## Limitations\n\nThere is no way to distinct from AST whether `.extend` identifier is related to `styled-components`\nor any other library/prototype etc. So if you know that there is some\n`.extend` function in your project that is not related to `styled-components` be aware\nand revert such changes manually diffing your code afterwards.\n\nAlso be aware that `.extend` in some rare cases is not equal `styled` and this codemod doesn't cover this case.\nRefer to this [issue](https://github.com/styled-components/styled-components/issues/1956) to understand the difference.\nIt's rare usage, so fix it manually. Shouldn't be a big deal.\n\n## Integration with WebStorm/VS Code to do migration file by file\n\nPreconditions:\n\n```\nnpm i -g babel-core babel-codemod styled-components-v3-to-v4-codemod\n```\n\n### WebStorm:\n\n1.  Go to Preferences -\u003e External Tools -\u003e Click plus to add tool.\n2.  Config:\n\n```\nName: SC v3 to v4\nProgram: codemod\nArguments: -p /usr/local/lib/node_modules/styled-components-v3-to-v4-codemod/src/index.js$FilePathRelativeToProjectRoot$\nWorking directory: $ProjectFileDir$\n\nIn advanced settings:\nTick on: Sync file after execution\n```\n\n3.  Open file you want to transform\n    `Right Click -\u003e External Tools -\u003e SC v3 to v4 -\u003e Apply prettier/code formatting -\u003e Enjoy`\n4.  For even better experience go to.\n    `Preferences -\u003e Keymap -\u003e External Tools -\u003e External Tools -\u003e h to JSX -\u003e Attach some key combination`\n\n### VS Code:\n\n1.  Open command pallete\n2.  `\u003eTasks: Configure Task`\n3.  Press Up -\u003e Select: `Task from tasks.json template` (or something like that)\n4.  Copy and paste this:\n\n```\n{\n    // See https://go.microsoft.com/fwlink/?LinkId=733558\n    // for the documentation about the tasks.json format\n    \"version\": \"2.0.0\",\n    \"tasks\": [\n        {\n            \"label\": \"SC v3 to v4\",\n            \"type\": \"shell\",\n            \"command\": \"codemod -p /usr/local/lib/node_modules/styled-components-v3-to-v4-codemod/src/index.js ${file}\"\n        }\n    ]\n}\n```\n\n5.  Open command pallete and ask it to open `keybindings.json`\n6.  Add this:\n\n```\n    {\n        \"key\": \"cmd+e\",\n        \"command\": \"workbench.action.tasks.runTask\",\n        \"args\": \"SC v3 to v4\"\n    }\n```\n\n7.  Open any file and press cmd+e to apply codemod on file.\n8.  Or if you don't want to bloat your `keybindings.json` just open Command pallete and type.\n    `Run task -\u003e Enter -\u003e Find in the list \"H to JSX\" -\u003e Enter` (Usually will be on top)\n9.  Apply formatting and enjoy\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frip21%2Fstyled-components-v3-to-v4-codemod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frip21%2Fstyled-components-v3-to-v4-codemod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frip21%2Fstyled-components-v3-to-v4-codemod/lists"}