Skip to content

cenfun/svg-minifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SVG Minifier

A tool to minify svg files

Install

npm i svg-minifier

Usage

const svgMinifier = require('svg-minifier');
const metadata = svgMinifier({
    id: 'my-icons',
    dirs: [path.resolve(__dirname, 'icons')],
    exclude: [],
    excludeSubDir: false,

    outputDir: path.resolve(__dirname, 'dist'),
    outputMetadata: true,
    outputRuntime: true,

    silent: false,
    logDuplicates: true,

    onSVGName: function(name, item) {

        //filter
        // if (name === 'xxx') {
        //     return;
        // }

        return this.onSVGNameDefault(name, item);
    },
    
    //original svg file content
    onSVGContent: function(content, item) {
        //svg content handler
        // return newContent;
    },

    //cheerio DOM (jQuery API)
    onSVGDocument: function($svg, item, $) {
        //svg DOM handler
    },

    //cheerio DOM after optimized
    onSVGOptimized: function($svg, item, $) {
        //svg DOM handler

        //filter
        // if (item.name === 'xxx') {
        //     return false;
        // }
    },

    //optimize failed
    onSVGError: function(result, item, $) {
        return this.onSVGErrorDefault(result, item, $);
    },

    onFinish: function(options, metadata) {

    },

    //additional metadata
    metadata: {
        key: 'value'
    },

    //svgo config (SVG Optimizer)
    svgo: {}

});

console.log(metadata);

see options

With type for svg name

svgMinifier({
    id: 'my-icons',
    dirs: ['icons/solid', {
        //name = [svg-name]-[type-name]
        outline: 'icons/outline'
    }]
});

Test

npm run test

see test

Link

Changelog

  • 2.0.0

    • update lz-utils
  • 1.0.11

    • added filter for onSVGOptimized