mirror of
https://github.com/iconify/iconify.git
synced 2024-12-12 21:57:50 +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 type { CustomIconLoader } from './types';
|
||||
import { camelize, pascalize } from '../misc/strings';
|
||||
import { isNode } from './loader';
|
||||
|
||||
/**
|
||||
* Returns CustomIconLoader for loading icons from a directory
|
||||
@ -10,25 +11,26 @@ export function FileSystemIconLoader(
|
||||
dir: string,
|
||||
transform?: (svg: string) => Awaitable<string>
|
||||
): CustomIconLoader {
|
||||
return async (name) => {
|
||||
const paths = [
|
||||
`${dir}/${name}.svg`,
|
||||
`${dir}/${camelize(name)}.svg`,
|
||||
`${dir}/${pascalize(name)}.svg`,
|
||||
];
|
||||
let stat: Stats;
|
||||
for (const path of paths) {
|
||||
try {
|
||||
stat = await fs.lstat(path);
|
||||
} catch (err) {
|
||||
continue;
|
||||
return isNode
|
||||
? async (name) => {
|
||||
const paths = [
|
||||
`${dir}/${name}.svg`,
|
||||
`${dir}/${camelize(name)}.svg`,
|
||||
`${dir}/${pascalize(name)}.svg`,
|
||||
];
|
||||
let stat: Stats;
|
||||
for (const path of paths) {
|
||||
try {
|
||||
stat = await fs.lstat(path);
|
||||
} 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()) {
|
||||
const svg = await fs.readFile(path, 'utf-8');
|
||||
return typeof transform === 'function'
|
||||
? await transform(svg)
|
||||
: svg;
|
||||
}
|
||||
}
|
||||
};
|
||||
} : () => undefined;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user