merge removeUnusedStyles plugin with minifyStyles
@ -1,45 +1,147 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
exports.type = 'perItem';
|
exports.type = 'full';
|
||||||
|
|
||||||
exports.active = true;
|
exports.active = true;
|
||||||
|
|
||||||
exports.params = {
|
exports.description = 'minifies styles and removes unused styles based on usage data';
|
||||||
svgo: {}
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.description = 'minifies existing styles in svg';
|
exports.params = {
|
||||||
|
// ... CSSO options goes here
|
||||||
|
svgo: {},
|
||||||
|
|
||||||
|
// additional
|
||||||
|
usage: {
|
||||||
|
ids: true,
|
||||||
|
classes: true,
|
||||||
|
tags: true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var csso = require('csso');
|
var csso = require('csso');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minifies styles (<style> element + style attribute) using svgo
|
* Minifies styles (<style> element + style attribute) using CSSO
|
||||||
*
|
|
||||||
* @param {Object} item current iteration item
|
|
||||||
* @return {Boolean} if false, item will be filtered out
|
|
||||||
*
|
*
|
||||||
* @author strarsis <strarsis@gmail.com>
|
* @author strarsis <strarsis@gmail.com>
|
||||||
*/
|
*/
|
||||||
exports.fn = function(item, svgoOptions) {
|
exports.fn = function(ast, options) {
|
||||||
|
options = options || {};
|
||||||
|
|
||||||
if(item.elem) {
|
var minifyOptionsForStylesheet = cloneObject(options);
|
||||||
if(item.isElem('style') && !item.isEmpty()) {
|
var minifyOptionsForAttribute = cloneObject(options);
|
||||||
var styleCss = item.content[0].text || item.content[0].cdata || [],
|
var elems = findStyleElems(ast);
|
||||||
DATA = styleCss.indexOf('>') >= 0 || styleCss.indexOf('<') >= 0 ? 'cdata' : 'text';
|
|
||||||
if(styleCss.length > 0) {
|
|
||||||
var styleCssMinified = csso.minify(styleCss, svgoOptions);
|
|
||||||
item.content[0][DATA] = styleCssMinified.css;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(item.hasAttr('style')) {
|
minifyOptionsForStylesheet.usage = collectUsageData(ast, options);
|
||||||
var itemCss = item.attr('style').value;
|
minifyOptionsForAttribute.usage = null;
|
||||||
if(itemCss.length > 0) {
|
|
||||||
var itemCssMinified = csso.minifyBlock(itemCss, svgoOptions);
|
elems.forEach(function(elem) {
|
||||||
item.attr('style').value = itemCssMinified.css;
|
if (elem.isElem('style')) {
|
||||||
}
|
// <style> element
|
||||||
}
|
var styleCss = elem.content[0].text || elem.content[0].cdata || [];
|
||||||
|
var DATA = styleCss.indexOf('>') >= 0 || styleCss.indexOf('<') >= 0 ? 'cdata' : 'text';
|
||||||
|
|
||||||
|
elem.content[0][DATA] = csso.minify(styleCss, minifyOptionsForStylesheet).css;
|
||||||
|
} else {
|
||||||
|
// style attribute
|
||||||
|
var elemStyle = elem.attr('style').value;
|
||||||
|
|
||||||
|
elem.attr('style').value = csso.minifyBlock(elemStyle, minifyOptionsForAttribute).css;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return ast;
|
||||||
|
};
|
||||||
|
|
||||||
|
function cloneObject(obj) {
|
||||||
|
var result = {};
|
||||||
|
|
||||||
|
for (var key in obj) {
|
||||||
|
result[key] = obj[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
return item;
|
return result;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
function findStyleElems(ast) {
|
||||||
|
|
||||||
|
function walk(items, styles) {
|
||||||
|
for (var i = 0; i < items.content.length; i++) {
|
||||||
|
var item = items.content[i];
|
||||||
|
|
||||||
|
// go deeper
|
||||||
|
if (item.content) {
|
||||||
|
walk(item, styles);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.isElem('style') && !item.isEmpty()) {
|
||||||
|
styles.push(item);
|
||||||
|
} else if (item.isElem() && item.hasAttr('style')) {
|
||||||
|
styles.push(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return styles;
|
||||||
|
}
|
||||||
|
|
||||||
|
return walk(ast, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
function shouldFilter(options, name) {
|
||||||
|
if ('usage' in options === false) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.usage && name in options.usage === false) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Boolean(options.usage && options.usage[name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function collectUsageData(ast, options) {
|
||||||
|
|
||||||
|
function walk(items, usageData) {
|
||||||
|
for (var i = 0; i < items.content.length; i++) {
|
||||||
|
var item = items.content[i];
|
||||||
|
|
||||||
|
// go deeper
|
||||||
|
if (item.content) {
|
||||||
|
walk(item, usageData);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.isElem()) {
|
||||||
|
usageData.tags[item.elem] = true;
|
||||||
|
|
||||||
|
if (item.hasAttr('id')) {
|
||||||
|
usageData.ids[item.attr('id').value] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.hasAttr('class')) {
|
||||||
|
item.attr('class').value.replace(/^\s+|\s+$/g, '').split(/\s+/).forEach(function(className) {
|
||||||
|
usageData.classes[className] = true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return usageData;
|
||||||
|
}
|
||||||
|
|
||||||
|
var hasData = false;
|
||||||
|
var usageData = walk(ast, {
|
||||||
|
ids: Object.create(null),
|
||||||
|
classes: Object.create(null),
|
||||||
|
tags: Object.create(null)
|
||||||
|
});
|
||||||
|
|
||||||
|
for (var key in usageData) {
|
||||||
|
usageData[key] = shouldFilter(options, key) && Object.keys(usageData[key]);
|
||||||
|
|
||||||
|
if (usageData[key]) {
|
||||||
|
hasData = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasData ? usageData : null;
|
||||||
|
}
|
||||||
|
@ -1,121 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
exports.type = 'full';
|
|
||||||
|
|
||||||
exports.active = true;
|
|
||||||
|
|
||||||
exports.description = 'removes unused styles from <style> based on usage data';
|
|
||||||
|
|
||||||
exports.params = {
|
|
||||||
removeUnusedStyles: {
|
|
||||||
ids: true,
|
|
||||||
classes: true,
|
|
||||||
tags: true
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var csso = require('csso');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove unused styles from <style> based on usage data
|
|
||||||
*
|
|
||||||
* @author Roman Dvornov
|
|
||||||
*/
|
|
||||||
exports.fn = function(ast, params) {
|
|
||||||
|
|
||||||
var usageData = collectUsageData(ast);
|
|
||||||
var styles = findStyleElems(ast);
|
|
||||||
|
|
||||||
for (var key in usageData) {
|
|
||||||
if (!shouldFilter(params, key)) {
|
|
||||||
usageData[key] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
styles.forEach(function(style) {
|
|
||||||
var styleCss = style.content[0].text || style.content[0].cdata || [];
|
|
||||||
var DATA = styleCss.indexOf('>') >= 0 || styleCss.indexOf('<') >= 0 ? 'cdata' : 'text';
|
|
||||||
|
|
||||||
style.content[0][DATA] = csso.minify(styleCss, {
|
|
||||||
usage: usageData
|
|
||||||
}).css;
|
|
||||||
});
|
|
||||||
|
|
||||||
return ast;
|
|
||||||
};
|
|
||||||
|
|
||||||
function shouldFilter(params, name) {
|
|
||||||
if (!params || 'removeUnusedStyles' in params === false) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (params.removeUnusedStyles && name in params.removeUnusedStyles === false) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Boolean(params.removeUnusedStyles && params.removeUnusedStyles[name]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function findStyleElems(ast) {
|
|
||||||
|
|
||||||
function walk(items, styles) {
|
|
||||||
for (var i = 0; i < items.content.length; i++) {
|
|
||||||
var item = items.content[i];
|
|
||||||
|
|
||||||
// go deeper
|
|
||||||
if (item.content) {
|
|
||||||
walk(item, styles);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.isElem('style') && !item.isEmpty()) {
|
|
||||||
styles.push(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return styles;
|
|
||||||
}
|
|
||||||
|
|
||||||
return walk(ast, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
function collectUsageData(ast) {
|
|
||||||
|
|
||||||
function walk(items, usageData) {
|
|
||||||
for (var i = 0; i < items.content.length; i++) {
|
|
||||||
var item = items.content[i];
|
|
||||||
|
|
||||||
// go deeper
|
|
||||||
if (item.content) {
|
|
||||||
walk(item, usageData);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.isElem()) {
|
|
||||||
usageData.tags[item.elem] = true;
|
|
||||||
|
|
||||||
if (item.hasAttr('id')) {
|
|
||||||
usageData.ids[item.attr('id').value] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.hasAttr('class')) {
|
|
||||||
item.attr('class').value.replace(/^\s+|\s+$/g, '').split(/\s+/).forEach(function(className) {
|
|
||||||
usageData.classes[className] = true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return usageData;
|
|
||||||
}
|
|
||||||
|
|
||||||
var usageData = walk(ast, {
|
|
||||||
ids: Object.create(null),
|
|
||||||
classes: Object.create(null),
|
|
||||||
tags: Object.create(null)
|
|
||||||
});
|
|
||||||
|
|
||||||
for (var key in usageData) {
|
|
||||||
usageData[key] = Object.keys(usageData[key]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return usageData;
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
||||||
<style>
|
<style>
|
||||||
.st0{ fill:red; padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; } @media screen and (max-width: 200px) { .st1 { display: none; } }
|
.st0{ fill:red; padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; } @media screen and (max-width: 200px) { .st0 { display: none; } }
|
||||||
</style>
|
</style>
|
||||||
<rect width="100" height="100" class="st0" style="stroke-width:3; margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 1em;"/>
|
<rect width="100" height="100" class="st0" style="stroke-width:3; margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 1em;"/>
|
||||||
</svg>
|
</svg>
|
||||||
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
||||||
<style>
|
<style>
|
||||||
.st0{fill:red;padding:1em}@media screen and (max-width:200px){.st1{display:none}}
|
.st0{fill:red;padding:1em}@media screen and (max-width:200px){.st0{display:none}}
|
||||||
</style>
|
</style>
|
||||||
<rect width="100" height="100" class="st0" style="stroke-width:3;margin:1em"/>
|
<rect width="100" height="100" class="st0" style="stroke-width:3;margin:1em"/>
|
||||||
</svg>
|
</svg>
|
||||||
|
Before Width: | Height: | Size: 705 B After Width: | Height: | Size: 705 B |
@ -1,7 +1,7 @@
|
|||||||
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
||||||
<style>
|
<style>
|
||||||
<![CDATA[
|
<![CDATA[
|
||||||
.st0{ fill:red; padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; } @media screen and (max-width: 200px) { .st1 { display: none; } }
|
.st0{ fill:red; padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; } @media screen and (max-width: 200px) { .st0 { display: none; } }
|
||||||
]]>
|
]]>
|
||||||
</style>
|
</style>
|
||||||
<style></style>
|
<style></style>
|
||||||
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
||||||
<style>
|
<style>
|
||||||
.st0{fill:red;padding:1em}@media screen and (max-width:200px){.st1{display:none}}
|
.st0{fill:red;padding:1em}@media screen and (max-width:200px){.st0{display:none}}
|
||||||
</style>
|
</style>
|
||||||
<style/>
|
<style/>
|
||||||
<rect width="100" height="100" class="st0" style="stroke-width:3;margin:1em"/>
|
<rect width="100" height="100" class="st0" style="stroke-width:3;margin:1em"/>
|
||||||
|
Before Width: | Height: | Size: 772 B After Width: | Height: | Size: 772 B |
@ -1,7 +1,7 @@
|
|||||||
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
||||||
<style>
|
<style>
|
||||||
<![CDATA[
|
<![CDATA[
|
||||||
.st0{ fill:red; padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; background-image: url('data:image/svg,<svg width="16" height="16"/>') } @media screen and (max-width: 200px) { .st1 { display: none; } }
|
.st0{ fill:red; padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; background-image: url('data:image/svg,<svg width="16" height="16"/>') } @media screen and (max-width: 200px) { .st0 { display: none; } }
|
||||||
]]>
|
]]>
|
||||||
</style>
|
</style>
|
||||||
<rect width="100" height="100" class="st0" style="stroke-width:3; margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 1em;"/>
|
<rect width="100" height="100" class="st0" style="stroke-width:3; margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 1em;"/>
|
||||||
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
<svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
||||||
<style>
|
<style>
|
||||||
<![CDATA[.st0{fill:red;padding:1em;background-image:url('data:image/svg,<svg width="16" height="16"/>')}@media screen and (max-width:200px){.st1{display:none}}]]>
|
<![CDATA[.st0{fill:red;padding:1em;background-image:url('data:image/svg,<svg width="16" height="16"/>')}@media screen and (max-width:200px){.st0{display:none}}]]>
|
||||||
</style>
|
</style>
|
||||||
<rect width="100" height="100" class="st0" style="stroke-width:3;margin:1em"/>
|
<rect width="100" height="100" class="st0" style="stroke-width:3;margin:1em"/>
|
||||||
</svg>
|
</svg>
|
||||||
|
Before Width: | Height: | Size: 890 B After Width: | Height: | Size: 890 B |
Before Width: | Height: | Size: 432 B After Width: | Height: | Size: 432 B |
@ -25,4 +25,4 @@
|
|||||||
|
|
||||||
@@@
|
@@@
|
||||||
|
|
||||||
{"removeUnusedStyles":{"ids":false,"tags":false}}
|
{"usage":{"ids":false,"tags":false}}
|
Before Width: | Height: | Size: 511 B After Width: | Height: | Size: 498 B |
@ -25,4 +25,4 @@
|
|||||||
|
|
||||||
@@@
|
@@@
|
||||||
|
|
||||||
{"removeUnusedStyles":false}
|
{"usage":false}
|
Before Width: | Height: | Size: 502 B After Width: | Height: | Size: 489 B |