2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-13 14:13:06 +00:00

chore: move node stuff + package.json changes

This commit is contained in:
Joaquín Sánchez Jiménez 2022-03-03 22:30:16 +01:00
parent 0a8a0c9a23
commit 7ba8d5a2be
10 changed files with 97 additions and 85 deletions

View File

@ -25,8 +25,9 @@ export default defineBuildConfig({
{ input: 'src/loader/fs', name: 'loader/fs' },
{ input: 'src/loader/install-pkg', name: 'loader/install-pkg' },
{ input: 'src/loader/loader', name: 'loader/loader' },
{ input: 'src/loader/loaders', name: 'loader/loaders' },
{ input: 'src/loader/modern', name: 'loader/modern' },
{ input: 'src/loader/node-loader', name: 'loader/node-loader' },
{ input: 'src/loader/node-loaders', name: 'loader/node-loaders' },
{ input: 'src/loader/types', name: 'loader/types' },
{ input: 'src/loader/utils', name: 'loader/utils' },
{ input: 'src/loader/warn', name: 'loader/warn' },

View File

@ -0,0 +1,7 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/tests/*-test.ts'],
};

View File

@ -0,0 +1,7 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/tests/*-test.ts'],
};

View File

@ -22,9 +22,8 @@
"test:jasmine": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jasmine",
"test": "npm run test:jest && npm run test:jasmine"
},
"sideEffects": "false",
"sideEffects": false,
"exports": {
"./*": "./*",
".": {
"require": "./lib/index.cjs",
"import": "./lib/index.mjs"
@ -139,15 +138,20 @@
"require": "./lib/loader/loader.cjs",
"import": "./lib/loader/loader.mjs"
},
"./lib/loader/loaders": {
"require": "./lib/loader/loaders.cjs",
"import": "./lib/loader/loaders.mjs",
"types": "./lib/loader/loaders.d.ts"
},
"./lib/loader/modern": {
"require": "./lib/loader/modern.cjs",
"import": "./lib/loader/modern.mjs"
},
"./lib/loader/node-loader": {
"require": "./lib/loader/node-loader.cjs",
"import": "./lib/loader/node-loader.mjs",
"types": "./lib/loader/node-loader.d.ts"
},
"./lib/loader/node-loaders": {
"require": "./lib/loader/node-loaders.cjs",
"import": "./lib/loader/node-loaders.mjs",
"types": "./lib/loader/node-loaders.d.ts"
},
"./lib/loader/types": {
"require": "./lib/loader/types.cjs",
"import": "./lib/loader/types.mjs"
@ -180,11 +184,23 @@
"./lib/svg/size": {
"require": "./lib/svg/size.cjs",
"import": "./lib/svg/size.mjs"
}
},
"main": "./lib/index.cjs",
"module": "./lib/index.mjs",
"types": "./lib/index.d.ts",
"./*": "./*"
},
"main": "lib/index.cjs",
"module": "lib/index.mjs",
"types": "lib/index.d.ts",
"files": [
"lib",
"lib/colors",
"lib/customisations",
"lib/icon",
"lib/icon-set",
"lib/loader",
"lib/misc",
"lib/svg",
"*.d.ts"
],
"dependencies": {
"@antfu/install-pkg": "^0.1.0",
"@antfu/utils": "^0.3.0",

View File

@ -53,11 +53,12 @@ export type {
IconCustomizations,
IconifyLoaderOptions,
InlineCollection,
UniversalIconLoader,
} from './loader/types';
export { mergeIconProps } from './loader/utils';
export { getCustomIcon } from './loader/custom';
export { searchForIcon } from './loader/modern';
export { isNode, loadIcon } from './loader/loader';
export { loadIcon } from './loader/loader';
// Misc
export { camelize, camelToKebab, snakelize, pascalize } from './misc/strings';

View File

@ -1,14 +1,12 @@
import { getCustomIcon } from './custom';
import type { IconifyLoaderOptions } from './types';
import type { UniversalIconLoader } from './types';
import { searchForIcon } from './modern';
export const isNode = typeof process < 'u' && typeof process.stdout < 'u';
export async function loadIcon(
collection: string,
icon: string,
options?: IconifyLoaderOptions
): Promise<string | undefined> {
export const loadIcon: UniversalIconLoader = async(
collection,
icon,
options
) => {
const custom = options?.customCollections?.[collection];
if (custom) {
@ -35,67 +33,5 @@ export async function loadIcon(
}
}
if (!isNode) {
return undefined;
}
return await loadNodeBuiltinIcon(collection, icon, options);
}
async function importFsModule(): Promise<typeof import('./fs') | undefined> {
try {
return await import('./fs');
} catch {
try {
// cjs environments
return require('./fs.cjs');
}
catch {
return undefined;
}
}
}
async function importWarnModule(): Promise<typeof import('./warn') | undefined> {
try {
return await import('./warn');
} catch {
try {
// cjs environments
return require('./warn.cjs');
}
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) {
const warnOnce = await importWarnModule().then(i => i?.warnOnce);
warnOnce?.(`failed to load ${options.warn} icon`);
}
return result;
}

View File

@ -0,0 +1,34 @@
import type { UniversalIconLoader } from './types';
import { searchForIcon } from './modern';
import { loadCollectionFromFS } from './fs';
import { warnOnce } from './warn';
import { loadIcon } from './loader';
export const loadNodeIcon: UniversalIconLoader = async(
collection,
icon,
options,
) => {
let result = await loadIcon(collection, icon, options);
if (result) {
return result;
}
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;
}

View File

@ -2,6 +2,15 @@ import type { Awaitable } from '@antfu/utils';
import type { FullIconCustomisations } from '../customisations';
import type { IconifyJSON } from '@iconify/types';
/**
* Type for universal icon loader.
*/
export type UniversalIconLoader = (
collection: string,
icon: string,
options?: IconifyLoaderOptions
) => Promise<string | undefined>;
/**
* Custom icon loader, used by `getCustomIcon`.
*/

View File

@ -9,10 +9,11 @@
"DOM"
],
"strict": true,
"skipLibCheck": true,
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"importsNotUsedAsValues": "error"
},
"include": ["./src/**/*.ts"]
"include": ["src/**/*.ts"]
}