mirror of
https://github.com/iconify/iconify.git
synced 2024-12-12 13:47:49 +00:00
Merge pull request #280 from antfu/feat/utils-node-loader-cwd
feat(utils): allow provide `cwd` options to node loader
This commit is contained in:
commit
e863c66533
@ -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;
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { promises as fs, Stats } from 'fs';
|
||||
import { isPackageExists, resolveModule, importModule } from 'local-pkg';
|
||||
import { isPackageExists, 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,27 +29,38 @@ 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
|
||||
if (!jsonPath) {
|
||||
let packagePath = resolveModule(packageName);
|
||||
let packagePath = await resolvePath(packageName, { url: cwd });
|
||||
if (packagePath?.match(/^[a-z]:/i)) {
|
||||
packagePath = `file:///${packagePath}`.replace(/\\/g, '/');
|
||||
}
|
||||
|
@ -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