mirror of
https://github.com/iconify/iconify.git
synced 2024-12-13 14:13:06 +00:00
chore: remove node
stuff
This commit is contained in:
parent
6134d1f62b
commit
7dd250ef55
@ -124,22 +124,10 @@
|
||||
"require": "./lib/loader/custom.js",
|
||||
"import": "./lib/loader/custom.mjs"
|
||||
},
|
||||
"./lib/loader/fs": {
|
||||
"require": "./lib/loader/fs.js",
|
||||
"import": "./lib/loader/fs.mjs"
|
||||
},
|
||||
"./lib/loader/install-pkg": {
|
||||
"require": "./lib/loader/install-pkg.js",
|
||||
"import": "./lib/loader/install-pkg.mjs"
|
||||
},
|
||||
"./lib/loader/loader": {
|
||||
"require": "./lib/loader/loader.js",
|
||||
"import": "./lib/loader/loader.mjs"
|
||||
},
|
||||
"./lib/loader/loaders": {
|
||||
"require": "./lib/loader/loaders.js",
|
||||
"import": "./lib/loader/loaders.mjs"
|
||||
},
|
||||
"./lib/loader/modern": {
|
||||
"require": "./lib/loader/modern.js",
|
||||
"import": "./lib/loader/modern.mjs"
|
||||
@ -152,10 +140,6 @@
|
||||
"require": "./lib/loader/utils.js",
|
||||
"import": "./lib/loader/utils.mjs"
|
||||
},
|
||||
"./lib/loader/warn": {
|
||||
"require": "./lib/loader/warn.js",
|
||||
"import": "./lib/loader/warn.mjs"
|
||||
},
|
||||
"./lib/misc/strings": {
|
||||
"require": "./lib/misc/strings.js",
|
||||
"import": "./lib/misc/strings.mjs"
|
||||
|
@ -55,7 +55,6 @@ export type {
|
||||
InlineCollection,
|
||||
} from './loader/types';
|
||||
export { mergeIconProps } from './loader/utils';
|
||||
export { warnOnce } from './loader/warn';
|
||||
export { getCustomIcon } from './loader/custom';
|
||||
export { searchForIcon } from './loader/modern';
|
||||
|
||||
|
@ -1,39 +0,0 @@
|
||||
import { promises as fs, Stats } from 'fs';
|
||||
import { isPackageExists, resolveModule } from 'local-pkg'
|
||||
import type { IconifyJSON } from '@iconify/types'
|
||||
import { tryInstallPkg } from './install-pkg';
|
||||
|
||||
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
|
||||
const isLegacyExists = isPackageExists('@iconify/json');
|
||||
|
||||
export async function loadCollectionFromFS(name: string, autoInstall = false): Promise<IconifyJSON | undefined> {
|
||||
if (!_collections[name]) {
|
||||
_collections[name] = task();
|
||||
}
|
||||
return _collections[name];
|
||||
|
||||
async function task() {
|
||||
let jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
||||
if (!jsonPath && isLegacyExists) {
|
||||
jsonPath = resolveModule(`@iconify/json/json/${name}.json`);
|
||||
}
|
||||
|
||||
if (!jsonPath && !isLegacyExists && autoInstall) {
|
||||
await tryInstallPkg(`@iconify-json/${name}`);
|
||||
jsonPath = resolveModule(`@iconify-json/${name}/icons.json`);
|
||||
}
|
||||
|
||||
let stat: Stats | undefined;
|
||||
try {
|
||||
stat = jsonPath ? await fs.lstat(jsonPath) : undefined;
|
||||
} catch (err) {
|
||||
return undefined;
|
||||
}
|
||||
if (stat && stat.isFile()) {
|
||||
return JSON.parse(await fs.readFile(jsonPath as string, 'utf8')) as IconifyJSON;
|
||||
}
|
||||
else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
import { installPackage } from '@antfu/install-pkg';
|
||||
import { sleep } from '@antfu/utils';
|
||||
import { cyan } from 'kolorist';
|
||||
import { warnOnce } from './warn';
|
||||
|
||||
let pending: Promise<void> | undefined;
|
||||
const tasks: Record<string, Promise<void> | undefined> = {};
|
||||
|
||||
export async function tryInstallPkg(name: string): Promise<void | undefined> {
|
||||
if (pending) {
|
||||
await pending;
|
||||
}
|
||||
|
||||
if (!tasks[name]) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(cyan(`Installing ${name}...`));
|
||||
tasks[name] = pending = installPackage(name, {
|
||||
dev: true,
|
||||
preferOffline: true,
|
||||
})
|
||||
.then(() => sleep(300))
|
||||
// eslint-disable-next-line
|
||||
.catch((e: any) => {
|
||||
warnOnce(`Failed to install ${name}`);
|
||||
console.error(e);
|
||||
})
|
||||
.finally(() => {
|
||||
pending = undefined;
|
||||
});
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
return tasks[name]!;
|
||||
}
|
@ -1,10 +1,6 @@
|
||||
import { getCustomIcon } from './custom';
|
||||
import { searchForIcon } from './modern';
|
||||
import { warnOnce } from './warn';
|
||||
import type { IconifyLoaderOptions } from './types';
|
||||
|
||||
export const isNode = typeof process < 'u' && typeof process.stdout < 'u';
|
||||
|
||||
export async function loadIcon(
|
||||
collection: string,
|
||||
icon: string,
|
||||
@ -19,52 +15,7 @@ export async function loadIcon(
|
||||
}
|
||||
}
|
||||
|
||||
if (!isNode) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return await loadNodeBuiltinIcon(collection, icon, options);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function importFsModule(): Promise<typeof import('./fs') | undefined> {
|
||||
try {
|
||||
return await import('./fs');
|
||||
} catch {
|
||||
try {
|
||||
// cjs environments
|
||||
return require('./fs.js');
|
||||
}
|
||||
catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function loadNodeBuiltinIcon(
|
||||
collection: string,
|
||||
icon: string,
|
||||
options?: IconifyLoaderOptions,
|
||||
): Promise<string | undefined> {
|
||||
let result: string | undefined;
|
||||
const loadCollectionFromFS = await importFsModule().then(i => i?.loadCollectionFromFS);
|
||||
if (loadCollectionFromFS) {
|
||||
const iconSet = await loadCollectionFromFS(collection, options?.autoInstall);
|
||||
if (iconSet) {
|
||||
// possible icon names
|
||||
const ids = [
|
||||
icon,
|
||||
icon.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(),
|
||||
icon.replace(/([a-z])(\d+)/g, '$1-$2'),
|
||||
];
|
||||
result = await searchForIcon(iconSet, collection, ids, options);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!result && options?.warn) {
|
||||
warnOnce(`failed to load ${options.warn} icon`);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1,34 +0,0 @@
|
||||
import type { Awaitable } from '@antfu/utils';
|
||||
import { promises as fs, Stats } from 'fs';
|
||||
import type { CustomIconLoader } from './types';
|
||||
import { camelize, pascalize } from '../misc/strings';
|
||||
|
||||
/**
|
||||
* Returns CustomIconLoader for loading icons from a directory
|
||||
*/
|
||||
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 {
|
||||
continue;
|
||||
}
|
||||
if (stat.isFile()) {
|
||||
const svg = await fs.readFile(path, 'utf-8');
|
||||
return typeof transform === 'function'
|
||||
? await transform(svg)
|
||||
: svg;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
import { yellow } from 'kolorist';
|
||||
|
||||
const warned = new Set<string>();
|
||||
|
||||
export function warnOnce(msg: string): void {
|
||||
if (!warned.has(msg)) {
|
||||
warned.add(msg);
|
||||
console.warn(yellow(`[@iconify-loader] ${msg}`));
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
import { FileSystemIconLoader } from '../lib/loader/loaders';
|
||||
|
||||
describe('Testing FileSystemIconLoader', () => {
|
||||
test('FileSystemIconLoader', async () => {
|
||||
const result = await FileSystemIconLoader(__dirname + '/fixtures')(
|
||||
'circle'
|
||||
);
|
||||
expect(result && result.indexOf('svg') > -1).toBeTruthy();
|
||||
});
|
||||
|
||||
test('FileSystemIconLoader with transform', async () => {
|
||||
const result = await FileSystemIconLoader(
|
||||
__dirname + '/fixtures',
|
||||
(icon) => {
|
||||
return icon.replace('<svg ', '<svg width="1em" height="1em" ');
|
||||
}
|
||||
)('circle');
|
||||
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
|
||||
expect(result && result.indexOf('height="1em"') > -1).toBeTruthy();
|
||||
});
|
||||
});
|
@ -1,116 +0,0 @@
|
||||
import { loadIcon } from '../lib/loader/loader';
|
||||
|
||||
describe('Testing loadIcon with @iconify-json/flat-color-icons>', () => {
|
||||
|
||||
test('loadIcon works', async () => {
|
||||
const result = await loadIcon('flat-color-icons', 'up-right');
|
||||
expect(result).toBeTruthy();
|
||||
});
|
||||
|
||||
test('loadIcon adds xmlns:xlink', async () => {
|
||||
const result = await loadIcon('flat-color-icons', 'up-right', { addXmlNs: true });
|
||||
expect(result).toBeTruthy();
|
||||
expect(result && result.indexOf('xmlns:xlink=') > - 1).toBeTruthy();
|
||||
});
|
||||
|
||||
test('loadIcon with customize with default style and class', async () => {
|
||||
const result = await loadIcon('flat-color-icons', 'up-right', {
|
||||
defaultStyle: 'margin-top: 1rem;',
|
||||
defaultClass: 'clazz',
|
||||
customizations: {
|
||||
customize(props) {
|
||||
props.width = '2em';
|
||||
props.height = '2em';
|
||||
return props;
|
||||
},
|
||||
}
|
||||
});
|
||||
expect(result).toBeTruthy();
|
||||
expect(result && result.indexOf('margin-top: 1rem;') > - 1).toBeTruthy();
|
||||
expect(result && result.indexOf('class="clazz"') > - 1).toBeTruthy();
|
||||
expect(result && result.indexOf('width="2em"') > - 1).toBeTruthy();
|
||||
expect(result && result.indexOf('height="2em"') > - 1).toBeTruthy();
|
||||
});
|
||||
|
||||
test('loadIcon preserves customizations order', async () => {
|
||||
const result = await loadIcon('flat-color-icons', 'up-right', {
|
||||
scale: 1,
|
||||
defaultStyle: 'color: red;',
|
||||
defaultClass: 'clazz1',
|
||||
customizations: {
|
||||
additionalProps: {
|
||||
'width': '2em',
|
||||
'height': '2em',
|
||||
'style': 'color: blue;',
|
||||
'class': 'clazz2',
|
||||
},
|
||||
// it will never be called, it is not a custom icon
|
||||
transform(icon) {
|
||||
return icon.replace('<svg ', '<svg width="4em" height="4em" ');
|
||||
},
|
||||
}
|
||||
});
|
||||
expect(result).toBeTruthy();
|
||||
expect(result && result.includes('style="color: blue;"')).toBeTruthy();
|
||||
expect(result && result.includes('class="clazz2"')).toBeTruthy();
|
||||
expect(result && result.includes('width="2em"')).toBeTruthy();
|
||||
expect(result && result.includes('height="2em"')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('loadIcon warn missing icon', async () => {
|
||||
// Intercept console.warn
|
||||
let warned = false;
|
||||
const warn = console.warn;
|
||||
console.warn = (/*...args*/) => {
|
||||
// warn.apply(this, args);
|
||||
warned = true;
|
||||
};
|
||||
|
||||
const result = await loadIcon('flat-color-icons', 'missing1', {
|
||||
warn: 'flat-color-icons:missing'
|
||||
});
|
||||
// Restore console.warn
|
||||
console.warn = warn;
|
||||
|
||||
expect(result).toBeFalsy();
|
||||
expect(warned).toEqual(true);
|
||||
});
|
||||
|
||||
test('test warnOnce on loadIcon on missing icon', async () => {
|
||||
// Intercept console.warn
|
||||
let warned = false;
|
||||
const warn = console.warn;
|
||||
console.warn = (/*...args*/) => {
|
||||
// warn.apply(this, args);
|
||||
warned = true;
|
||||
};
|
||||
|
||||
// use another name since it is using warnOnce
|
||||
const result = await loadIcon('flat-color-icons', 'missing1', {
|
||||
warn: 'flat-color-icons:missing'
|
||||
});
|
||||
// Restore console.warn
|
||||
console.warn = warn;
|
||||
|
||||
expect(result).toBeFalsy();
|
||||
expect(warned).toEqual(false);
|
||||
});
|
||||
|
||||
test('loadIcon doesn\'t warn missing icon', async () => {
|
||||
// Intercept console.warn
|
||||
let warned = false;
|
||||
const warn = console.warn;
|
||||
console.warn = (/*...args*/) => {
|
||||
// warn.apply(this, args);
|
||||
warned = true;
|
||||
};
|
||||
|
||||
// use another name since it is using warnOnce
|
||||
const result = await loadIcon('flat-color-icons', 'missing2');
|
||||
// Restore console.warn
|
||||
console.warn = warn;
|
||||
|
||||
expect(result).toBeFalsy();
|
||||
expect(warned).toEqual(false);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user