Skip to content

Commit

Permalink
perf: optimize config loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Nov 16, 2024
1 parent b41b870 commit 80e46ec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
28 changes: 14 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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, generateFontFaceCSS, createTypescript, type TypescriptOptions } from './utils.js';
import { createSVG, createTTF, createEOT, createWOFF, createWOFF2, createSvgSymbol, copyTemplate, type CSSOptions, createHTML, createTypescript, type TypescriptOptions } from './utils.js';
import { generateFontFaceCSS, getDefaultOptions } from './utils.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -210,24 +211,17 @@ export type IconInfo = {
}
export type InfoData = Record<string, Partial<IconInfo>>

export default async (options: SvgToFontOptions = {}) => {
const defaultOptions: SvgToFontOptions = merge({
dist: path.resolve(process.cwd(), 'fonts'),
src: path.resolve(process.cwd(), 'svg'),
startUnicode: 0xea01,
svg2ttf: {},
svgicons2svgfont: {
fontName: 'iconfont',
},
fontName: 'iconfont',
symbolNameDelimiter: '-',
}, options);
const loadConfig = async (options: SvgToFontOptions): Promise<SvgToFontOptions> => {
const defaultOptions = getDefaultOptions(options);
const data = autoConf<SvgToFontOptions>('svgtofont', {
mustExist: true,
default: defaultOptions,
...options.config,
});
options = merge(defaultOptions, data);
return merge(defaultOptions, data);
};

const handlePkgConfig = (options: SvgToFontOptions): SvgToFontOptions => {
const pkgPath = path.join(process.cwd(), 'package.json');
if (fs.pathExistsSync(pkgPath)) {
const pkg = fs.readJSONSync(pkgPath);
Expand All @@ -242,6 +236,12 @@ export default async (options: SvgToFontOptions = {}) => {
options.website.version = options.website.version ?? pkg.version;
}
}
return options;
};

export default async (options: SvgToFontOptions = {}) => {
options = await loadConfig(options);
options = handlePkgConfig(options);
if (options.log === undefined) options.log = true;
log.disabled = !options.log;
if (options.logger && typeof options.logger === 'function') log.logger = options.logger;
Expand Down
17 changes: 16 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ttf2eot from 'ttf2eot';
import ttf2woff from 'ttf2woff';
import ttf2woff2 from 'ttf2woff2';
import nunjucks from 'nunjucks';
import { merge } from 'auto-config-loader';
import { type SvgToFontOptions } from './';
import { log } from './log.js';

Expand Down Expand Up @@ -349,4 +350,18 @@ export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp
});
cssString += srcParts.join(',\n ') + ';';
return cssString;
}
}

export const getDefaultOptions = (options: SvgToFontOptions): SvgToFontOptions => {
return merge({
dist: path.resolve(process.cwd(), 'fonts'),
src: path.resolve(process.cwd(), 'svg'),
startUnicode: 0xea01,
svg2ttf: {},
svgicons2svgfont: {
fontName: 'iconfont',
},
fontName: 'iconfont',
symbolNameDelimiter: '-',
}, options);
};

0 comments on commit 80e46ec

Please sign in to comment.