Skip to content

Commit ab9c52a

Browse files
committed
feat(remove-selector): add tdesign-config exports
1 parent f3b5d3d commit ab9c52a

File tree

5 files changed

+106
-77
lines changed

5 files changed

+106
-77
lines changed

jest.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ module.exports = {
1111
],
1212
},
1313
moduleNameMapper: {
14-
'^@plugin-light/(.*)$': '<rootDir>/packages/plugin-light-$1',
15-
'^@plugin-light/webpack-loader-(.*)$': '<rootDir>/packages/webpack-loader-$1',
1614
},
1715
testMatch: [
1816
'**/tests/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)',
Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,2 @@
1-
import { shouldHandleFile, shouldRemoveRule } from './helper';
2-
3-
import type { Options, PostCSSPlugin } from './types';
4-
5-
export type { Options, FileConfig, ShouldRemoveRuleOptions, PostCSSPlugin } from './types';
6-
export { shouldHandleFile, shouldRemoveRule, extractIconName } from './helper';
7-
8-
/**
9-
* PostCSS 插件:移除指定的 CSS 选择器
10-
* 用于图标样式减包等场景
11-
*
12-
* @param opts 配置项
13-
* @returns PostCSS 插件
14-
*/
15-
export function postCssPluginRemoveSelector(opts: Options = { list: [] }): PostCSSPlugin & { postcss: true } {
16-
const { list = [], debug = false } = opts;
17-
18-
const plugin: PostCSSPlugin & { postcss: true } = {
19-
postcssPlugin: 'postcss-plugin-remove-selector',
20-
postcss: true,
21-
Once(root, { result }) {
22-
const fileName = result.opts?.from || '';
23-
const found = shouldHandleFile(list, fileName);
24-
25-
if (!found) {
26-
return;
27-
}
28-
29-
const { exclude = [], include = [], selectorPattern } = found;
30-
if (debug) {
31-
console.log('[postcss-plugin-remove-selector] handling:', fileName);
32-
}
33-
34-
let removedCount = 0;
35-
36-
root.walkRules((rule) => {
37-
if (shouldRemoveRule({
38-
selectorPattern,
39-
exclude,
40-
include,
41-
selector: rule.selector,
42-
})) {
43-
rule.remove();
44-
removedCount += 1;
45-
}
46-
});
47-
48-
if (debug) {
49-
console.log(`[postcss-plugin-remove-selector] removed ${removedCount} rules from:`, fileName);
50-
}
51-
},
52-
};
53-
54-
return plugin;
55-
}
1+
export * from './plugin';
2+
export { TDESIGN_ICON_REMOVE_SELECTOR } from './tdesign-uniapp-icon';
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { shouldHandleFile, shouldRemoveRule } from './helper';
2+
3+
import type { Options, PostCSSPlugin } from './types';
4+
5+
export type { Options, FileConfig, ShouldRemoveRuleOptions, PostCSSPlugin } from './types';
6+
export { shouldHandleFile, shouldRemoveRule, extractIconName } from './helper';
7+
8+
/**
9+
* PostCSS 插件:移除指定的 CSS 选择器
10+
* 用于图标样式减包等场景
11+
*
12+
* @param opts 配置项
13+
* @returns PostCSS 插件
14+
*/
15+
export function postCssPluginRemoveSelector(opts: Options = { list: [] }): PostCSSPlugin & { postcss: true } {
16+
const { list = [], debug = false } = opts;
17+
18+
const plugin: PostCSSPlugin & { postcss: true } = {
19+
postcssPlugin: 'postcss-plugin-remove-selector',
20+
postcss: true,
21+
Once(root, { result }) {
22+
const fileName = result.opts?.from || '';
23+
const found = shouldHandleFile(list, fileName);
24+
25+
if (!found) {
26+
return;
27+
}
28+
29+
const { exclude = [], include = [], selectorPattern } = found;
30+
if (debug) {
31+
console.log('[postcss-plugin-remove-selector] handling:', fileName);
32+
}
33+
34+
let removedCount = 0;
35+
36+
root.walkRules((rule) => {
37+
if (shouldRemoveRule({
38+
selectorPattern,
39+
exclude,
40+
include,
41+
selector: rule.selector,
42+
})) {
43+
rule.remove();
44+
removedCount += 1;
45+
}
46+
});
47+
48+
if (debug) {
49+
console.log(`[postcss-plugin-remove-selector] removed ${removedCount} rules from:`, fileName);
50+
}
51+
},
52+
};
53+
54+
return plugin;
55+
}

packages/postcss-plugin-remove-selector/tests/remote-selector.spec.js

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
const postcss = require('postcss');
22

3-
const plugin = require('../src/index');
3+
const { postCssPluginRemoveSelector } = require('../src/index');
44

55
const input = `
6-
.press-icon-plus-arrow-down:before {
6+
.t-icon-arrow-down:before {
77
content: 'e65e';
88
}
99
10-
.press-icon-plus-arrow-up:before {
10+
.t-icon-arrow-up:before {
1111
content: 'e65f';
1212
}
1313
14-
.press-icon-plus-invitation:before {
14+
.t-icon-invitation:before {
1515
content: 'e6d6';
1616
}
1717
18-
.press-icon-plus-like-o:before {
18+
.t-icon-like-o:before {
1919
content: 'e6d7';
2020
}
2121
@@ -25,13 +25,12 @@ const input = `
2525
`;
2626

2727

28-
const getFullIconName = (name => `.press-icon-plus-${name}:before`);
29-
3028
test('exclude', async () => {
31-
const result = await postcss([plugin({
29+
const result = await postcss([postCssPluginRemoveSelector({
3230
list: [{
3331
file: 'abc',
34-
exclude: ['invitation', 'like-o'].map(getFullIconName),
32+
exclude: ['invitation', 'like-o'],
33+
selectorPattern: /^\.t-icon-[\w-]+:before$/,
3534
}],
3635
})]).process(input, { from: 'abc' });
3736

@@ -41,10 +40,11 @@ test('exclude', async () => {
4140

4241

4342
test('include', async () => {
44-
const result = await postcss([plugin({
43+
const result = await postcss([postCssPluginRemoveSelector({
4544
list: [{
4645
file: 'abc',
47-
include: ['arrow-down', 'arrow-up'].map(getFullIconName),
46+
include: ['arrow-down', 'arrow-up'],
47+
selectorPattern: /^\.t-icon-[\w-]+:before$/,
4848
}],
4949
})]).process(input, { from: 'abc' });
5050

@@ -54,11 +54,12 @@ test('include', async () => {
5454

5555

5656
test('include & exclude', async () => {
57-
const result = await postcss([plugin({
57+
const result = await postcss([postCssPluginRemoveSelector({
5858
list: [{
5959
file: 'abc',
60-
include: ['arrow-down', 'arrow-up'].map(item => `.press-icon-plus-${item}:before`),
61-
exclude: ['invitation', 'like-o', 'arrow-up'].map(getFullIconName),
60+
include: ['arrow-down', 'arrow-up'],
61+
exclude: ['invitation', 'like-o', 'arrow-up'],
62+
selectorPattern: /^\.t-icon-[\w-]+:before$/,
6263
}],
6364
})]).process(input, { from: 'abc' });
6465

@@ -68,20 +69,48 @@ test('include & exclude', async () => {
6869

6970

7071
test('file is regexp', async () => {
71-
const result = await postcss([plugin({
72+
const result = await postcss([postCssPluginRemoveSelector({
7273
list: [{
73-
file: new RegExp('press-ui/press-icon-plus/css/icon.scss'),
74-
include: ['arrow-down', 'arrow-up'].map(getFullIconName),
74+
file: /tdesign\/uniapp\/dist\/icon/,
75+
include: ['arrow-down', 'arrow-up'],
76+
selectorPattern: /^\.t-icon-[\w-]+:before$/,
7577
}],
76-
})]).process(input, { from: 'press-ui/press-icon-plus/css/icon.scss' });
78+
})]).process(input, { from: 'tdesign/uniapp/dist/icon/icon.css' });
7779

7880
expect(result.css).toMatchSnapshot();
7981
expect(result.warnings()).toHaveLength(0);
8082
});
8183

8284

8385
test('empty options', async () => {
84-
const result = await postcss([plugin()]).process(input, { from: 'press-ui/press-icon-plus/css/icon.scss' });
86+
const result = await postcss([postCssPluginRemoveSelector()]).process(input, { from: 'tdesign/uniapp/dist/icon/icon.css' });
87+
88+
expect(result.css).toMatchSnapshot();
89+
expect(result.warnings()).toHaveLength(0);
90+
});
91+
92+
93+
test('no selectorPattern - should not remove non-icon rules', async () => {
94+
const result = await postcss([postCssPluginRemoveSelector({
95+
list: [{
96+
file: 'abc',
97+
include: ['arrow-down'],
98+
}],
99+
})]).process(input, { from: 'abc' });
100+
101+
expect(result.css).toMatchSnapshot();
102+
expect(result.warnings()).toHaveLength(0);
103+
});
104+
105+
106+
test('file not matched - should not modify css', async () => {
107+
const result = await postcss([postCssPluginRemoveSelector({
108+
list: [{
109+
file: 'not-matched-file',
110+
exclude: ['arrow-down'],
111+
selectorPattern: /^\.t-icon-[\w-]+:before$/,
112+
}],
113+
})]).process(input, { from: 'abc' });
85114

86115
expect(result.css).toMatchSnapshot();
87116
expect(result.warnings()).toHaveLength(0);

packages/webpack-plugin-dispatch-script/tests/data/dispatch-script.result-deps.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105111,4 +105111,4 @@
105111105111
"node_modules/.pnpm/@tt+pmd-vue@1.0.149/node_modules/@tt/pmd-vue/lib/debug/index.ts",
105112105112
"src/project/user/views/home/homepage/account-manage.vue"
105113105113
]
105114-
}
105114+
}

0 commit comments

Comments
 (0)