mirror of
https://github.com/iconify/iconify.git
synced 2024-11-17 01:55:09 +00:00
feat(utils): allow provide cwd
options to node loader
This commit is contained in:
parent
0398360de9
commit
1a97b64a10
@ -413,7 +413,8 @@
|
||||
"@iconify/types": "workspace:^",
|
||||
"debug": "^4.3.4",
|
||||
"kolorist": "^1.8.0",
|
||||
"local-pkg": "^0.5.0"
|
||||
"local-pkg": "^0.5.0",
|
||||
"mlly": "^1.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@iconify-json/flat-color-icons": "^1.1.6",
|
||||
|
@ -12,7 +12,8 @@ import { getPossibleIconNames } from './utils';
|
||||
*/
|
||||
export function createExternalPackageIconLoader(
|
||||
packageName: ExternalPkgName,
|
||||
autoInstall: AutoInstall = false
|
||||
autoInstall: AutoInstall = false,
|
||||
cwd?: string
|
||||
) {
|
||||
let scope: string;
|
||||
let collection: string;
|
||||
@ -39,7 +40,8 @@ export function createExternalPackageIconLoader(
|
||||
collections[collection] = createCustomIconLoader(
|
||||
scope,
|
||||
collection,
|
||||
autoInstall
|
||||
autoInstall,
|
||||
cwd
|
||||
);
|
||||
|
||||
return collections;
|
||||
@ -48,10 +50,16 @@ export function createExternalPackageIconLoader(
|
||||
function createCustomIconLoader(
|
||||
scope: string,
|
||||
collection: string,
|
||||
autoInstall: AutoInstall
|
||||
autoInstall: AutoInstall,
|
||||
cwd?: string
|
||||
) {
|
||||
// create the custom collection loader
|
||||
const iconSetPromise = loadCollectionFromFS(collection, autoInstall, scope);
|
||||
const iconSetPromise = loadCollectionFromFS(
|
||||
collection,
|
||||
autoInstall,
|
||||
scope,
|
||||
cwd
|
||||
);
|
||||
return <CustomIconLoader>(async (icon) => {
|
||||
// await until the collection is loaded
|
||||
const iconSet = await iconSetPromise;
|
||||
|
@ -3,6 +3,7 @@ import { isPackageExists, resolveModule, importModule } from 'local-pkg';
|
||||
import type { IconifyJSON } from '@iconify/types';
|
||||
import { tryInstallPkg } from './install-pkg';
|
||||
import type { AutoInstall } from './types';
|
||||
import { resolvePath } from 'mlly';
|
||||
|
||||
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
|
||||
const isLegacyExists = isPackageExists('@iconify/json');
|
||||
@ -18,7 +19,8 @@ const isLegacyExists = isPackageExists('@iconify/json');
|
||||
export async function loadCollectionFromFS(
|
||||
name: string,
|
||||
autoInstall: AutoInstall = false,
|
||||
scope = '@iconify-json'
|
||||
scope = '@iconify-json',
|
||||
cwd = process.cwd()
|
||||
): Promise<IconifyJSON | undefined> {
|
||||
if (!(await _collections[name])) {
|
||||
_collections[name] = task();
|
||||
@ -27,22 +29,33 @@ export async function loadCollectionFromFS(
|
||||
|
||||
async function task() {
|
||||
const packageName = scope.length === 0 ? name : `${scope}/${name}`;
|
||||
let jsonPath = resolveModule(`${packageName}/icons.json`);
|
||||
let jsonPath = await resolvePath(`${packageName}/icons.json`, {
|
||||
url: cwd,
|
||||
}).catch(() => undefined);
|
||||
|
||||
// Legacy support for @iconify/json
|
||||
if (scope === '@iconify-json') {
|
||||
if (!jsonPath && isLegacyExists) {
|
||||
jsonPath = resolveModule(`@iconify/json/json/${name}.json`);
|
||||
jsonPath = await resolvePath(
|
||||
`@iconify/json/json/${name}.json`,
|
||||
{
|
||||
url: cwd,
|
||||
}
|
||||
).catch(() => undefined);
|
||||
}
|
||||
|
||||
// Try to install the package if it doesn't exist
|
||||
if (!jsonPath && !isLegacyExists && autoInstall) {
|
||||
await tryInstallPkg(packageName, autoInstall);
|
||||
jsonPath = resolveModule(`${packageName}/icons.json`);
|
||||
jsonPath = await resolvePath(`${packageName}/icons.json`, {
|
||||
url: cwd,
|
||||
}).catch(() => undefined);
|
||||
}
|
||||
} else if (!jsonPath && autoInstall) {
|
||||
await tryInstallPkg(packageName, autoInstall);
|
||||
jsonPath = resolveModule(`${packageName}/icons.json`);
|
||||
jsonPath = await resolvePath(`${packageName}/icons.json`, {
|
||||
url: cwd,
|
||||
}).catch(() => undefined);
|
||||
}
|
||||
|
||||
// Try to import module if it exists
|
||||
|
@ -17,7 +17,9 @@ export const loadNodeIcon: UniversalIconLoader = async (
|
||||
|
||||
const iconSet = await loadCollectionFromFS(
|
||||
collection,
|
||||
options?.autoInstall
|
||||
options?.autoInstall,
|
||||
undefined,
|
||||
options?.cwd
|
||||
);
|
||||
if (iconSet) {
|
||||
result = await searchForIcon(
|
||||
|
@ -179,4 +179,12 @@ export type IconifyLoaderOptions = {
|
||||
* If you need that properties just add an empty object here, useful for example when using the `svg` on `CSS`.
|
||||
*/
|
||||
usedProps?: Record<string, string>;
|
||||
|
||||
/**
|
||||
* Current working directory, used to resolve the @iconify-json package.
|
||||
*
|
||||
* Only used on `node` environment.
|
||||
* @default process.cwd()
|
||||
*/
|
||||
cwd?: string;
|
||||
};
|
||||
|
2231
pnpm-lock.yaml
2231
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user