From cda388bd81bad91c2b10673378e0b50320b725c4 Mon Sep 17 00:00:00 2001 From: Kirill Stryaponov Date: Wed, 21 Feb 2024 23:51:55 +0600 Subject: [PATCH] Add public API (#20) Co-authored-by: Chemi Atlow --- README.md | 14 ++++++++++++++ src/index.ts | 15 ++++++++++++++- src/types/generatedWebfont.ts | 6 ++++++ src/types/publicApi.ts | 5 +++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/types/generatedWebfont.ts create mode 100644 src/types/publicApi.ts diff --git a/README.md b/README.md index d9d177f..fc560d6 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**: `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 +- [This repo](https://github.com/stryaponoff/vite-plugin-preload-webfont) contains the usage example. diff --git a/src/index.ts b/src/index.ts index c35eeaf..0e548ae 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); @@ -17,13 +19,14 @@ 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; let _moduleGraph: ModuleGraph; let _reloadModule: undefined | ((module: ModuleNode) => 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