This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/migrate-antd5-remove-style-import-codemo (#516)
- Loading branch information
1 parent
01ca264
commit dfce804
Showing
16 changed files
with
536 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Remove Style Import | ||
|
||
## Description | ||
Comment out the style file import from antd (in js file). | ||
|
||
## Example | ||
|
||
### Before | ||
|
||
```ts | ||
import 'antd/es/auto-complete/style'; | ||
import 'antd/lib/button/style/index.less'; | ||
import 'antd/dist/antd.compact.min.css'; | ||
|
||
``` | ||
|
||
### After | ||
|
||
```ts | ||
// import 'antd/es/auto-complete/style'; | ||
// import 'antd/lib/button/style/index.less'; | ||
// import 'antd/dist/antd.compact.min.css'; | ||
``` | ||
|
||
## Applicability Criteria | ||
|
||
Ant Design >= 5.0.0 | ||
|
||
## Other Metadata | ||
|
||
### Codemod Version | ||
|
||
v1.0.0 | ||
|
||
### Change Mode | ||
|
||
**Assistive**: The automation partially completes changes. Human involvement is needed to make changes ready to be pushed and merged. | ||
|
||
### **Codemod Engine** | ||
|
||
[jscodeshift](https://github.com/facebook/jscodeshift) | ||
|
||
### Estimated Time Saving | ||
|
||
Up to 1 minutes per occurrence | ||
|
||
### Owner | ||
|
||
[Intuita](https://github.com/intuita-inc) | ||
|
||
### Links for more info | ||
|
||
- https://github.com/ant-design/codemod-v5/tree/main?tab=readme-ov-file#v5-remove-style-import |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "schemaVersion": "1.0.0", "engine": "jscodeshift", "arguments": [] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import type { API, FileInfo } from 'jscodeshift'; | ||
export default function transform(file: FileInfo, api: API): string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@codemod-registry/antd-5-v5-remove-style-import", | ||
"dependencies": { | ||
"@codemod-registry/codemods/antd/5/utils": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@codemod-registry/tsconfig": "workspace:*", | ||
"@codemod-registry/utilities": "workspace:*", | ||
"@codemod-registry/cjs-builder": "workspace:*", | ||
"typescript": "^5.2.2", | ||
"esbuild": "0.19.5", | ||
"ts-node": "^10.9.1", | ||
"jscodeshift": "^0.15.1", | ||
"@types/jscodeshift": "^0.11.10" | ||
}, | ||
"main": "./dist/index.cjs", | ||
"types": "/dist/index.d.ts", | ||
"scripts": { | ||
"build:cjs": "cjs-builder ./src/index.js" | ||
}, | ||
"files": [ | ||
"README.md", | ||
"config.json", | ||
"./dist/index.cjs", | ||
"./index.d.ts" | ||
], | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/*! @license | ||
MIT LICENSE | ||
Copyright (c) 2015-present Ant UED, https://xtech.antfin.com/ | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
The source code has been taken from https://raw.githubusercontent.com/ant-design/codemod-v5/main/transforms/v5-remove-style-import.js | ||
Changes: migrate imports from cjs to esm | ||
*/ | ||
|
||
import { printOptions } from '@codemod-registry/codemods/antd/5/utils/config'; | ||
import { parseStrToArray } from '@codemod-registry/codemods/antd/5/utils'; | ||
|
||
// handle forked antd | ||
const commentOutStyleImport = [ | ||
// antd/es/auto-complete/style | ||
/(es|lib)\/.+\/style.*/, | ||
// antd/dist/antd.compact.min.css | ||
/dist\/.+\.(less|css)/, | ||
]; | ||
|
||
const transform = (file, api, options) => { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
const antdPkgNames = parseStrToArray(options.antdPkgNames || 'antd'); | ||
|
||
// import 'antd/es/auto-complete/style'; | ||
// import 'antd/dist/antd.compact.min.css'; | ||
function removeStyleImport(j, root) { | ||
let hasChanged = false; | ||
|
||
const regexList = antdPkgNames | ||
.map((antdPkg) => { | ||
return new RegExp( | ||
[ | ||
antdPkg, | ||
`(${commentOutStyleImport | ||
.map((re) => re.source) | ||
.join('|')})`, | ||
].join('/'), | ||
); | ||
}) | ||
.concat( | ||
// import '@ant-design/compatible/assets/index.css'; | ||
new RegExp('@ant-design/compatible/assets/index\\.css'), | ||
); | ||
|
||
// import { Comment, PageHeader } from 'antd'; | ||
// import { Comment, PageHeader } from '@forked/antd'; | ||
root.find(j.ImportDeclaration) | ||
.filter( | ||
(path) => | ||
path.node.source.type === 'StringLiteral' && | ||
regexList.some((re) => re.test(path.node.source.value)), | ||
) | ||
.forEach((path) => { | ||
hasChanged = true; | ||
j(path).replaceWith((path) => { | ||
// 不加空行会导致无法执行 root.toSource() | ||
const empty = j.emptyStatement(); | ||
// add indent | ||
empty.comments = [ | ||
j.commentLine(' ' + j(path.node).toSource()), | ||
]; | ||
return empty; | ||
}); | ||
}); | ||
|
||
return hasChanged; | ||
} | ||
|
||
// step1. import deprecated components from '@ant-design/compatible' | ||
// step2. cleanup antd import if empty | ||
let hasChanged = false; | ||
hasChanged = removeStyleImport(j, root) || hasChanged; | ||
|
||
return hasChanged | ||
? root.toSource(options.printOptions || printOptions) | ||
: null; | ||
}; | ||
|
||
export default transform; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "@codemod-registry/tsconfig", | ||
"include": ["./src/**/*.{ts,js}"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/*! @license | ||
MIT LICENSE | ||
Copyright (c) 2015-present Ant UED, https://xtech.antfin.com/ | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
The source code has been taken from https://github.com/ant-design/codemod-v5/blob/main/transforms/utils/config.js | ||
*/ | ||
|
||
// https://github.com/benjamn/recast/blob/master/lib/options.ts | ||
export const printOptions = { | ||
quote: 'single', | ||
}; |
Oops, something went wrong.