From 80e46ecbce88f23048383aa8478f059c1c3579af Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sun, 17 Nov 2024 06:31:30 +0800 Subject: [PATCH] perf: optimize config loading. --- src/index.ts | 28 ++++++++++++++-------------- src/utils.ts | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index e8008c4e..9d82c243 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); @@ -210,24 +211,17 @@ export type IconInfo = { } export type InfoData = Record> -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 => { + const defaultOptions = getDefaultOptions(options); const data = autoConf('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); @@ -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; diff --git a/src/utils.ts b/src/utils.ts index 7a76f590..0e31091d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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'; @@ -349,4 +350,18 @@ export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp }); cssString += srcParts.join(',\n ') + ';'; return cssString; -} \ No newline at end of file +} + +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); +}; \ No newline at end of file