Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

feat/migrate-antd5-remove-style-import-codemo #516

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion cjs-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import esbuild from 'esbuild';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';

// list of packages that should be bundled to the codemod (e.g codemod internal utils)
const EXTERNAL_DEPENDENCIES = ['jscodeshift', 'ts-morph'];
Copy link
Contributor

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?

Copy link
Contributor Author

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


export const buildCjs = async () => {
const relativeInputFilePath = process.argv.at(2);

Expand All @@ -28,7 +31,7 @@ export const buildCjs = async () => {
const options: Parameters<typeof esbuild.build>[0] = {
entryPoints: [relativeInputFilePath],
bundle: true,
packages: 'external',
external: EXTERNAL_DEPENDENCIES,
platform: 'node',
minify: true,
minifyWhitespace: true,
Expand Down
1 change: 1 addition & 0 deletions cjs-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"cjs-builder": "./dist/index.js"
},
"scripts": {
"build": "tsc",
"postinstall": "tsc"
},
"devDependencies": {
Expand Down
53 changes: 53 additions & 0 deletions codemods/antd/5/remove-style-import/README.md
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
1 change: 1 addition & 0 deletions codemods/antd/5/remove-style-import/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "schemaVersion": "1.0.0", "engine": "jscodeshift", "arguments": [] }
2 changes: 2 additions & 0 deletions codemods/antd/5/remove-style-import/index.d.ts
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;
28 changes: 28 additions & 0 deletions codemods/antd/5/remove-style-import/package.json
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"
}
101 changes: 101 additions & 0 deletions codemods/antd/5/remove-style-import/src/index.js
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;
4 changes: 4 additions & 0 deletions codemods/antd/5/remove-style-import/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@codemod-registry/tsconfig",
"include": ["./src/**/*.{ts,js}"]
}
32 changes: 32 additions & 0 deletions codemods/antd/5/utils/config.js
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',
};