From 2b01399c20c84160d2aed3f5f076e045e75acfbd Mon Sep 17 00:00:00 2001 From: Kirill Stryaponov Date: Thu, 15 Feb 2024 14:31:14 +0600 Subject: [PATCH 1/5] Add public API --- src/index.ts | 13 +++++++++++++ src/types/generatedWebfont.ts | 4 ++++ src/types/publicApi.ts | 5 +++++ 3 files changed, 22 insertions(+) create mode 100644 src/types/generatedWebfont.ts create mode 100644 src/types/publicApi.ts diff --git a/src/index.ts b/src/index.ts index c35eeaf..9f12d7a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,8 @@ import { parseOptions, parseFiles } from './optionParser'; import type { Plugin, ModuleGraph, ModuleNode } from 'vite'; import type { GeneratedFontTypes, WebfontsGeneratorResult } from '@vusion/webfonts-generator'; import type { IconPluginOptions } from './optionParser'; +import type { GeneratedWebfont } from './types/generatedWebfont'; +import type { PublicApi } from './types/publicApi'; const ac = new AbortController(); const webfontGenerator = promisify(_webfontGenerator); @@ -24,6 +26,7 @@ export function viteSvgToWebfont void); let generatedFonts: undefined | Pick, 'generateCss' | 'generateHtml' | T>; + const generatedWebfonts: GeneratedWebfont[] = []; const generate = async (updateFiles?: boolean) => { if (updateFiles) { @@ -54,6 +57,11 @@ export function viteSvgToWebfont { + generatedWebfonts.push({ type, href }); + }); fileRefs = Object.fromEntries(emitted) as { [Ref in T]: string }; } }, @@ -112,6 +124,7 @@ export function viteSvgToWebfont Date: Thu, 15 Feb 2024 14:56:56 +0600 Subject: [PATCH 2/5] Narrow the type for generated font type --- src/types/generatedWebfont.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/types/generatedWebfont.ts b/src/types/generatedWebfont.ts index eef9cd4..d37faf3 100644 --- a/src/types/generatedWebfont.ts +++ b/src/types/generatedWebfont.ts @@ -1,4 +1,6 @@ +import type { GeneratedFontTypes } from '@vusion/webfonts-generator'; + export type GeneratedWebfont = { - type: string; + type: GeneratedFontTypes; href: string; }; From 2141544f2d6a7fd0ee0af603fdcb04126aae5c5c Mon Sep 17 00:00:00 2001 From: Kirill Stryaponov Date: Thu, 15 Feb 2024 15:01:06 +0600 Subject: [PATCH 3/5] Improve typings --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9f12d7a..0e548ae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,7 +19,7 @@ const RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`; * The plugin uses {@link https://github.com/vusion/webfonts-generator/ webfonts-generator} package to create fonts in any format. * It also generates CSS files that allow using the icons directly in your HTML output, using CSS classes per-icon. */ -export function viteSvgToWebfont(options: IconPluginOptions): Plugin { +export function viteSvgToWebfont(options: IconPluginOptions): Plugin { const processedOptions = parseOptions(options); let isBuild: boolean; let fileRefs: { [Ref in T]: string } | undefined; @@ -61,7 +61,7 @@ export function viteSvgToWebfont Date: Tue, 20 Feb 2024 14:16:35 +0600 Subject: [PATCH 4/5] Update README.md --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index d9d177f..08b6833 100644 --- a/README.md +++ b/README.md @@ -249,3 +249,17 @@ The plugin has an API consisting of one required parameter and multiple optional - woff - [ttf2woff](https://github.com/fontello/ttf2woff). - eot - [ttf2eot](https://github.com/fontello/ttf2eot). - See [webfonts-generator#formatoptions](https://github.com/vusion/webfonts-generator#formatoptions) + +## Public API + +The plugin exposes a public API that can be used inside another plugins using [Rollup inter-plugin communication mechanism](https://rollupjs.org/plugin-development/#inter-plugin-communication). + +Currently available methods: + +### getGeneratedWebfonts + +- **returns**: `{ type: 'svg' | 'ttf' | 'woff2' | 'woff' | 'eot', href: string }` +- **description** + - `type` - a font format generated by a plugin + - `href` - a path to a generated font +- [This repo](https://github.com/stryaponoff/vite-plugin-preload-webfont) contains the usage example. From e87898cda8f6118ff8a31cc86f99dea9cdc54f55 Mon Sep 17 00:00:00 2001 From: Kirill Stryaponov Date: Wed, 21 Feb 2024 14:05:09 +0600 Subject: [PATCH 5/5] Update README.md Co-authored-by: Chemi Atlow --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08b6833..fc560d6 100644 --- a/README.md +++ b/README.md @@ -258,7 +258,7 @@ Currently available methods: ### getGeneratedWebfonts -- **returns**: `{ type: 'svg' | 'ttf' | 'woff2' | 'woff' | 'eot', href: string }` +- **returns**: `Array<{ type: 'svg' | 'ttf' | 'woff2' | 'woff' | 'eot', href: string }>` - **description** - `type` - a font format generated by a plugin - `href` - a path to a generated font