Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

svg-sprite-loader

Package Overview
Dependencies
Maintainers
1
Versions
131
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svg-sprite-loader - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

lib/processings/index.js

74

index.js
var path = require('path');
var loaderUtils = require('loader-utils');
var extend = require('extend');
var convertToSymbol = require('./lib/convertToSymbol');
var optimize = require('./lib/optimize');
var SVGDoc = require('./lib/svg-document');
var procs = require('./lib/processings');
var utils = require('./lib/utils');
var defaults = {
prefix: null,
optimize: true
};
/**
* Default loader config
*
* @typedef {{
name: String,
svgo: Boolean|SVGOConfig,
spriteModule: String,
* }} loaderConfig
*/
var defaultConfig = {
// Sprite image naming pattern. Supported patterns: `[name]`, `[pathhash]`.
name: '[name]',
var getIdFromFilepath = function(filepath) {
var basename = path.basename(filepath);
var nameParts = basename.split('.');
nameParts.splice(-1);
return nameParts.join('.');
// Use SVGO for optimization. Boolean or SVGO config supported.
// See https://github.com/svg/svgo/blob/master/docs/how-it-works/en.md#1-config.
svgo: true,
// Sprite module name. You can define your own sprite module (based on `./lib/web/sprite` or not).
spriteModule: path.resolve(__dirname, 'lib/web/global-sprite')
};

@@ -21,14 +31,40 @@

this.cacheable && this.cacheable();
var options = extend({}, defaults, loaderUtils.parseQuery(this.query));
var filepath = this.resourcePath;
this.addDependency(filepath);
var symbolId = (options.prefix ? options.prefix + '_' : '') + getIdFromFilepath(filepath);
var query = loaderUtils.parseQuery(this.query);
/** @type {loaderConfig} */
var config = extend({}, defaultConfig, query);
var resourcePath = this.resourcePath;
var basename = path.basename(resourcePath);
var isRasterImage = utils.isRasterImage(resourcePath);
if (options.optimize)
content = optimize(content);
this.addDependency(resourcePath);
content = convertToSymbol(content, symbolId);
if (isRasterImage)
content = procs.rasterImageToSVG(resourcePath);
return 'module.exports = ' + JSON.stringify(content);
if (config.svgo)
content = procs.svgo(content, typeof config.svgo === 'boolean' ? null : config.svgo);
var doc = new SVGDoc(content);
var id = utils.generateIdFromFilepath(resourcePath, config.name);
procs.prefixize(doc, id + '_');
// Check raster image pixel ratio based on file name (e.g. image@2x.png)
if (isRasterImage) {
var pixelRatio = utils.getPixelRatioFromFilename(basename);
var scale = Number((1 / pixelRatio).toFixed(1));
if (scale !== 1)
doc.$('image').attr('transform', 'scale('+ scale +')');
}
doc.$svg.attr('id', id);
content = doc.toString(SVGDoc.OUTPUT_FORMAT.SYMBOL);
return [
'var sprite = require("' + config.spriteModule.replace(/\\/g, "/") + '")',
'var image = ' + JSON.stringify(content),
'sprite.add(image)',
'module.exports = ' + JSON.stringify(id) + ';'
].join(';\n');
};
{
"name": "svg-sprite-loader",
"version": "0.0.2",
"version": "0.0.3",
"description": "SVG sprite webpack loader",

@@ -20,3 +20,2 @@ "main": "index.js",

"lib/",
"web/",
"index.js",

@@ -35,2 +34,3 @@ "README.md",

"extend": "^3.0.0",
"image-size": "^0.3.5",
"loader-utils": "^0.2.11",

@@ -37,0 +37,0 @@ "svgo": "^0.5.5"

# Webpack SVG sprite loader
```js
// Create sprite instance
var SvgSprite = require('node_modules/svg-sprite-loader/web/svg-sprite');
var sprite = new SvgSprite();
// Add single image to the sprite
sprite.add(require('svg-sprite!images/logos/logo.svg'));
require('svg-sprite!images/logos/logo.svg');
// ...or bunch of them
sprite.add(require.context('svg-sprite!images/logos', false, /\.svg$/));
var files = require.context('svg-sprite!images/logos', false, /\.svg$/);
files.keys().forEach(files);
// Render the sprite
sprite.render(document.body);
// Use it somewhere on the page
<svg><use xlink:href="#logo"></use></svg>
// PROFIT!
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc