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

svg-sprite-loader

Package Overview
Dependencies
Maintainers
2
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.3.0 to 2.0.0-alpha.1

lib/config.js

252

lib/utils.js

@@ -1,27 +0,235 @@

var path = require('path');
var crypto = require('crypto');
/* eslint-disable import/no-extraneous-dependencies,no-trailing-spaces */
const webpackPkg = require('webpack/package.json');
const ruleMatcher = require('webpack/lib/ModuleFilenameHelpers').matchObject;
const defaults = require('./config');
exports.objectToAttrString = function (obj) {
if (Object.prototype.toString.call(obj) !== '[object Object]')
throw new TypeError('Must be an object');
const webpackMajorVersion = parseInt(webpackPkg.version.split('.')[0], 10);
const loaderDefaults = defaults.loader;
const loaderPath = require.resolve('./loader');
const stringifiedRegexp = /^'|".*'|"$/;
return Object.keys(obj)
.map(function (key) {
return key + '="' + obj[key] + '"';
}).join(' ');
};
/**
* If already stringified - return original content
* @param {Object|Array} content
* @return {string}
*/
function stringify(content) {
if (typeof content === 'string' && stringifiedRegexp.test(content)) {
return content;
}
return JSON.stringify(content, null, 2);
}
exports.isRasterImage = function (imagepath) {
var re = /\.(jpe?g|png|gif)$/i;
return re.test(imagepath);
};
/**
* @param {SpriteSymbol} symbol
* @return {string}
*/
function stringifySymbol(symbol) {
return stringify({
id: symbol.id,
use: symbol.useId,
viewBox: symbol.viewBox,
content: symbol.render()
});
}
exports.getPixelRatioFromFilename = function (filename) {
var ratioMatch = filename.substring(0, filename.lastIndexOf('.')).match(/\@([0-9])x$/);
var ratio = ratioMatch !== null ? parseInt(ratioMatch[1]) : 1;
return ratio;
};
/**
* @param {string} content
* @param {Object<string, string>} replacements
* @return {string}
*/
function replaceSpritePlaceholder(content, replacements) {
const regexp = defaults.SPRITE_PLACEHOLDER_PATTERN;
return content.replace(regexp, (match, p1) => {
return p1 ? replacements[p1] : match;
});
}
exports.generateHashFromPath = function (path_) {
return crypto.createHash('md5').update(path_).digest("hex");
};
/**
* @param {NormalModule|ExtractedModule} module
* @param {Object<string, string>} replacements
* @return {NormalModule|ExtractedModule}
*/
function replaceInModuleSource(module, replacements) {
const source = module._source;
if (typeof source === 'string') {
module._source = replaceSpritePlaceholder(source, replacements);
} else if (typeof source === 'object' && typeof source._value === 'string') {
source._value = replaceSpritePlaceholder(source._value, replacements);
}
return module;
}
/**
* @param {string} filepath
* @return {string}
*/
function generateSpritePlaceholder(filepath) {
return `[sprite-filename|${filepath}]`;
}
function generateImport(symbol, module, esModule = loaderDefaults.esModule) {
return esModule ?
`import ${symbol} from ${stringify(module)}` :
`var ${symbol} = require(${stringify(module)})`;
}
/**
* @param {string} content
* @param {boolean} [esModule=false]
* @return {string}
*/
function generateExport(content, esModule = defaults.esModule) {
return esModule ?
`export default ${content}` :
`module.exports = ${content}`;
}
/**
* @param {NormalModule} module
* @return {boolean}
*/
function isModuleShouldBeExtracted(module) {
const { request, issuer, loaders } = module;
const rule = loaders && loaders.find(l => l.loader === loaderPath);
const issuerResource = issuer && issuer.resource;
if (request && (!request.includes(loaderPath) || !rule)) {
return false;
}
return !!(
(issuer && defaults.EXTRACTABLE_MODULE_ISSUER_PATTERN.test(issuerResource)) ||
(rule && rule.options && rule.options.extract)
);
}
/**
* @param {Compiler} compiler
* @return {Rule[]}
*/
function getLoadersRules(compiler) {
const rulesProp = webpackMajorVersion === 1 ? 'loaders' : 'rules';
return compiler.options.module[rulesProp];
}
/**
* @param {string} request
* @param {Rule[]} rules Webpack loaders config
* @return {Rule[]}
*/
function getMatchedRules(request, rules) {
return rules.filter(rule => ruleMatcher(rule, request));
}
/**
* Always returns last matched rule
* @param {string} request
* @param {Rule[]} rules Webpack loaders config
* @return {Rule} Webpack rule
*/
function getMatchedRule(request, rules) {
const matched = getMatchedRules(request, rules);
return matched[matched.length - 1];
}
/**
* Find nearest module chunk (not sure that is reliable method, but who cares).
* @see http://stackoverflow.com/questions/43202761/how-to-determine-all-module-chunks-in-webpack
* @param {NormalModule} module
* @return {Chunk?}
*/
function getModuleChunk(module) {
const { chunks, issuer } = module;
if (Array.isArray(chunks) && chunks.length > 0) {
return chunks[chunks.length - 1];
} else if (issuer) {
return getModuleChunk(issuer);
}
return null;
}
/**
* @param {SpriteSymbol[]} symbols
* @param {Compilation} compilation
* @return {Object<string, Object<module: NormalModule[], spriteFilename: string>>[]}
*/
function aggregate(symbols, compilation) {
const { compiler } = compilation;
const modules = compilation.modules.filter(isModuleShouldBeExtracted);
const publicPath = compiler.options.output.publicPath || '';
const rules = getLoadersRules(compiler);
const defaultSpriteFilename = loaderDefaults.spriteFilename;
const chunkNamePlaceholder = '[chunkname]';
const aggregated = [];
return symbols.reduce((acc, symbol) => {
const resource = symbol.request.toString();
const module = modules.find(m => m.resource === resource);
const rule = getMatchedRule(symbol.request.file, rules);
// TODO get spriteFilename from rule
if (module && rule) {
let spriteFilename = (rule.options && rule.options.spriteFilename) ?
rule.options.spriteFilename :
defaultSpriteFilename;
if (spriteFilename.includes(chunkNamePlaceholder)) {
const chunk = getModuleChunk(module);
spriteFilename = spriteFilename.replace(chunkNamePlaceholder, chunk.name);
}
acc.push({
symbol,
module,
resource,
spriteFilename,
url: `${publicPath}${spriteFilename}#${symbol.id}`,
useUrl: `${publicPath}${spriteFilename}#${symbol.useId}`
});
}
return acc;
}, aggregated);
}
/**
* @param {Object} aggregated {@see aggregate}
* @return {Object<string, SpriteSymbol[]>}
*/
function groupSymbolsBySprites(aggregated) {
return aggregated.map(item => item.spriteFilename)
.filter((value, index, self) => self.indexOf(value) === index)
.reduce((acc, spriteFilename) => {
acc[spriteFilename] = aggregated
.filter(item => item.spriteFilename === spriteFilename)
.map(item => item.symbol);
return acc;
}, {});
}
exports.webpackMajorVersion = webpackMajorVersion;
exports.stringify = stringify;
exports.stringifySymbol = stringifySymbol;
exports.replaceSpritePlaceholder = replaceSpritePlaceholder;
exports.replaceInModuleSource = replaceInModuleSource;
exports.generateSpritePlaceholder = generateSpritePlaceholder;
exports.generateImport = generateImport;
exports.generateExport = generateExport;
exports.isModuleShouldBeExtracted = isModuleShouldBeExtracted;
exports.getModuleChunk = getModuleChunk;
exports.getLoadersRules = getLoadersRules;
exports.getMatchedRules = getMatchedRules;
exports.getMatchedRule = getMatchedRule;
exports.aggregate = aggregate;
exports.groupSymbolsBySprites = groupSymbolsBySprites;

71

package.json
{
"name": "svg-sprite-loader",
"version": "0.3.0",
"description": "SVG sprite webpack loader",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "2.0.0-alpha.1",
"description": "SVG sprite loader for Webpack",
"homepage": "https://github.com/kisenka/webpack-svg-sprite-loader#readme",
"main": "lib/loader.js",
"files": [
"lib/**",
"runtime/**",
"plugin.js",
"README.md",
"LICENSE"
],
"repository": {

@@ -18,9 +23,2 @@ "type": "git",

],
"files": [
"lib/",
"index.js",
"config.js",
"README.md",
"LICENSE"
],
"author": "kisenka",

@@ -31,11 +29,46 @@ "license": "MIT",

},
"homepage": "https://github.com/kisenka/webpack-svg-sprite-loader#readme",
"dependencies": {
"cheerio": "^0.22.0",
"extend": "^3.0.0",
"image-size": "^0.3.5",
"loader-utils": "^0.2.11",
"bluebird": "^3.5.0",
"deepmerge": "^1.3.2",
"loader-utils": "^1.1.0",
"postcss": "^5.2.10",
"sniffr": "1.1.4"
"sniffr": "1.1.4",
"svg-baker": "^1.2.0",
"svg-baker-runtime": "^1.2.1",
"url-slug": "^2.0.0"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^6.4.1",
"babel-plugin-async-to-promises": "^1.0.5",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.0",
"chai": "^3.5.0",
"css-loader": "^0.28.0",
"eslint": "^3.18.0",
"eslint-config-airbnb-base": "^11.1.2",
"eslint-plugin-import": "^2.2.0",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.11.1",
"html-loader": "^0.4.5",
"istanbul": "^0.4.5",
"minimist": "^1.2.0",
"mocha": "^3.2.0",
"rollup": "^0.41.6",
"rollup-plugin-buble": "^0.15.0",
"rollup-plugin-commonjs": "^8.0.2",
"rollup-plugin-node-resolve": "^3.0.0",
"svgo": "^0.7.2",
"svgo-loader": "^1.2.1",
"wallaby-webpack": "0.0.38",
"webpack": "^2.3.3",
"webpack-toolkit": "^0.9.0"
},
"scripts": {
"build:runtime": "node scripts/build-runtime.js",
"coverage": "istanbul cover --report html --report lcov _mocha test/*.test.js",
"lint": "eslint --quiet .",
"release": "standard-version",
"test": "mocha test/*.test.js"
}
}

Sorry, the diff of this file is not supported yet

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