-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add public API #20
Add public API #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<T extends GeneratedFontTypes = GeneratedFontTypes>(options: IconPluginOptions<T>): Plugin { | ||
export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTypes>(options: IconPluginOptions<T>): Plugin<PublicApi> { | ||
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<WebfontsGeneratorResult<T>, 'generateCss' | 'generateHtml' | T>; | ||
const generatedWebfonts: GeneratedWebfont[] = []; | ||
|
||
const generate = async (updateFiles?: boolean) => { | ||
if (updateFiles) { | ||
|
@@ -54,6 +57,11 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp | |
return { | ||
name: 'vite-svg-2-webfont', | ||
enforce: 'pre', | ||
api: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, I was unable to find any docs about this, could you share some docs about how this works and where it can be used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello! Here's the docs: https://rollupjs.org/plugin-development/#direct-plugin-communication I'll update the readme a little bit later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the reference! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! I'll plan to update the readme tomorrow. Sorry for the delay :( |
||
getGeneratedWebfonts(): GeneratedWebfont[] { | ||
return generatedWebfonts; | ||
}, | ||
}, | ||
configResolved(_config) { | ||
isBuild = _config.command === 'build'; | ||
}, | ||
|
@@ -85,6 +93,10 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp | |
type, | ||
`/${this.getFileName(this.emitFile({ type: 'asset', fileName: `assets/${processedOptions.fontName}-${guid()}.${type}`, source: generatedFonts?.[type] }))}`, | ||
]); | ||
|
||
emitted.forEach(([type, href]) => { | ||
generatedWebfonts.push({ type, href }); | ||
}); | ||
fileRefs = Object.fromEntries(emitted) as { [Ref in T]: string }; | ||
} | ||
}, | ||
|
@@ -112,6 +124,7 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp | |
}; | ||
} | ||
export default viteSvgToWebfont; | ||
export { type GeneratedWebfont, type PublicApi }; | ||
|
||
/** | ||
* Paths of default templates available for use. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { GeneratedFontTypes } from '@vusion/webfonts-generator'; | ||
|
||
export type GeneratedWebfont = { | ||
type: GeneratedFontTypes; | ||
href: string; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { GeneratedWebfont } from './generatedWebfont'; | ||
|
||
export interface PublicApi { | ||
getGeneratedWebfonts(): GeneratedWebfont[]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to find a best way to provide an example of how it works without adding a big code examples to the README.md and without duplicating your ./example dir so I just made another repo with plugin example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works IMHO 🙂