Skip to content

Commit

Permalink
[icon] Change rollup-plugin-stdf-icon
Browse files Browse the repository at this point in the history
  • Loading branch information
dufu1991 committed Aug 16, 2023
1 parent b1ce116 commit b6ab519
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions packages/rollup-plugin-stdf-icon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@ import fs from 'fs-extra';
import { optimize } from 'svgo';
import svgstore from 'svgstore';

export default function symbol(options = {}) {
const { inFile = 'src/assets/svgs', outFile = 'public/fonts', fileName = 'symbol', ...rest } = options;
export default function svgSprite(datas) {
// 可接收多组参数,处理多个文件夹内的文件
// Multiple parameters can be accepted to process files in multiple folders
const data = datas ? datas : [{ inFile: 'src/assets/svgs', outFile: 'public/fonts', fileName: 'symbol' }];

// 循环处理每一组参数
// Loop through each set of parameters
data.forEach(item => {
const { inFile, outFile, fileName } = item;
handleFile(inFile, outFile, fileName);
});

return {
name: 'rollup-plugin-stdf-icon',
};
}

// 处理一个文件夹内的文件
// Process files in a folder
function handleFile(inFile, outFile, fileName) {
// 如果 outFile 不存在, 则创建
// If outFile does not exist, create it
if (!fs.existsSync(outFile)) {
Expand All @@ -30,19 +47,26 @@ export default function symbol(options = {}) {
// Use SVGO for optimization
const result = optimize(code);

// 删除 result 中的 fill p-id width height class 等属性
// Delete fill p-id width height class and other attributes in result
result.data = result.data
.replace(/fill="[^"]*"/g, '')
.replace(/p-id="[^"]*"/g, '')
.replace(/width="[^"]*"/g, '')
.replace(/height="[^"]*"/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 (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 (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.toString({ inline: true });
sprites.add(svg.replace('.svg', ''), result.data);
// sprites.toString();
sprites.add(svg.replace('.svg', ''), result.data, { copyAttrs: ['fill', 'stroke'] });
});

// 删除 sprites 的 <?xml...?> 标签和 <!DOCTYPE...> 标签 和 <defs/> 标签
Expand All @@ -56,8 +80,4 @@ export default function symbol(options = {}) {
// 写入到指定的文件中
// Write to the specified file
fs.writeFileSync(outFile + '/' + fileName + '.svg', spritesStr);

return {
name: 'rollup-plugin-stdf-icon',
};
}

0 comments on commit b6ab519

Please sign in to comment.