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
feat/migrate-antd5-remove-style-import-codemo #516
Merged
DmytroHryshyn
merged 6 commits into
main
from
12-11-feat/migrate-antd5-remove-style-import-codemo
Dec 12, 2023
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
30f5e4c
feat/migrate-antd5-remove-style-import-codemo
DmytroHryshyn a4b94b5
prettier, imports
DmytroHryshyn a71ba5f
fix
DmytroHryshyn f65d8b2
fix externals
DmytroHryshyn ce65831
fix
DmytroHryshyn 7828134
build
DmytroHryshyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"cjs-builder": "./dist/index.js" | ||
}, | ||
"scripts": { | ||
"build": "esbuild ./index.ts --bundle --platform=node --target=node16 --format=esm --outfile=./dist/index.js --external:esbuild", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not |
||
"postinstall": "tsc" | ||
}, | ||
"devDependencies": { | ||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm what about
@intuita-inc/filemod
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we only import types from it, so its fine