瀏覽代碼

[icon]Format code

dufu1991 3 年之前
父節點
當前提交
b37c704c36

+ 13 - 14
packages/rollup-plugin-stdf-icon/README.md

@@ -1,4 +1,3 @@
-
 [简体中文](https://github.com/dufu1991/stdf/blob/main/packages/rollup-plugin-stdf-icon/README_CN.md)
 
 # Introduction
@@ -30,14 +29,14 @@ Configure the plugin in vite.config.js or vite.config.ts:
 import svgSprite from 'rollup-plugin-stdf-icon';
 
 export default defineConfig({
-    // ...
-    plugins:[
-        // ...
-        svgSprite(),
-        // ...
-    ]
-    // ...
-})
+	// ...
+	plugins: [
+		// ...
+		svgSprite(),
+		// ...
+	],
+	// ...
+});
 ```
 
 During development, simply place the SVG files you need in the specified input folder (default: `src/lib/icons`). The plugin will automatically combine them into a single SVG symbol file and output it to the specified output folder (default: `static/fonts`). After building with Vite, the files in the public folder will be copied to the dist folder. Therefore, after building, the combined SVG symbol file will be located in the dist/fonts folder. Please refer to the [Vite Asset Handling](https://vitejs.dev/guide/assets.html#the-public-directory) documentation.
@@ -65,12 +64,12 @@ In general, the use of symbols is to combine a series of small, single-color SVG
 
 # Configuration
 
-| Parameter | Default           | Description                                                                                                   |
-| --------- | ----------------- | ------------------------------------------------------------------------------------------------------------- |
+| Parameter | Default         | Description                                                                                                   |
+| --------- | --------------- | ------------------------------------------------------------------------------------------------------------- |
 | inFile    | 'src/lib/icons' | The folder where all the SVG files to be merged are located.                                                  |
-| outFile   | 'static/fonts'    | The output path for the merged SVG symbol file.                                                               |
-| fileName  | 'symbol'          | The filename of the merged SVG symbol file.                                                                   |
-| simple    | true              | Whether to use the simple mode, the simple mode will remove the color attributes of the SVG files themselves. |
+| outFile   | 'static/fonts'  | The output path for the merged SVG symbol file.                                                               |
+| fileName  | 'symbol'        | The filename of the merged SVG symbol file.                                                                   |
+| simple    | true            | Whether to use the simple mode, the simple mode will remove the color attributes of the SVG files themselves. |
 
 # License
 

+ 10 - 9
packages/rollup-plugin-stdf-icon/README_CN.md

@@ -29,14 +29,14 @@ npm i rollup-plugin-stdf-icon -D
 import svgSprite from 'rollup-plugin-stdf-icon';
 
 export default defineConfig({
-    // ...
-    plugins:[
-        // ...
-        svgSprite(),
-        // ...
-    ]
-    // ...
-})
+	// ...
+	plugins: [
+		// ...
+		svgSprite(),
+		// ...
+	],
+	// ...
+});
 ```
 
 开发过程中只需要将用到的 svg 文件放到指定的入口文件夹(默认 `src/lib/icons`),插件会自动将其合并为一个 SVG symbol 文件,输出到指定的输出文件夹(默认 `static/fonts`)。Vite 构建之后 public 文件夹下的文件会被复制到 dist 文件夹下,所以在构建之后,合并的 SVG symbol 文件会出现在 dist/fonts 文件夹下。请参考 [Vite 配置](https://cn.vitejs.dev/guide/assets.html#the-public-directory)。
@@ -55,6 +55,7 @@ export default defineConfig({
     ]),
 // ...
 ```
+
 此配置会合并两个 symbol。**此时使用 STDF 的 Icon 组件时,请配合修改 path 为对应的 `fonts/symbol.svg` 或 `fonts/icon.svg`**
 
 注意:**传入参数为数组**,表示多个文件夹的配置。**每个文件夹的配置为一个对象**,对象中的 inFile 为将要被合并的 SVG 文件所在的文件夹,outFile 为合并后的 SVG symbol 文件的输出路径,fileName 为合并后的 SVG symbol 文件的文件名。请注意路径与文件名的正确性与冲突。
@@ -72,4 +73,4 @@ export default defineConfig({
 
 # 许可证
 
-本项目基于 [MIT 许可证](https://github.com/dufu1991/stdf/blob/main/LICENSE)。请随意享受并贡献给这个开源项目。
+本项目基于 [MIT 许可证](https://github.com/dufu1991/stdf/blob/main/LICENSE)。请随意享受并贡献给这个开源项目。

+ 43 - 43
packages/rollup-plugin-stdf-icon/package.json

@@ -1,45 +1,45 @@
 {
-    "name": "rollup-plugin-stdf-icon",
-    "version": "0.0.7",
-    "description": "A Rollup plugin that combines a series of svg into one symbol for use in the STDF project",
-    "main": "dist/index.js",
-    "scripts": {
-        "build": "rollup -c",
-        "test": "echo \"Error: no test specified\" && exit 1"
-    },
-    "type": "module",
-    "author": "dufu1991",
-    "license": "MIT",
-    "homepage": "https://github.com/dufu1991/stdf/blob/main/packages/rollup-plugin-stdf-icon/README.md",
-    "repository": {
-        "type": "git",
-        "url": "https://github.com/dufu1991/stdf/tree/main/packages/rollup-plugin-stdf-icon"
-    },
-    "bugs": {
-        "url": "https://github.com/dufu1991/stdf/issues?q=is:issue+is:open+rollup-plugin-stdf-icon"
-    },
-    "files": [
-        "dist/**"
-    ],
-    "engines": {
-        "node": ">=14.14"
-    },
-    "dependencies": {
-        "fs-extra": "^11.1.1",
-        "svgo": "^3.0.2",
-        "svgstore": "^3.0.1"
-    },
-    "devDependencies": {
-        "@rollup/plugin-terser": "^0.4.3",
-        "rollup": "^3.27.0"
-    },
-    "keywords": [
-        "stdf",
-        "rollup",
-        "symbol",
-        "sprite",
-        "vite",
-        "svg",
-        "icon"
-    ]
+	"name": "rollup-plugin-stdf-icon",
+	"version": "0.0.8",
+	"description": "A Rollup plugin that combines a series of svg into one symbol for use in the STDF project",
+	"main": "dist/index.js",
+	"scripts": {
+		"build": "rollup -c",
+		"test": "echo \"Error: no test specified\" && exit 1"
+	},
+	"type": "module",
+	"author": "dufu1991",
+	"license": "MIT",
+	"homepage": "https://github.com/dufu1991/stdf/blob/main/packages/rollup-plugin-stdf-icon/README.md",
+	"repository": {
+		"type": "git",
+		"url": "https://github.com/dufu1991/stdf/tree/main/packages/rollup-plugin-stdf-icon"
+	},
+	"bugs": {
+		"url": "https://github.com/dufu1991/stdf/issues?q=is:issue+is:open+rollup-plugin-stdf-icon"
+	},
+	"files": [
+		"dist/**"
+	],
+	"engines": {
+		"node": ">=14.14"
+	},
+	"dependencies": {
+		"fs-extra": "^11.1.1",
+		"svgo": "^3.0.2",
+		"svgstore": "^3.0.1"
+	},
+	"devDependencies": {
+		"@rollup/plugin-terser": "^0.4.3",
+		"rollup": "^3.27.0"
+	},
+	"keywords": [
+		"stdf",
+		"rollup",
+		"symbol",
+		"sprite",
+		"vite",
+		"svg",
+		"icon"
+	]
 }

+ 4 - 4
packages/rollup-plugin-stdf-icon/rollup.config.js

@@ -1,8 +1,8 @@
 import terser from '@rollup/plugin-terser';
 
 export default {
-    input: 'src/index.js',
-    output: [{ file: 'dist/index.js', format: 'es' }],
-    external: ['fs-extra', 'svgo', 'svgstore'],
-    plugins: [terser({ keep_fnames: true })],
+	input: 'src/index.js',
+	output: [{ file: 'dist/index.js', format: 'es' }],
+	external: ['fs-extra', 'svgo', 'svgstore'],
+	plugins: [terser({ keep_fnames: true })],
 };

+ 57 - 57
packages/rollup-plugin-stdf-icon/src/index.js

@@ -5,78 +5,78 @@ import { optimize } from 'svgo';
 import svgstore from 'svgstore';
 
 export default function svgSprite(datas) {
-    // 可接收多组参数,处理多个文件夹内的文件
-    // Multiple parameters can be accepted to process files in multiple folders
-    const data = datas ? datas : [{ inFile: 'src/lib/icons', outFile: 'static/fonts', fileName: 'symbol', simple: true }];
+	// 可接收多组参数,处理多个文件夹内的文件
+	// Multiple parameters can be accepted to process files in multiple folders
+	const data = datas ? datas : [{ inFile: 'src/lib/icons', outFile: 'static/fonts', fileName: 'symbol', simple: true }];
 
-    // 循环处理每一组参数
-    // Loop through each set of parameters
-    data.forEach(item => {
-        const { inFile, outFile, fileName, simple = true } = item;
-        handleFile(inFile, outFile, fileName, simple);
-    });
+	// 循环处理每一组参数
+	// Loop through each set of parameters
+	data.forEach(item => {
+		const { inFile, outFile, fileName, simple = true } = item;
+		handleFile(inFile, outFile, fileName, simple);
+	});
 
-    return {
-        name: 'rollup-plugin-stdf-icon',
-    };
+	return {
+		name: 'rollup-plugin-stdf-icon',
+	};
 }
 
 // 处理一个文件夹内的文件
 // Process files in a folder
 function handleFile(inFile, outFile, fileName, simple = true) {
-    // 如果 outFile 不存在, 则创建
-    // If outFile does not exist, create it
-    if (!fs.existsSync(outFile)) {
-        fs.mkdirSync(outFile);
-    }
+	// 如果 outFile 不存在, 则创建
+	// If outFile does not exist, create it
+	if (!fs.existsSync(outFile)) {
+		fs.mkdirSync(outFile);
+	}
 
-    // 创建一个空的 svgstore
-    // Create an empty svgstore
-    const sprites = svgstore({ cleanDefs: true });
+	// 创建一个空的 svgstore
+	// Create an empty svgstore
+	const sprites = svgstore({ cleanDefs: true });
 
-    // 循环 icons 目录下的所有 svg 文件
-    // Loop through all svg files in the icons directory
-    const svgs = fs.readdirSync(inFile);
+	// 循环 icons 目录下的所有 svg 文件
+	// Loop through all svg files in the icons directory
+	const svgs = fs.readdirSync(inFile);
 
-    svgs.forEach(svg => {
-        // 读取 svg 文件内容, 作为字符串
-        // Read the svg file content as a string
-        const code = fs.readFileSync(`${inFile}/${svg}`, 'utf8');
+	svgs.forEach(svg => {
+		// 读取 svg 文件内容, 作为字符串
+		// Read the svg file content as a string
+		const code = fs.readFileSync(`${inFile}/${svg}`, 'utf8');
 
-        // 使用 SVGO 进行优化
-        // Use SVGO for optimization
-        const result = optimize(code);
+		// 使用 SVGO 进行优化
+		// Use SVGO for optimization
+		const result = optimize(code);
 
-        // 删除 result 中的 p-id class 等属性
-        // Delete p-id class and other attributes in result
-        result.data = result.data.replace(/p-id="[^"]*"/g, '').replace(/class="[^"]*"/g, '');
+		// 删除 result 中的 p-id class 等属性
+		// Delete p-id class and other attributes in result
+		result.data = result.data.replace(/p-id="[^"]*"/g, '').replace(/class="[^"]*"/g, '');
 
-        // 如果 result 中有 fill 属性,属性值不为 none, 则将 fill 属性值设置为 currentColor
-        // If there is a fill attribute in result, the attribute value is not none, then set the fill attribute value to currentColor
-        if (simple && result.data.indexOf('fill=') > -1 && result.data.indexOf('fill="none"') === -1) {
-            result.data = result.data.replace(/fill="[^"]*"/g, 'fill="currentColor"');
-        }
+		// 如果 result 中有 fill 属性,属性值不为 none, 则将 fill 属性值设置为 currentColor
+		// If there is a fill attribute in result, the attribute value is not none, then set the fill attribute value to currentColor
+		if (simple && result.data.indexOf('fill=') > -1 && result.data.indexOf('fill="none"') === -1) {
+			result.data = result.data.replace(/fill="[^"]*"/g, 'fill="currentColor"');
+		}
 
-        // 如果 result 中有 stroke 属性,属性值不为 none, 则将 stroke 属性值设置为 currentColor
-        // If there is a stroke attribute in result, the attribute value is not none, then set the stroke attribute value to currentColor
-        if (simple && result.data.indexOf('stroke=') > -1 && result.data.indexOf('stroke="none"') === -1) {
-            result.data = result.data.replace(/stroke="[^"]*"/g, 'stroke="currentColor"');
-        }
+		// 如果 result 中有 stroke 属性,属性值不为 none, 则将 stroke 属性值设置为 currentColor
+		// If there is a stroke attribute in result, the attribute value is not none, then set the stroke attribute value to currentColor
+		if (simple && result.data.indexOf('stroke=') > -1 && result.data.indexOf('stroke="none"') === -1) {
+			result.data = result.data.replace(/stroke="[^"]*"/g, 'stroke="currentColor"');
+		}
 
-        // 将优化后的 svg 添加到 sprites 中
-        // Add the optimized svg to sprites
-        sprites.add(svg.replace('.svg', ''), result.data, { copyAttrs: ['fill', 'stroke'] });
-    });
+		// 将优化后的 svg 添加到 sprites 中
+		// Add the optimized svg to sprites
+		sprites.add(svg.replace('.svg', ''), result.data, { copyAttrs: ['fill', 'stroke'] });
+	});
 
-    // 删除 sprites 的 <?xml...?> 标签和 <!DOCTYPE...> 标签 和 <defs/> 标签
-    // Delete the <?xml...?> tag and <!DOCTYPE...> tag of sprites
-    const spritesStr = sprites
-        .toString()
-        .replace(/<\?xml[^>]*>/g, '')
-        .replace(/<!DOCTYPE[^>]*>/g, '')
-        .replace(/<defs\/>/g, '');
+	// 删除 sprites 的 <?xml...?> 标签和 <!DOCTYPE...> 标签 和 <defs/> 标签
+	// Delete the <?xml...?> tag and <!DOCTYPE...> tag of sprites
+	const spritesStr = sprites
+		.toString()
+		.replace(/<\?xml[^>]*>/g, '')
+		.replace(/<!DOCTYPE[^>]*>/g, '')
+		.replace(/<defs\/>/g, '');
 
-    // 写入到指定的文件中
-    // Write to the specified file
-    fs.writeFileSync(outFile + '/' + fileName + '.svg', spritesStr);
+	// 写入到指定的文件中
+	// Write to the specified file
+	fs.writeFileSync(outFile + '/' + fileName + '.svg', spritesStr);
 }