Skip to content

Commit

Permalink
feat(svgo): use configuration to config svgo
Browse files Browse the repository at this point in the history
  • Loading branch information
nivekcode committed Mar 25, 2020
1 parent b2466e2 commit de279ae
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 115 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"format:write": "prettier --write 'src/**/*.ts'",
"prebuild": "npm run copy:readme",
"start": "ts-node ./src/bin/svg-to-ts.ts -s './inputfiles/*.svg'",
"start:svgconfig": "ts-node ./src/bin/svg-to-ts.ts -s './inputfiles/*.svg' --svgoConfig ./svgo-test.config.json",
"start:ofl": "ts-node ./src/bin/svg-to-ts.ts -s './inputfiles/*.svg' --optimizeForLazyLoading true",
"start:ofl:preCompile": "ts-node ./src/bin/svg-to-ts.ts -s './inputfiles/*.svg' --optimizeForLazyLoading true --preCompileSources true",
"start:ofl:preCompile-additionalPath": "ts-node ./src/bin/svg-to-ts.ts -s './inputfiles/*.svg' --optimizeForLazyLoading true --preCompileSources true --additionalModelOutputPath ./additional",
Expand Down
11 changes: 4 additions & 7 deletions src/bin/svg-to-ts.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
#!/usr/bin/env node

import { convertToSingleFile } from '../lib/converters/single-file.converter';
import { convertToMultipleFiles } from '../lib/converters/multiple-files.converter';
import { getOptions, MultiFileConvertionOptions, SingleFileConvertionOptions } from '../lib/options/convertion-options';
import { printLogo } from '../lib/helpers/log-helper';
import { setupCommander } from '../lib/options/args-collector';

const start = async () => {
(async () => {
setupCommander();
printLogo();
const convertionOptions = await getOptions();

if (convertionOptions.optimizeForLazyLoading) {
convertToMultipleFiles(convertionOptions as MultiFileConvertionOptions);
await convertToMultipleFiles(convertionOptions as MultiFileConvertionOptions);
} else {
convertToSingleFile(convertionOptions as SingleFileConvertionOptions);
await convertToSingleFile(convertionOptions as SingleFileConvertionOptions);
}
};

start();
})();
5 changes: 4 additions & 1 deletion src/lib/converters/multiple-files.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { getFilePathsFromRegex } from '../helpers/regex-helpers';
import { deleteFiles, deleteFolder, extractSvgContent, writeFile } from '../helpers/file-helpers';
import { info, separatorEnd, separatorStart, success } from '../helpers/log-helper';
import { svgOptimizer } from '../helpers/svg-optimization';
import { generateSvgOptimizer } from '../helpers/svg-optimization';
import { MultiFileConvertionOptions } from '../options/convertion-options';
import { compile } from '../compiler/typescript-compiler';

Expand All @@ -28,8 +28,11 @@ export const convertToMultipleFiles = async (convertionOptions: MultiFileConvert
modelFileName,
additionalModelOutputPath,
iconsFolderName,
svgoConfig,
compileSources
} = convertionOptions;
const svgOptimizer = generateSvgOptimizer(svgoConfig);

let indexFileContent = '';
let types = generateTypeDefinition(typeName);

Expand Down
15 changes: 13 additions & 2 deletions src/lib/converters/single-file.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ import {
import { getFilePathsFromRegex } from '../helpers/regex-helpers';
import { extractSvgContent, writeFile } from '../helpers/file-helpers';
import { error, success, underlineSuccess } from '../helpers/log-helper';
import { svgOptimizer } from '../helpers/svg-optimization';
import { SingleFileConvertionOptions } from '../options/convertion-options';
import { generateSvgOptimizer } from '../helpers/svg-optimization';

const typesDelimitor = ' | ';

export const convertToSingleFile = async (convertionOptions: SingleFileConvertionOptions): Promise<void> => {
const { typeName, prefix, delimiter, interfaceName, outputDirectory, srcFiles, fileName } = convertionOptions;
const {
typeName,
prefix,
delimiter,
interfaceName,
outputDirectory,
srcFiles,
fileName,
svgoConfig
} = convertionOptions;
const svgOptimizer = generateSvgOptimizer(svgoConfig);

let svgConstants = '';
let types = generateTypeDefinition(typeName);

Expand Down
108 changes: 3 additions & 105 deletions src/lib/helpers/svg-optimization.ts
Original file line number Diff line number Diff line change
@@ -1,110 +1,8 @@
import { readFile } from './file-helpers';

const svgo = require('svgo');

export const svgOptimizer = new svgo({
plugins: [
{
cleanupAttrs: true
},
{
removeDoctype: true
},
{
removeXMLProcInst: true
},
{
removeComments: true
},
{
removeMetadata: true
},
{
removeTitle: true
},
{
removeDesc: true
},
{
removeUselessDefs: true
},
{
removeEditorsNSData: true
},
{
removeEmptyAttrs: true
},
{
removeHiddenElems: true
},
{
removeEmptyText: true
},
{
removeEmptyContainers: true
},
{
removeViewBox: false
},
{
cleanupEnableBackground: true
},
{
convertStyleToAttrs: true
},
{
convertColors: true
},
{
convertPathData: true
},
{
convertTransform: true
},
{
removeUnknownsAndDefaults: true
},
{
removeNonInheritableGroupAttrs: true
},
{
removeUselessStrokeAndFill: true
},
{
removeUnusedNS: true
},
{
cleanupIDs: true
},
{
cleanupNumericValues: true
},
{
moveElemsAttrsToGroup: true
},
{
moveGroupAttrsToElems: true
},
{
collapseGroups: true
},
{
removeRasterImages: false
},
{
mergePaths: true
},
{
convertShapeToPath: true
},
{
sortAttrs: true
},
{
removeDimensions: true
}
]
});
import { readFile } from './file-helpers';

export const generateSvgOptimizer = config => new svgo(config);

export const getSvgoConfig = async (svgoConfig: any): Promise<string> => {
if (typeof svgoConfig === 'string') {
Expand Down
10 changes: 10 additions & 0 deletions svgo-test.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
{
"cleanupAttrs": true
},
{
"removeDoctype": true
}
]
}

0 comments on commit de279ae

Please sign in to comment.