Skip to content

Commit

Permalink
feat: add excludeFormat option. #238
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Nov 16, 2024
1 parent 38b8110 commit b41b870
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 41 deletions.
30 changes: 24 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { FontOptions } from 'svg2ttf';
import type { Config } from 'svgo';
import { log } from './log.js';
import { generateIconsSource, generateReactIcons, generateReactNativeIcons } from './generate.js';
import { createSVG, createTTF, createEOT, createWOFF, createWOFF2, createSvgSymbol, copyTemplate, type CSSOptions, createHTML, createTypescript, type TypescriptOptions } from './utils.js';
import { createSVG, createTTF, createEOT, createWOFF, createWOFF2, createSvgSymbol, copyTemplate, type CSSOptions, createHTML, generateFontFaceCSS, createTypescript, type TypescriptOptions } from './utils.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -129,6 +129,11 @@ export type SvgToFontOptions = {
* This is the setting for [svg2ttf](https://github.com/fontello/svg2ttf/tree/c33a126920f46b030e8ce960cc7a0e38a6946bbc#svg2ttfsvgfontstring-options---buf)
*/
svg2ttf?: FontOptions;
/**
* You can configure which font files to exclude from generation. By default, all font files will be generated.
* https://github.com/jaywcjlove/svgtofont/issues/238
*/
excludeFormat?: Array<"eot" | "woff" | "woff2" | "ttf" | "svg" | "symbol.svg">;
website?: {
/**
* Add a Github corner to your website
Expand Down Expand Up @@ -243,6 +248,8 @@ export default async (options: SvgToFontOptions = {}) => {

options.svgicons2svgfont.fontName = options.fontName;
options.classNamePrefix = options.classNamePrefix || options.fontName;

const excludeFormat = options.excludeFormat || [];

const fontSizeOpt = typeof options.css !== 'boolean' && options.css.fontSize;
const fontSize = typeof fontSizeOpt === 'boolean' ? (fontSizeOpt === true ? 'font-size: 16px;' : '') : `font-size: ${fontSizeOpt};`;
Expand Down Expand Up @@ -314,15 +321,25 @@ export default async (options: SvgToFontOptions = {}) => {
await fs.writeJSON(infoDataPath, infoData, { spaces: 2 });
log.log(`${color.green('SUCCESS')} Created ${infoDataPath} `);
}

const ttf = await createTTF(options);
await createEOT(options, ttf);
await createWOFF(options, ttf);
await createWOFF2(options, ttf);
await createSvgSymbol(options);
if (!excludeFormat.includes('eot')) await createEOT(options, ttf);
if (!excludeFormat.includes('woff')) await createWOFF(options, ttf);
if (!excludeFormat.includes('woff2')) await createWOFF2(options, ttf);
if (!excludeFormat.includes('symbol.svg')) await createSvgSymbol(options);

const ttfPath = path.join(options.dist, options.fontName + ".ttf");
if (excludeFormat.includes('ttf')) {
fs.removeSync(ttfPath);
}

if (options.css) {
const styleTemplatePath = options.styleTemplates || path.resolve(__dirname, 'styles')
const outDir = typeof options.css === 'object' ? options.css.output || options.dist : options.dist;

const cssOptions = typeof options.css === 'object' ? options.css : {};
const fontFamilyString = generateFontFaceCSS(options.fontName, cssOptions.cssPath || "", Date.now(), excludeFormat);

await copyTemplate(styleTemplatePath, outDir, {
fontname: options.fontName,
cssString: cssString.join(''),
Expand All @@ -331,8 +348,9 @@ export default async (options: SvgToFontOptions = {}) => {
fontSize: fontSize,
timestamp: new Date().getTime(),
prefix,
fontFamily: fontFamilyString,
nameAsUnicode: options.useNameAsUnicode,
_opts: typeof options.css === 'boolean' ? {} : { ...options.css }
_opts: cssOptions
});
}

Expand Down
8 changes: 1 addition & 7 deletions src/styles/_{{filename}}.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
@font-face {
font-family: "{{fontname}}";
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}'); /* IE9*/
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}#iefix') format('embedded-opentype'), /* IE6-IE8 */
url("{{cssPath}}{{fontname}}.woff2?t={{timestamp}}") format("woff2"),
url("{{cssPath}}{{fontname}}.woff?t={{timestamp}}") format("woff"),
url('{{cssPath}}{{fontname}}.ttf?t={{timestamp}}') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
{{fontFamily}}
}

{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
Expand Down
9 changes: 2 additions & 7 deletions src/styles/_{{filename}}.less
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
@font-face {font-family: "{{fontname}}";
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}'); /* IE9*/
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}#iefix') format('embedded-opentype'), /* IE6-IE8 */
url("{{cssPath}}{{fontname}}.woff2?t={{timestamp}}") format("woff2"),
url("{{cssPath}}{{fontname}}.woff?t={{timestamp}}") format("woff"),
url('{{cssPath}}{{fontname}}.ttf?t={{timestamp}}') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
@font-face {
{{fontFamily}}
}

{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
Expand Down
9 changes: 2 additions & 7 deletions src/styles/_{{filename}}.module.less
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
@font-face {font-family: "{{fontname}}";
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}'); /* IE9*/
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}#iefix') format('embedded-opentype'), /* IE6-IE8 */
url("{{cssPath}}{{fontname}}.woff2?t={{timestamp}}") format("woff2"),
url("{{cssPath}}{{fontname}}.woff?t={{timestamp}}") format("woff"),
url('{{cssPath}}{{fontname}}.ttf?t={{timestamp}}') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
@font-face {
{{fontFamily}}
}

{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
Expand Down
9 changes: 2 additions & 7 deletions src/styles/_{{filename}}.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
@font-face {font-family: "{{fontname}}";
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}'); /* IE9*/
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}#iefix') format('embedded-opentype'), /* IE6-IE8 */
url("{{cssPath}}{{fontname}}.woff2?t={{timestamp}}") format("woff2"),
url("{{cssPath}}{{fontname}}.woff?t={{timestamp}}") format("woff"),
url('{{cssPath}}{{fontname}}.ttf?t={{timestamp}}') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
@font-face {
{{fontFamily}}
}

{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
Expand Down
9 changes: 2 additions & 7 deletions src/styles/_{{filename}}.styl
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
@font-face {font-family: "{{fontname}}";
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}'); /* IE9*/
src: url('{{cssPath}}{{fontname}}.eot?t={{timestamp}}#iefix') format('embedded-opentype'), /* IE6-IE8 */
url("{{cssPath}}{{fontname}}.woff2?t={{timestamp}}") format("woff2"),
url("{{cssPath}}{{fontname}}.woff?t={{timestamp}}") format("woff"),
url('{{cssPath}}{{fontname}}.ttf?t={{timestamp}}') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
@font-face {
{{fontFamily}}
}

{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
Expand Down
24 changes: 24 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,27 @@ export function createHTML(templatePath: string, data: Record<string, any>): str
});
};

export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp: number, excludeFormat: string[]): string {
const formats = [
{ ext: 'eot', format: 'embedded-opentype', ieFix: true },
{ ext: 'woff2', format: 'woff2' },
{ ext: 'woff', format: 'woff' },
{ ext: 'ttf', format: 'truetype' },
{ ext: 'svg', format: 'svg' }
];
let cssString = ` font-family: "${fontName}";\n`;
if (!excludeFormat.includes('eot')) {
cssString += ` src: url('${cssPath}${fontName}.eot?t=${timestamp}'); /* IE9*/\n`;
}
cssString += ' src: ';
const srcParts = formats
.filter(format => !excludeFormat.includes(format.ext))
.map(format => {
if (format.ext === 'eot') {
return `url('${cssPath}${fontName}.eot?t=${timestamp}#iefix') format('${format.format}') /* IE6-IE8 */`;
}
return `url('${cssPath}${fontName}.${format.ext}?t=${timestamp}') format('${format.format}')`;
});
cssString += srcParts.join(',\n ') + ';';
return cssString;
}

0 comments on commit b41b870

Please sign in to comment.