我要完成一个艰巨的任务。通过使用“ grunt-text-replace”,我可以通过自定义正则表达式传递缩小的SVG(svgmin),该表达式将所有乱码引用替换为适当的类。
例如,在Illustrator中,将图层/对象名称声明为class =“ tree”。这将由Illustrator导出为id =“ class =” tree“”。下面的grunt任务将解决这个问题,并将其设置为class =“ tree”。我还在其他一些子任务下粘贴了ID清理(推荐)。
replace: {
// ID cleanup: performs a manual ID cleanup as Illustrator exports a mess
illustrator: {
src: ['assets/svg/optimised/*.svg'],
overwrite: true,
replacements: [{
// Remove escaped underscore character
from: '_x5F_',
to: '_'
}, {
// Replace class names with proper classes
//class_x3D__x22_tank-option_x22__2_
from: /id\=\"class_x3D__x22_(.+?)_x22_(.*?)\"/gi,
to: function(matchedWord, index, fullText, regexMatches) {
return 'class="'+ regexMatches[0].toLowerCase()+'"';
}
}, {
// Lowercase all ids
from: /id\=\"(.+?)\"/gi,
to: function(matchedWord, index, fullText, regexMatches) {
return 'id="'+ regexMatches[0].toLowerCase()+'"';
}
}, {
// Lowercase all id references to match the previous replace rule
from: /url\(\#(.+?)\)/gi,
to: function(matchedWord, index, fullText, regexMatches) {
return 'url(#'+ regexMatches[0].toLowerCase() +')';
}
}, {
// Lowercase all id href to match the previous replace rule
from: /href\=\"\#(.+?)\"/gi,
to: function(matchedWord, index, fullText, regexMatches) {
return 'href="#'+ regexMatches[0].toLowerCase() +'"';
}
}, {
// Remove all font references as we will use CSS for this
from: /font\-family\=\"(.+?)\"/gi,
to: function(matchedWord, index, fullText, regexMatches) {
return '';
}
}]
}
}
然后,您可以在Gruntfile中以以下方式调用此任务:
grunt.registerTask('svgclean', [
'replace:illustrator'
]);