mirror of
https://github.com/iconify/iconify.git
synced 2024-12-13 14:13:06 +00:00
feat(next): split modern loader for node and browser support
This commit is contained in:
parent
65dfecf963
commit
062a852a79
@ -2,6 +2,7 @@ import type { Awaitable } from '@antfu/utils';
|
|||||||
import { promises as fs, Stats } from 'fs';
|
import { promises as fs, Stats } from 'fs';
|
||||||
import type { CustomIconLoader } from './types';
|
import type { CustomIconLoader } from './types';
|
||||||
import { camelize, pascalize } from '../misc/strings';
|
import { camelize, pascalize } from '../misc/strings';
|
||||||
|
import { isNode } from './loader';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns CustomIconLoader for loading icons from a directory
|
* Returns CustomIconLoader for loading icons from a directory
|
||||||
@ -10,25 +11,26 @@ export function FileSystemIconLoader(
|
|||||||
dir: string,
|
dir: string,
|
||||||
transform?: (svg: string) => Awaitable<string>
|
transform?: (svg: string) => Awaitable<string>
|
||||||
): CustomIconLoader {
|
): CustomIconLoader {
|
||||||
return async (name) => {
|
return isNode
|
||||||
const paths = [
|
? async (name) => {
|
||||||
`${dir}/${name}.svg`,
|
const paths = [
|
||||||
`${dir}/${camelize(name)}.svg`,
|
`${dir}/${name}.svg`,
|
||||||
`${dir}/${pascalize(name)}.svg`,
|
`${dir}/${camelize(name)}.svg`,
|
||||||
];
|
`${dir}/${pascalize(name)}.svg`,
|
||||||
let stat: Stats;
|
];
|
||||||
for (const path of paths) {
|
let stat: Stats;
|
||||||
try {
|
for (const path of paths) {
|
||||||
stat = await fs.lstat(path);
|
try {
|
||||||
} catch (err) {
|
stat = await fs.lstat(path);
|
||||||
continue;
|
} catch (err) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (stat.isFile()) {
|
||||||
|
const svg = await fs.readFile(path, 'utf-8');
|
||||||
|
return typeof transform === 'function'
|
||||||
|
? await transform(svg)
|
||||||
|
: svg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (stat.isFile()) {
|
} : () => undefined;
|
||||||
const svg = await fs.readFile(path, 'utf-8');
|
|
||||||
return typeof transform === 'function'
|
|
||||||
? await transform(svg)
|
|
||||||
: svg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user