done first version
This commit is contained in:
@@ -3,13 +3,13 @@ import { join } from 'path';
|
||||
|
||||
import { MainConfig, SVGOConfig, OptimizedResponse } from './config';
|
||||
import { svgClean } from './modules/svgo.clean';
|
||||
import { makeConfig } from './modules/svgo.config';
|
||||
import { makeConfig, defaultConfig } from './modules/svgo.config';
|
||||
|
||||
const SVGO = require('svgo');
|
||||
|
||||
export class SvgIconset {
|
||||
|
||||
private optimizeConfig: SVGOConfig;
|
||||
private optimizeConfig: { [prop: string]: boolean }[];
|
||||
private isValid: boolean = true;
|
||||
private svgoPlugin: typeof SVGO;
|
||||
|
||||
@@ -18,8 +18,9 @@ export class SvgIconset {
|
||||
this.isValid = false;
|
||||
}
|
||||
this.optimizeConfig = config.optimize !== undefined ? makeConfig(config.optimize) : makeConfig();
|
||||
this.svgoPlugin = new SVGO(this.optimizeConfig);
|
||||
|
||||
this.svgoPlugin = new SVGO({
|
||||
plugins: this.optimizeConfig
|
||||
});
|
||||
}
|
||||
|
||||
public createSet() {
|
||||
|
28
src/bin.ts
28
src/bin.ts
@@ -2,16 +2,18 @@
|
||||
import { exit } from 'process';
|
||||
|
||||
import { SvgIconset } from './app';
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
|
||||
const argv = require('yargs').argv;
|
||||
const exampleString = 'Example: svg-iconset --source=assets/images/icons --name=icons';
|
||||
const exampleString = 'Example: svg-iconset --source=assets/images/icons --result=icons, check docs https://github.com/DariusNorv/svg-iconset#usage';
|
||||
let optimize = undefined;
|
||||
|
||||
const {
|
||||
source,
|
||||
name,
|
||||
attrs,
|
||||
removeViewBox
|
||||
result,
|
||||
svgoConfig
|
||||
} = argv;
|
||||
|
||||
|
||||
@@ -21,17 +23,23 @@ if (source === undefined) {
|
||||
exit();
|
||||
}
|
||||
|
||||
if (name === undefined) {
|
||||
if (result === undefined) {
|
||||
console.error('Result filename is not set');
|
||||
console.error(exampleString);
|
||||
exit();
|
||||
}
|
||||
|
||||
if (svgoConfig !== undefined) {
|
||||
try {
|
||||
optimize = JSON.parse(readFileSync(join(process.cwd(), svgoConfig), 'utf-8'));
|
||||
} catch (err) {
|
||||
console.error('SVGO configuration file parse error', err.message);
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
new SvgIconset({
|
||||
source,
|
||||
result: name,
|
||||
optimize: {
|
||||
attrs,
|
||||
removeViewBox
|
||||
}
|
||||
result,
|
||||
optimize
|
||||
}).createSet();
|
||||
|
60
src/config.d.ts
vendored
60
src/config.d.ts
vendored
@@ -1,19 +1,3 @@
|
||||
declare type SVGOConfig = [
|
||||
{
|
||||
removeAttrs: {
|
||||
attrs: string
|
||||
}
|
||||
},
|
||||
{
|
||||
removeViewBox: boolean
|
||||
}
|
||||
]
|
||||
|
||||
export interface SvgOptimizeConfig {
|
||||
attrs?: string;
|
||||
removeViewBox?: boolean
|
||||
}
|
||||
|
||||
export interface SVGOResponse {
|
||||
data: string;
|
||||
info: { [propname: string]: string }
|
||||
@@ -27,5 +11,47 @@ export interface OptimizedResponse {
|
||||
export interface MainConfig {
|
||||
source: string;
|
||||
result: string;
|
||||
optimize?: SvgOptimizeConfig
|
||||
optimize?: SVGOConfig
|
||||
}
|
||||
|
||||
interface SVGOConfig extends Iterable<{ [prop: string]: boolean }>{
|
||||
removeDoctype?: boolean;
|
||||
removeXMLProcInst?: boolean;
|
||||
removeComments?: boolean;
|
||||
removeMetadata?: boolean;
|
||||
removeXMLNS?: boolean;
|
||||
removeEditorsNSData?: boolean;
|
||||
cleanupAttrs?: boolean;
|
||||
inlineStyles?: boolean;
|
||||
minifyStyles?: boolean;
|
||||
convertStyleToAttrs?: boolean;
|
||||
cleanupIDs?: boolean;
|
||||
removeRasterImages?: boolean;
|
||||
removeUselessDefs?: boolean;
|
||||
cleanupNumericValues?: boolean;
|
||||
cleanupListOfValues?: boolean;
|
||||
convertColors?: boolean;
|
||||
removeUnknownsAndDefaults?: boolean;
|
||||
removeNonInheritableGroupAttrs?: boolean;
|
||||
removeUselessStrokeAndFill?: boolean;
|
||||
removeViewBox?: boolean;
|
||||
cleanupEnableBackground?: boolean;
|
||||
removeHiddenElems?: boolean;
|
||||
removeEmptyText?: boolean;
|
||||
convertShapeToPath?: boolean;
|
||||
moveElemsAttrsToGroup?: boolean;
|
||||
moveGroupAttrsToElems?: boolean;
|
||||
collapseGroups?: boolean;
|
||||
convertPathData?: boolean;
|
||||
convertTransform?: boolean;
|
||||
removeEmptyAttrs?: boolean;
|
||||
removeEmptyContainers?: boolean;
|
||||
mergePaths?: boolean;
|
||||
removeUnusedNS?: boolean;
|
||||
sortAttrs?: boolean;
|
||||
removeTitle?: boolean;
|
||||
removeDesc?: boolean;
|
||||
removeDimensions?: boolean;
|
||||
removeStyleElement?: boolean;
|
||||
removeScriptElement?: boolean;
|
||||
}
|
||||
|
@@ -1,27 +1,65 @@
|
||||
import { SVGOConfig, SvgOptimizeConfig } from '../config';
|
||||
import { SVGOConfig } from '../config';
|
||||
|
||||
const defaultConfig: SVGOConfig = [
|
||||
{
|
||||
removeAttrs: {
|
||||
attrs: '(width|height)'
|
||||
},
|
||||
},
|
||||
{ removeViewBox: false }
|
||||
];
|
||||
export const defaultConfig: SVGOConfig = {
|
||||
removeDoctype: true,
|
||||
removeXMLProcInst: true,
|
||||
removeComments: true,
|
||||
removeMetadata: true,
|
||||
removeXMLNS: false,
|
||||
removeEditorsNSData: true,
|
||||
cleanupAttrs: true,
|
||||
inlineStyles: true,
|
||||
minifyStyles: true,
|
||||
convertStyleToAttrs: true,
|
||||
cleanupIDs: true,
|
||||
removeRasterImages: false,
|
||||
removeUselessDefs: true,
|
||||
cleanupNumericValues: true,
|
||||
cleanupListOfValues: false,
|
||||
convertColors: true,
|
||||
removeUnknownsAndDefaults: true,
|
||||
removeNonInheritableGroupAttrs: true,
|
||||
removeUselessStrokeAndFill: true,
|
||||
removeViewBox: false,
|
||||
cleanupEnableBackground: true,
|
||||
removeHiddenElems: true,
|
||||
removeEmptyText: true,
|
||||
convertShapeToPath: true,
|
||||
moveElemsAttrsToGroup: true,
|
||||
moveGroupAttrsToElems: true,
|
||||
collapseGroups: true,
|
||||
convertPathData: true,
|
||||
convertTransform: true,
|
||||
removeEmptyAttrs: true,
|
||||
removeEmptyContainers: true,
|
||||
mergePaths: true,
|
||||
removeUnusedNS: true,
|
||||
sortAttrs: false,
|
||||
removeTitle: true,
|
||||
removeDesc: true,
|
||||
removeDimensions: true,
|
||||
removeStyleElement: false,
|
||||
removeScriptElement: false,
|
||||
|
||||
export function makeConfig(config?: SvgOptimizeConfig): SVGOConfig {
|
||||
|
||||
if (config !== undefined) {
|
||||
const { attrs, removeViewBox } = config;
|
||||
|
||||
if (attrs !== undefined) {
|
||||
defaultConfig[0].removeAttrs.attrs = `(${attrs.replace(/,\s/g, '|')})`;
|
||||
}
|
||||
|
||||
if (removeViewBox) {
|
||||
defaultConfig[1].removeViewBox = removeViewBox;
|
||||
*[Symbol.iterator]() {
|
||||
for (const name of Object.keys(this)) {
|
||||
yield JSON.parse(`{"${name}": ${(this as any)[name]}}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return defaultConfig;
|
||||
export function makeConfig(config?: SVGOConfig): { [prop: string]: boolean }[] {
|
||||
|
||||
if (config !== undefined) {
|
||||
return [...defaultConfig].map(item => {
|
||||
const key = Object.keys(item)[0];
|
||||
if (config.hasOwnProperty(key)) {
|
||||
item[key] = (config as any)[key];
|
||||
}
|
||||
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
return [...defaultConfig];
|
||||
}
|
||||
|
Reference in New Issue
Block a user