Skip to content
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 moduleId param to options that allow customization of virtualModuleId #18

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ The plugin has an API consisting of one required parameter and multiple optional
- eot - [ttf2eot](https://github.com/fontello/ttf2eot).
- See [webfonts-generator#formatoptions](https://github.com/vusion/webfonts-generator#formatoptions)

### moduleId

- **type**: `string`
- **description**: Virtual module id which is used by Vite to import the plugin artifacts. E.g. the default value is "vite-svg-2-webfont.css" so "virtual:vite-svg-2-webfont.css" should be imported.
- **default** `'vite-svg-2-webfont.css'`

## 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).
Expand Down
26 changes: 18 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import type { PublicApi } from './types/publicApi';

const ac = new AbortController();
const webfontGenerator = promisify(_webfontGenerator);
const VIRTUAL_MODULE_ID = 'virtual:vite-svg-2-webfont.css';
const RESOLVED_VIRTUAL_MODULE_ID = `\0${VIRTUAL_MODULE_ID}`;
const DEFAULT_MODULE_ID = 'vite-svg-2-webfont.css';

function getVirtualModuleId<T extends string>(moduleId: T): `virtual:${T}` {
return `virtual:${moduleId}`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the virtual prefix should also be a const?
WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do it if you want, but I personally don't think it's necessary because it's just a simple conventional value that never implies to be changed and it is not used anywhere else.

}

function getResolvedVirtualModuleId<T extends string>(virtualModuleId: T): `\0${T}` {
return `\0${virtualModuleId}`;
}

/**
* A Vite plugin that generates a webfont from your SVG icons.
Expand All @@ -27,6 +34,9 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp
let _reloadModule: undefined | ((module: ModuleNode) => void);
let generatedFonts: undefined | Pick<WebfontsGeneratorResult<T>, 'generateCss' | 'generateHtml' | T>;
const generatedWebfonts: GeneratedWebfont[] = [];
const moduleId = options.moduleId ?? DEFAULT_MODULE_ID;
const virtualModuleId = getVirtualModuleId(moduleId);
const resolvedVirtualModuleId = getResolvedVirtualModuleId(virtualModuleId);

const generate = async (updateFiles?: boolean) => {
if (updateFiles) {
Expand All @@ -48,7 +58,7 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp
await Promise.all(promises);
}
if (updateFiles) {
const module = _moduleGraph?.getModuleById(RESOLVED_VIRTUAL_MODULE_ID);
const module = _moduleGraph?.getModuleById(resolvedVirtualModuleId);
if (module && _reloadModule) {
_reloadModule(module);
}
Expand All @@ -66,22 +76,22 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp
isBuild = _config.command === 'build';
},
resolveId(id) {
if (id !== VIRTUAL_MODULE_ID) {
if (id !== virtualModuleId) {
return undefined;
}
return RESOLVED_VIRTUAL_MODULE_ID;
return resolvedVirtualModuleId;
},
transform(_code, id) {
if (id !== RESOLVED_VIRTUAL_MODULE_ID) {
if (id !== resolvedVirtualModuleId) {
return undefined;
}
return generatedFonts?.generateCss?.(fileRefs) || '';
},
load(id) {
if (id !== RESOLVED_VIRTUAL_MODULE_ID) {
if (id !== resolvedVirtualModuleId) {
return undefined;
}
return RESOLVED_VIRTUAL_MODULE_ID;
return resolvedVirtualModuleId;
},
async buildStart() {
if (!isBuild) {
Expand Down
7 changes: 7 additions & 0 deletions src/optionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ export interface IconPluginOptions<T extends GeneratedFontTypes = GeneratedFontT
* @default '.icon'
*/
baseSelector?: string;
/**
* Virtual module id which is used by Vite to import the plugin artifacts.
* E.g. the default value is "vite-svg-2-webfont.css" so "virtual:vite-svg-2-webfont.css" should be imported.
*
* @default 'vite-svg-2-webfont.css'
*/
moduleId?: string;
}

export function parseIconTypesOption<T extends GeneratedFontTypes = GeneratedFontTypes>({ types }: Pick<IconPluginOptions<T>, 'types'>): T[] {
Expand Down
Loading