This webpack plugin generates a collection of SVGs in JSON format from all .svg
files in a directory. This strips out the svg and title tags for manually addition when loading the SVG data. In addition to the SVG data, it also returns each SVG's title and viewbox. This code is heavily based on the SVG Spritemap Webpack Plugin by Freshheads.
NPM: svg-to-json-webpack-plugin
npm install svg-to-json-webpack-plugin --save-dev
// webpack.config.js
var SvgToJsonPlugin = require('svg-to-json-webpack-plugin');
module.exports = {
// ...
plugins: [
new SvgToJsonPlugin({
// Optional options object
})
]
}
Option | Default | Description |
---|---|---|
src |
'**/*.svg' |
glob used for finding the SVGs that should be in the spritemap |
glob |
{} |
Options object for glob |
prefix |
'sprite-' |
Prefix added to sprite identifier in the spritemap |
filename |
'spritemap.json' |
Name for the generated file (located at the webpack output.path ), [hash] and [contenthash] are supported |
chunk |
'spritemap' |
Name of the generated chunk |
deleteChunk |
true |
Deletes the chunked file chunk after packing is complete |