Skip to content

Commit 5066da7

Browse files
authored
feat: support whitelist and blacklist (#21)
* feat: support whitelist and blacklist * chore: update flow
1 parent f45bd72 commit 5066da7

File tree

4 files changed

+85
-53
lines changed

4 files changed

+85
-53
lines changed

.github/workflows/main.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ jobs:
1515

1616
- uses: actions/setup-node@v1
1717
with:
18-
node-version: '16'
18+
node-version: '20'
1919

2020
- name: cache package-lock.json
21-
uses: actions/cache@v2
21+
uses: actions/cache@v4
2222
with:
2323
path: package-temp-dir
2424
key: lock-${{ github.sha }}
@@ -35,29 +35,29 @@ jobs:
3535
3636
- name: cache node_modules
3737
id: node_modules_cache_id
38-
uses: actions/cache@v2
38+
uses: actions/cache@v4
3939
with:
4040
path: node_modules
4141
key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }}
4242

4343
- name: install
4444
if: steps.node_modules_cache_id.outputs.cache-hit != 'true'
4545
run: npm i
46-
46+
4747
lint:
4848
runs-on: ubuntu-latest
4949
steps:
5050
- name: checkout
5151
uses: actions/checkout@master
5252

5353
- name: restore cache from package-lock.json
54-
uses: actions/cache@v2
54+
uses: actions/cache@v4
5555
with:
5656
path: package-temp-dir
5757
key: lock-${{ github.sha }}
5858

5959
- name: restore cache from node_modules
60-
uses: actions/cache@v2
60+
uses: actions/cache@v4
6161
with:
6262
path: node_modules
6363
key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }}
@@ -66,21 +66,21 @@ jobs:
6666
run: npm run lint
6767

6868
needs: setup
69-
69+
7070
compile:
7171
runs-on: ubuntu-latest
7272
steps:
7373
- name: checkout
7474
uses: actions/checkout@master
7575

7676
- name: restore cache from package-lock.json
77-
uses: actions/cache@v2
77+
uses: actions/cache@v4
7878
with:
7979
path: package-temp-dir
8080
key: lock-${{ github.sha }}
8181

8282
- name: restore cache from node_modules
83-
uses: actions/cache@v2
83+
uses: actions/cache@v4
8484
with:
8585
path: node_modules
8686
key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }}
@@ -89,26 +89,26 @@ jobs:
8989
run: npm run compile
9090

9191
needs: setup
92-
92+
9393
coverage:
9494
runs-on: ubuntu-latest
9595
steps:
9696
- name: checkout
9797
uses: actions/checkout@master
9898

9999
- name: restore cache from package-lock.json
100-
uses: actions/cache@v2
100+
uses: actions/cache@v4
101101
with:
102102
path: package-temp-dir
103103
key: lock-${{ github.sha }}
104104

105105
- name: restore cache from node_modules
106-
uses: actions/cache@v2
106+
uses: actions/cache@v4
107107
with:
108108
path: node_modules
109109
key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }}
110110

111111
- name: coverage
112112
run: npm run coverage && bash <(curl -s https://codecov.io/bash)
113113

114-
needs: setup
114+
needs: setup

package.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
"@testing-library/react": "^13.0.0",
4141
"@types/classnames": "^2.2.10",
4242
"@types/jest": "^26.0.15",
43-
"@types/react": "^16.8.19",
44-
"@types/react-dom": "^16.8.4",
43+
"@types/react": "^18.3.23",
44+
"@types/react-dom": "^18.3.7",
4545
"antd": "^5.8.5",
4646
"cross-env": "^7.0.1",
4747
"dumi": "^2.1.0",
@@ -57,13 +57,7 @@
5757
},
5858
"dependencies": {
5959
"@ant-design/cssinjs": "^1.8.1",
60-
"@babel/runtime": "^7.18.3",
61-
"@rc-component/portal": "^1.1.0",
62-
"classnames": "^2.3.2",
63-
"rc-align": "^4.0.0",
64-
"rc-motion": "^2.0.0",
65-
"rc-resize-observer": "^1.3.1",
66-
"rc-util": "^5.27.1"
60+
"@babel/runtime": "^7.18.3"
6761
},
6862
"peerDependencies": {
6963
"antd": "^5.3.0",

src/index.tsx

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import {
2-
createCache,
3-
extractStyle as extStyle,
4-
StyleProvider,
5-
} from '@ant-design/cssinjs';
1+
import { createCache, extractStyle as extStyle, StyleProvider } from '@ant-design/cssinjs';
2+
import { renderToString } from 'react-dom/server';
63
import * as antd from 'antd';
74
import React from 'react';
8-
import { renderToString } from 'react-dom/server';
95
import type { CustomRender } from './interface';
106

11-
const blackList: string[] = [
7+
const defaultBlackList: string[] = [
128
'ConfigProvider',
139
'Drawer',
1410
'Grid',
@@ -39,39 +35,65 @@ const ComponentCustomizeRender: Record<
3935
Tree: (Tree) => <Tree treeData={[]} />,
4036
};
4137

42-
const defaultNode = () => (
43-
<>
44-
{Object.keys(antd)
45-
.filter(
46-
(name) =>
47-
!blackList.includes(name) && name[0] === name[0].toUpperCase(),
48-
)
49-
.map((compName) => {
50-
const Comp = antd[compName];
38+
interface NodeProps {
39+
excludes?: string[];
40+
includes?: string[];
41+
}
42+
43+
const defaultNode = ({ excludes = [], includes }: NodeProps) => {
44+
const components = includes ?? Object.keys(antd);
5145

52-
const renderFunc = ComponentCustomizeRender[compName];
46+
return (
47+
<>
48+
{components
49+
.filter(
50+
(name) =>
51+
![...defaultBlackList, ...excludes].includes(name) && name[0] === name[0].toUpperCase(),
52+
)
53+
.map((compName) => {
54+
const Comp = antd[compName];
5355

54-
if (renderFunc) {
55-
return (
56-
<React.Fragment key={compName}>{renderFunc(Comp)}</React.Fragment>
57-
);
58-
}
56+
const renderFunc = ComponentCustomizeRender[compName];
5957

60-
return <Comp key={compName} />;
61-
})}
62-
</>
63-
);
58+
if (renderFunc) {
59+
return (
60+
<React.Fragment key={compName}>{renderFunc(Comp)}</React.Fragment>
61+
);
62+
}
6463

65-
export function extractStyle(customTheme?: CustomRender): string {
64+
return <Comp key={compName} />;
65+
})}
66+
</>
67+
);
68+
}
69+
70+
export function extractStyle(arg?: CustomRender | {
71+
customTheme?: CustomRender,
72+
excludes?: string[],
73+
includes?: string[],
74+
}): string {
6675
const cache = createCache();
76+
77+
let customTheme: CustomRender | undefined;
78+
let excludes: string[];
79+
let includes: string[];
80+
if (typeof arg === 'function') {
81+
customTheme = arg;
82+
} else {
83+
({ customTheme, excludes, includes } = arg || {});
84+
}
85+
86+
const nodeProps: NodeProps = {
87+
includes,
88+
excludes,
89+
}
90+
6791
renderToString(
6892
<StyleProvider cache={cache}>
69-
{customTheme ? customTheme(defaultNode()) : defaultNode()}
93+
{customTheme ? customTheme(defaultNode(nodeProps)) : defaultNode(nodeProps)}
7094
</StyleProvider>,
7195
);
7296

7397
// Grab style from cache
74-
const styleText = extStyle(cache, true);
75-
76-
return styleText;
98+
return extStyle(cache, true);
7799
}

tests/index.test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,20 @@ describe('Static-Style-Extract', () => {
5555
));
5656
expect(cssText2).toContain(':where');
5757
});
58+
59+
it('whitelist should work', () => {
60+
const cssText = extractStyle({
61+
includes: ['Button']
62+
});
63+
expect(cssText).toContain('.ant-btn');
64+
expect(cssText).not.toContain('.ant-select');
65+
});
66+
67+
it('blacklist should work', () => {
68+
const cssText = extractStyle({
69+
excludes: ['Card']
70+
});
71+
expect(cssText).toContain('.ant-btn');
72+
expect(cssText).not.toContain('.ant-card');
73+
})
5874
});

0 commit comments

Comments
 (0)