svgo优化文件夹中的svg(node.js读写文件)

该博客介绍了如何通过Node.js结合svgo库,实现批量读取并优化SVG文件的过程,旨在提高SVG图像的加载速度和文件大小。文章详细讲解了相关步骤和技术要点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >





var FS = require('fs'),
    SVGO = require('svgo'),
    PATH = require('path'),
    filepath = PATH.resolve('路径');

svgo = new SVGO({
    plugins: [{
        cleanupAttrs: true, //来自换行符,尾随和重复空格的清理属性
    }, {
        inlineStyles: false,//将样式从<style>元素移动并合并到元素style属性
    }, {
        removeDoctype: true,
    }, {
        removeXMLProcInst: true,//删除XML处理指令
    }, {
        removeComments: true,
    }, {
        removeMetadata: true,
    }, {
        removeTitle: true,
    }, {
        removeDesc: true,
    }, {
        removeUselessDefs: true,//删除<defs>没有的元素id
    }, {
        removeEditorsNSData: true,//删除编辑器命名空间,元素和属性
    }, {
        removeEmptyAttrs: true,
    }, {
        removeHiddenElems: true,
    }, {
        removeEmptyText: true,
    }, {
        removeEmptyContainers: true,
    }, {
        removeViewBox: false,
    }, {
        cleanupEnableBackground: true,
    }, {
        convertStyleToAttrs: false,
    }, {
        convertColors: true,//色彩转换(从rgb()到#rrggbb,从#rrggbb到#rgb)
    }, {
        convertPathData: false,//将路径数据转换为相对或绝对(以较短者为准),将一个段转换为另一个段,修剪无用的分隔符,智能舍入等等
    }, {
        convertTransform: false,//将多个转换折叠为一个,将矩阵转换为短别名等等
    }, {
        removeUnknownsAndDefaults: true,
    }, {
        removeNonInheritableGroupAttrs: true,
    }, {
        removeUselessStrokeAndFill: false,
    }, {
        removeUnusedNS: true,//删除未使用的命名空间声明
    }, {
        cleanupIDs: true,//删除未使用的和缩小使用的ID
    }, {
        cleanupNumericValues: true,
    }, {
        moveElemsAttrsToGroup: true,
    }, {
        moveGroupAttrsToElems: false,
    }, {
        collapseGroups: true,
    }, {
        removeRasterImages: false,
    }, {
        mergePaths: false,
    }, {
        convertShapeToPath: false,
    }, {
        sortAttrs: true,
    }, {
        removeDimensions: false,
    }, {
        removeStyleElement: true,
    }, {
        removeAttrs: {
            attrs:
                ['data-name', 'class']
        },
    }]
});

fileDisplay(filepath);

function fileDisplay(filePath) {
    //根据文件路径读取文件,返回文件列表
    FS.readdir(filePath, function (err, files) {
        if (err) {
            console.warn(err)
        } else {
            //遍历读取到的文件列表
            files.forEach(function (filename) {
                //获取当前文件的绝对路径
                var filedir = PATH.join(filePath, filename);
                //根据文件路径获取文件信息,返回一个fs.Stats对象
                FS.stat(filedir, function (eror, stats) {
                    if (eror) {
                        console.warn('获取文件stats失败');
                    } else {
                        var isFile = stats.isFile();//是文件
                        var isDir = stats.isDirectory();//是文件夹
                        if (isFile) {
                            console.log(filedir);
                            // 读取文件内容
                            // var content = fs.readFileSync(filedir, 'utf-8');
                            // console.log(content);
                            FS.readFile(filedir, 'utf8', function (err, data) {
                                if (err) {
                                    throw err;
                                }
                                svgo.optimize(data, { path: filedir }).then(function (result) {
                                    // console.log(result.data);
                                    // 写入原文件
                                    FS.writeFile(filedir, result.data, function (err) {
                                        if (err) {
                                            throw err;
                                        }
                                    })
                                });
                            });
                        }
                        if (isDir) {
                            fileDisplay(filedir);//递归,如果是文件夹,就继续遍历该文件夹下面的文件
                        }
                    }
                })
            });
        }
    });
}








复制代码

转载于:https://juejin.im/post/5c467f83518825253661d6fe

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值