Skip to content

Commit

Permalink
fix: Fix svg size issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Feb 10, 2022
1 parent 4567736 commit 6936d6c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ async function buildPathsObject(files: string[], options: SvgToFontOptions = {})
);
}

const reactSource = (name: string, source: string) => `import React from 'react';
const reactSource = (name: string, size: string, source: string) => `import React from 'react';
export const ${name} = props => (
<svg viewBox="0 0 20 20" width="20" height="20" {...props}>${source}</svg>
<svg viewBox="0 0 20 20" width="${size}" height="${size}" {...props}>${source}</svg>
);
`;

Expand All @@ -69,6 +69,7 @@ const capitalizeEveryWord = (str: string) => str.replace(/-(\w)/g, ($0, $1) => $

async function outputReactFile(files: string[], options: SvgToFontOptions = {}) {
const svgoOptions = options.svgoOptions || {}
const fontSize = options.css && typeof options.css !== 'boolean' && options.css.fontSize ? options.css.fontSize : '16px';
return Promise.all(
files.map(async filepath => {
const name = capitalizeEveryWord(path.basename(filepath, '.svg'));
Expand All @@ -88,7 +89,7 @@ async function outputReactFile(files: string[], options: SvgToFontOptions = {})
const str: string[] = (pathData.data.match(/ d="[^"]+"/g) || []).map(s => s.slice(3));
const outDistPath = path.join(options.dist, 'react', `${name}.js`);
const pathStrings = str.map((d, i) => `<path d=${d} fillRule="evenodd" />`);
fs.outputFileSync(outDistPath, reactSource(name, pathStrings.join(',\n')));
fs.outputFileSync(outDistPath, reactSource(name, fontSize, pathStrings.join(',\n')));
fs.outputFileSync(outDistPath.replace(/\.js$/, '.d.ts'), reactTypeSource(name));
return `export * from './${name}';`;
}),
Expand Down

0 comments on commit 6936d6c

Please sign in to comment.