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:^",
|
"@iconify/types": "workspace:^",
|
||||||
"debug": "^4.3.4",
|
"debug": "^4.3.4",
|
||||||
"kolorist": "^1.8.0",
|
"kolorist": "^1.8.0",
|
||||||
"local-pkg": "^0.5.0"
|
"local-pkg": "^0.5.0",
|
||||||
|
"mlly": "^1.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@iconify-json/flat-color-icons": "^1.1.6",
|
"@iconify-json/flat-color-icons": "^1.1.6",
|
||||||
|
@ -12,7 +12,8 @@ import { getPossibleIconNames } from './utils';
|
|||||||
*/
|
*/
|
||||||
export function createExternalPackageIconLoader(
|
export function createExternalPackageIconLoader(
|
||||||
packageName: ExternalPkgName,
|
packageName: ExternalPkgName,
|
||||||
autoInstall: AutoInstall = false
|
autoInstall: AutoInstall = false,
|
||||||
|
cwd?: string
|
||||||
) {
|
) {
|
||||||
let scope: string;
|
let scope: string;
|
||||||
let collection: string;
|
let collection: string;
|
||||||
@ -39,7 +40,8 @@ export function createExternalPackageIconLoader(
|
|||||||
collections[collection] = createCustomIconLoader(
|
collections[collection] = createCustomIconLoader(
|
||||||
scope,
|
scope,
|
||||||
collection,
|
collection,
|
||||||
autoInstall
|
autoInstall,
|
||||||
|
cwd
|
||||||
);
|
);
|
||||||
|
|
||||||
return collections;
|
return collections;
|
||||||
@ -48,10 +50,16 @@ export function createExternalPackageIconLoader(
|
|||||||
function createCustomIconLoader(
|
function createCustomIconLoader(
|
||||||
scope: string,
|
scope: string,
|
||||||
collection: string,
|
collection: string,
|
||||||
autoInstall: AutoInstall
|
autoInstall: AutoInstall,
|
||||||
|
cwd?: string
|
||||||
) {
|
) {
|
||||||
// create the custom collection loader
|
// create the custom collection loader
|
||||||
const iconSetPromise = loadCollectionFromFS(collection, autoInstall, scope);
|
const iconSetPromise = loadCollectionFromFS(
|
||||||
|
collection,
|
||||||
|
autoInstall,
|
||||||
|
scope,
|
||||||
|
cwd
|
||||||
|
);
|
||||||
return <CustomIconLoader>(async (icon) => {
|
return <CustomIconLoader>(async (icon) => {
|
||||||
// await until the collection is loaded
|
// await until the collection is loaded
|
||||||
const iconSet = await iconSetPromise;
|
const iconSet = await iconSetPromise;
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { promises as fs, Stats } from 'fs';
|
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 type { IconifyJSON } from '@iconify/types';
|
||||||
import { tryInstallPkg } from './install-pkg';
|
import { tryInstallPkg } from './install-pkg';
|
||||||
import type { AutoInstall } from './types';
|
import type { AutoInstall } from './types';
|
||||||
|
import { resolvePath } from 'mlly';
|
||||||
|
|
||||||
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
|
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
|
||||||
const isLegacyExists = isPackageExists('@iconify/json');
|
const isLegacyExists = isPackageExists('@iconify/json');
|
||||||
@ -18,7 +19,8 @@ const isLegacyExists = isPackageExists('@iconify/json');
|
|||||||
export async function loadCollectionFromFS(
|
export async function loadCollectionFromFS(
|
||||||
name: string,
|
name: string,
|
||||||
autoInstall: AutoInstall = false,
|
autoInstall: AutoInstall = false,
|
||||||
scope = '@iconify-json'
|
scope = '@iconify-json',
|
||||||
|
cwd = process.cwd()
|
||||||
): Promise<IconifyJSON | undefined> {
|
): Promise<IconifyJSON | undefined> {
|
||||||
if (!(await _collections[name])) {
|
if (!(await _collections[name])) {
|
||||||
_collections[name] = task();
|
_collections[name] = task();
|
||||||
@ -27,27 +29,38 @@ export async function loadCollectionFromFS(
|
|||||||
|
|
||||||
async function task() {
|
async function task() {
|
||||||
const packageName = scope.length === 0 ? name : `${scope}/${name}`;
|
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
|
// Legacy support for @iconify/json
|
||||||
if (scope === '@iconify-json') {
|
if (scope === '@iconify-json') {
|
||||||
if (!jsonPath && isLegacyExists) {
|
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
|
// Try to install the package if it doesn't exist
|
||||||
if (!jsonPath && !isLegacyExists && autoInstall) {
|
if (!jsonPath && !isLegacyExists && autoInstall) {
|
||||||
await tryInstallPkg(packageName, autoInstall);
|
await tryInstallPkg(packageName, autoInstall);
|
||||||
jsonPath = resolveModule(`${packageName}/icons.json`);
|
jsonPath = await resolvePath(`${packageName}/icons.json`, {
|
||||||
|
url: cwd,
|
||||||
|
}).catch(() => undefined);
|
||||||
}
|
}
|
||||||
} else if (!jsonPath && autoInstall) {
|
} else if (!jsonPath && autoInstall) {
|
||||||
await tryInstallPkg(packageName, 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
|
// Try to import module if it exists
|
||||||
if (!jsonPath) {
|
if (!jsonPath) {
|
||||||
let packagePath = resolveModule(packageName);
|
let packagePath = await resolvePath(packageName, { url: cwd });
|
||||||
if (packagePath?.match(/^[a-z]:/i)) {
|
if (packagePath?.match(/^[a-z]:/i)) {
|
||||||
packagePath = `file:///${packagePath}`.replace(/\\/g, '/');
|
packagePath = `file:///${packagePath}`.replace(/\\/g, '/');
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,9 @@ export const loadNodeIcon: UniversalIconLoader = async (
|
|||||||
|
|
||||||
const iconSet = await loadCollectionFromFS(
|
const iconSet = await loadCollectionFromFS(
|
||||||
collection,
|
collection,
|
||||||
options?.autoInstall
|
options?.autoInstall,
|
||||||
|
undefined,
|
||||||
|
options?.cwd
|
||||||
);
|
);
|
||||||
if (iconSet) {
|
if (iconSet) {
|
||||||
result = await searchForIcon(
|
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`.
|
* If you need that properties just add an empty object here, useful for example when using the `svg` on `CSS`.
|
||||||
*/
|
*/
|
||||||
usedProps?: Record<string, string>;
|
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