@novlan/postcss-plugin-remove-selector 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +77 -0
- package/lib/helper.d.ts +18 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +96 -0
- package/lib/tdesign-uniapp-icon.d.ts +22 -0
- package/lib/types.d.ts +45 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 novlan1
|
|
4
|
+
|
|
5
|
+
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:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# 移除选择器
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="https://img.shields.io/npm/dw/@novlan/postcss-plugin-remove-selector">
|
|
5
|
+
<img src="https://img.shields.io/npm/unpacked-size/@novlan/postcss-plugin-remove-selector">
|
|
6
|
+
<img src="https://img.shields.io/npm/v/@novlan/postcss-plugin-remove-selector">
|
|
7
|
+
<img src="https://img.shields.io/npm/l/@novlan/postcss-plugin-remove-selector">
|
|
8
|
+
<img src="https://img.shields.io/github/last-commit/novlan1/plugins">
|
|
9
|
+
<img src="https://img.shields.io/github/created-at/novlan1/plugins">
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
可用于移除三方库中的不需要的样式,从而减小包体积。
|
|
13
|
+
|
|
14
|
+
## 1. 作者
|
|
15
|
+
|
|
16
|
+
**novlan1**
|
|
17
|
+
|
|
18
|
+
## 2. 如何使用
|
|
19
|
+
|
|
20
|
+
安装
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add @novlan/postcss-plugin-remove-selector -D
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
在 `vite.config.ts` 中使用:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { defineConfig } from 'vite';
|
|
30
|
+
import { postCssPluginRemoveSelector } from '@novlan/postcss-plugin-remove-selector';
|
|
31
|
+
import {
|
|
32
|
+
TDESIGN_ICON_REMOVE_SELECTOR
|
|
33
|
+
} from '@novlan/postcss-plugin-remove-selector/lib/tdesign-uniapp-icon';
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
css: {
|
|
38
|
+
postcss: {
|
|
39
|
+
plugins: [postCssPluginRemoveSelector(TDESIGN_ICON_REMOVE_SELECTOR)],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 3. 类型
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
interface FileConfig {
|
|
49
|
+
/** 文件匹配规则,可以是字符串或正则表达式 */
|
|
50
|
+
file: RegExp | string;
|
|
51
|
+
/** 需要保留的选择器列表(图标名称) */
|
|
52
|
+
include?: string[];
|
|
53
|
+
/** 需要移除的选择器列表(图标名称) */
|
|
54
|
+
exclude?: string[];
|
|
55
|
+
/** 选择器匹配模式,只处理匹配该模式的选择器 */
|
|
56
|
+
selectorPattern?: RegExp;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface Options {
|
|
60
|
+
/** 配置列表 */
|
|
61
|
+
list: FileConfig[];
|
|
62
|
+
/** 是否开启调试模式 */
|
|
63
|
+
debug?: boolean;
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## 4. 更新日志
|
|
68
|
+
|
|
69
|
+
[点此查看](./CHANGELOG.md)
|
|
70
|
+
|
|
71
|
+
## 5. 效果
|
|
72
|
+
|
|
73
|
+
使用此插件,可使小程序大小减少`15KB`。
|
|
74
|
+
|
|
75
|
+
<img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/3/own_mike_5110452c29f3a22c3a.png" width="600">
|
|
76
|
+
|
|
77
|
+
<img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/3/own_mike_d5703094c04270a6d5.png" width="600">
|
package/lib/helper.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FileConfig, ShouldRemoveRuleOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 判断是否应该处理该文件
|
|
4
|
+
* @param configList 配置列表
|
|
5
|
+
* @param current 当前文件路径
|
|
6
|
+
*/
|
|
7
|
+
export declare function shouldHandleFile(configList: FileConfig[] | undefined, current: string): FileConfig | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* 从选择器中提取图标名称
|
|
10
|
+
* @param selector 选择器,如 .t-icon-home:before
|
|
11
|
+
* @returns 图标名称,如 home
|
|
12
|
+
*/
|
|
13
|
+
export declare function extractIconName(selector: string): string | null;
|
|
14
|
+
/**
|
|
15
|
+
* 判断是否应该移除该规则
|
|
16
|
+
* @param options 配置项
|
|
17
|
+
*/
|
|
18
|
+
export declare function shouldRemoveRule(options: ShouldRemoveRuleOptions): boolean;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Options, PostCSSPlugin } from './types';
|
|
2
|
+
export type { Options, FileConfig, ShouldRemoveRuleOptions, PostCSSPlugin } from './types';
|
|
3
|
+
export { shouldHandleFile, shouldRemoveRule, extractIconName } from './helper';
|
|
4
|
+
/**
|
|
5
|
+
* PostCSS 插件:移除指定的 CSS 选择器
|
|
6
|
+
* 用于图标样式减包等场景
|
|
7
|
+
*
|
|
8
|
+
* @param opts 配置项
|
|
9
|
+
* @returns PostCSS 插件
|
|
10
|
+
*/
|
|
11
|
+
export declare function postCssPluginRemoveSelector(opts?: Options): PostCSSPlugin & {
|
|
12
|
+
postcss: true;
|
|
13
|
+
};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 判断是否应该处理该文件
|
|
7
|
+
* @param configList 配置列表
|
|
8
|
+
* @param current 当前文件路径
|
|
9
|
+
*/
|
|
10
|
+
function shouldHandleFile(configList = [], current) {
|
|
11
|
+
return configList.find((item) => {
|
|
12
|
+
const { file } = item;
|
|
13
|
+
if (typeof file === 'string') {
|
|
14
|
+
return current.includes(file);
|
|
15
|
+
}
|
|
16
|
+
return file.test(current);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 从选择器中提取图标名称
|
|
21
|
+
* @param selector 选择器,如 .t-icon-home:before
|
|
22
|
+
* @returns 图标名称,如 home
|
|
23
|
+
*/
|
|
24
|
+
function extractIconName(selector) {
|
|
25
|
+
const match = selector.match(/\.t-icon-([\w-]+):before/);
|
|
26
|
+
return match ? match[1] : null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 判断是否应该移除该规则
|
|
30
|
+
* @param options 配置项
|
|
31
|
+
*/
|
|
32
|
+
function shouldRemoveRule(options) {
|
|
33
|
+
const { selectorPattern, include, exclude, selector } = options;
|
|
34
|
+
// 如果配置了 selectorPattern,只处理匹配的选择器
|
|
35
|
+
if (selectorPattern && !selectorPattern.test(selector)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
// 提取图标名称进行精确匹配
|
|
39
|
+
const iconName = extractIconName(selector);
|
|
40
|
+
// 如果有 include 列表,只保留 include 中的图标
|
|
41
|
+
if (include.length) {
|
|
42
|
+
return !include.includes(iconName || '');
|
|
43
|
+
}
|
|
44
|
+
// 如果有 exclude 列表,移除 exclude 中的图标
|
|
45
|
+
if (exclude.length) {
|
|
46
|
+
return exclude.includes(iconName || '');
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* PostCSS 插件:移除指定的 CSS 选择器
|
|
53
|
+
* 用于图标样式减包等场景
|
|
54
|
+
*
|
|
55
|
+
* @param opts 配置项
|
|
56
|
+
* @returns PostCSS 插件
|
|
57
|
+
*/
|
|
58
|
+
function postCssPluginRemoveSelector(opts = { list: [] }) {
|
|
59
|
+
const { list = [], debug = false } = opts;
|
|
60
|
+
const plugin = {
|
|
61
|
+
postcssPlugin: 'postcss-plugin-remove-selector',
|
|
62
|
+
postcss: true,
|
|
63
|
+
Once(root, { result }) {
|
|
64
|
+
const fileName = result.opts?.from || '';
|
|
65
|
+
const found = shouldHandleFile(list, fileName);
|
|
66
|
+
if (!found) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const { exclude = [], include = [], selectorPattern } = found;
|
|
70
|
+
if (debug) {
|
|
71
|
+
console.log('[postcss-plugin-remove-selector] handling:', fileName);
|
|
72
|
+
}
|
|
73
|
+
let removedCount = 0;
|
|
74
|
+
root.walkRules((rule) => {
|
|
75
|
+
if (shouldRemoveRule({
|
|
76
|
+
selectorPattern,
|
|
77
|
+
exclude,
|
|
78
|
+
include,
|
|
79
|
+
selector: rule.selector,
|
|
80
|
+
})) {
|
|
81
|
+
rule.remove();
|
|
82
|
+
removedCount += 1;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
if (debug) {
|
|
86
|
+
console.log(`[postcss-plugin-remove-selector] removed ${removedCount} rules from:`, fileName);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
return plugin;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
exports.extractIconName = extractIconName;
|
|
94
|
+
exports.postCssPluginRemoveSelector = postCssPluginRemoveSelector;
|
|
95
|
+
exports.shouldHandleFile = shouldHandleFile;
|
|
96
|
+
exports.shouldRemoveRule = shouldRemoveRule;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TDesign UniApp 图标减包配置
|
|
3
|
+
*
|
|
4
|
+
* 使用说明:
|
|
5
|
+
* 1. 在 USED_ICONS 数组中配置项目中实际使用的图标名称
|
|
6
|
+
* 2. 插件只会处理 .t-icon-xxx:before 这类图标选择器
|
|
7
|
+
* 3. 其他基础样式(如 @font-face、.t-icon 等)会自动保留
|
|
8
|
+
*
|
|
9
|
+
* 如何查找项目中使用的图标:
|
|
10
|
+
* grep -rho 'icon="[^"]*"\|left-icon="[^"]*"\|name="[^"]*"' src --include="*.vue" | sort | uniq
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* TDesign UniApp 图标减包配置
|
|
14
|
+
*/
|
|
15
|
+
export declare const TDESIGN_ICON_REMOVE_SELECTOR: {
|
|
16
|
+
list: {
|
|
17
|
+
file: RegExp;
|
|
18
|
+
selectorPattern: RegExp;
|
|
19
|
+
include: string[];
|
|
20
|
+
exclude: never[];
|
|
21
|
+
}[];
|
|
22
|
+
};
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Root, Result } from 'postcss';
|
|
2
|
+
/**
|
|
3
|
+
* 单个文件匹配配置
|
|
4
|
+
*/
|
|
5
|
+
export interface FileConfig {
|
|
6
|
+
/** 文件匹配规则,可以是字符串或正则表达式 */
|
|
7
|
+
file: RegExp | string;
|
|
8
|
+
/** 需要保留的选择器列表(图标名称) */
|
|
9
|
+
include?: string[];
|
|
10
|
+
/** 需要移除的选择器列表(图标名称) */
|
|
11
|
+
exclude?: string[];
|
|
12
|
+
/** 选择器匹配模式,只处理匹配该模式的选择器 */
|
|
13
|
+
selectorPattern?: RegExp;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 插件配置项
|
|
17
|
+
*/
|
|
18
|
+
export interface Options {
|
|
19
|
+
/** 配置列表 */
|
|
20
|
+
list: FileConfig[];
|
|
21
|
+
/** 是否开启调试模式 */
|
|
22
|
+
debug?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 判断是否移除规则的参数
|
|
26
|
+
*/
|
|
27
|
+
export interface ShouldRemoveRuleOptions {
|
|
28
|
+
/** 选择器匹配模式 */
|
|
29
|
+
selectorPattern?: RegExp;
|
|
30
|
+
/** 需要保留的选择器列表 */
|
|
31
|
+
include: string[];
|
|
32
|
+
/** 需要移除的选择器列表 */
|
|
33
|
+
exclude: string[];
|
|
34
|
+
/** 当前选择器 */
|
|
35
|
+
selector: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* PostCSS 插件返回类型
|
|
39
|
+
*/
|
|
40
|
+
export interface PostCSSPlugin {
|
|
41
|
+
postcssPlugin: string;
|
|
42
|
+
Once(root: Root, helpers: {
|
|
43
|
+
result: Result;
|
|
44
|
+
}): void;
|
|
45
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@novlan/postcss-plugin-remove-selector",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"homepage": "https://novlan1.github.io/plugins/zh/postcss-plugin-remove-selector.html",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/novlan1/plugins/issues"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/novlan1/plugins"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "novlan1",
|
|
14
|
+
"main": "./lib/index",
|
|
15
|
+
"types": "./lib/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"lib/"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"postcss": "^8.0.0"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"registry": "https://registry.npmjs.org/"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "rm -rf lib && rollup -c",
|
|
29
|
+
"bump": "node ../../script/monorepo/version-simple $PWD",
|
|
30
|
+
"changelog": "node ../../script/monorepo/changelog $PWD",
|
|
31
|
+
"release": "node ../../script/monorepo/release $PWD"
|
|
32
|
+
}
|
|
33
|
+
}
|