index.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env node
  2. import fs from 'fs-extra';
  3. import { optimize } from 'svgo';
  4. import svgstore from 'svgstore';
  5. export default function svgSprite(datas) {
  6. // 可接收多组参数,处理多个文件夹内的文件
  7. // Multiple parameters can be accepted to process files in multiple folders
  8. const data = datas ? datas : [{ inFile: 'src/lib/icons', outFile: 'static/fonts', fileName: 'symbol', simple: true }];
  9. // 循环处理每一组参数
  10. // Loop through each set of parameters
  11. data.forEach(item => {
  12. const { inFile, outFile, fileName, simple = true } = item;
  13. handleFile(inFile, outFile, fileName, simple);
  14. });
  15. return {
  16. name: 'rollup-plugin-stdf-icon',
  17. };
  18. }
  19. // 处理一个文件夹内的文件
  20. // Process files in a folder
  21. function handleFile(inFile, outFile, fileName, simple = true) {
  22. // 如果 outFile 不存在, 则创建
  23. // If outFile does not exist, create it
  24. if (!fs.existsSync(outFile)) {
  25. fs.mkdirSync(outFile);
  26. }
  27. // 创建一个空的 svgstore
  28. // Create an empty svgstore
  29. const sprites = svgstore({ cleanDefs: true });
  30. // 循环 icons 目录下的所有 svg 文件
  31. // Loop through all svg files in the icons directory
  32. const svgs = fs.readdirSync(inFile);
  33. svgs.forEach(svg => {
  34. // 读取 svg 文件内容, 作为字符串
  35. // Read the svg file content as a string
  36. const code = fs.readFileSync(`${inFile}/${svg}`, 'utf8');
  37. // 使用 SVGO 进行优化
  38. // Use SVGO for optimization
  39. const result = optimize(code);
  40. // 删除 result 中的 p-id class 等属性
  41. // Delete p-id class and other attributes in result
  42. result.data = result.data.replace(/p-id="[^"]*"/g, '').replace(/class="[^"]*"/g, '');
  43. // 如果 result 中有 fill 属性,属性值不为 none, 则将 fill 属性值设置为 currentColor
  44. // If there is a fill attribute in result, the attribute value is not none, then set the fill attribute value to currentColor
  45. if (simple && result.data.indexOf('fill=') > -1 && result.data.indexOf('fill="none"') === -1) {
  46. result.data = result.data.replace(/fill="[^"]*"/g, 'fill="currentColor"');
  47. }
  48. // 如果 result 中有 stroke 属性,属性值不为 none, 则将 stroke 属性值设置为 currentColor
  49. // If there is a stroke attribute in result, the attribute value is not none, then set the stroke attribute value to currentColor
  50. if (simple && result.data.indexOf('stroke=') > -1 && result.data.indexOf('stroke="none"') === -1) {
  51. result.data = result.data.replace(/stroke="[^"]*"/g, 'stroke="currentColor"');
  52. }
  53. // 将优化后的 svg 添加到 sprites 中
  54. // Add the optimized svg to sprites
  55. sprites.add(svg.replace('.svg', ''), result.data, { copyAttrs: ['fill', 'stroke'] });
  56. });
  57. // 删除 sprites 的 <?xml...?> 标签和 <!DOCTYPE...> 标签 和 <defs/> 标签
  58. // Delete the <?xml...?> tag and <!DOCTYPE...> tag of sprites
  59. const spritesStr = sprites
  60. .toString()
  61. .replace(/<\?xml[^>]*>/g, '')
  62. .replace(/<!DOCTYPE[^>]*>/g, '')
  63. .replace(/<defs\/>/g, '');
  64. // 写入到指定的文件中
  65. // Write to the specified file
  66. fs.writeFileSync(outFile + '/' + fileName + '.svg', spritesStr);
  67. }