Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preset-icons): allow fallback to CDN loader in Node, close #1359
- Loading branch information
Showing
2 changed files
with
33 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,38 @@ | ||
import type { UniversalIconLoader } from '@iconify/utils' | ||
import { loadIcon } from '@iconify/utils' | ||
import { createCDNLoader } from './cdn' | ||
import { createPresetIcons } from './core' | ||
import { combineLoaders, createPresetIcons } from './core' | ||
import { isNode, isVSCode } from './utils' | ||
|
||
export * from './core' | ||
|
||
async function createNodeLoader() { | ||
try { | ||
return await import('@iconify/utils/lib/loader/node-loader').then(i => i?.loadNodeIcon) | ||
} | ||
catch { } | ||
try { | ||
return require('@iconify/utils/lib/loader/node-loader.cjs') | ||
} | ||
catch { } | ||
} | ||
|
||
export const presetIcons = createPresetIcons(async (options) => { | ||
const { | ||
cdn, | ||
} = options | ||
|
||
const loaders: UniversalIconLoader[] = [] | ||
|
||
if (isNode && !isVSCode) | ||
loaders.push(await createNodeLoader()) | ||
|
||
if (cdn) | ||
return createCDNLoader(cdn) | ||
|
||
if (isNode && !isVSCode) { | ||
try { | ||
return await import('@iconify/utils/lib/loader/node-loader').then(i => i?.loadNodeIcon) | ||
} | ||
catch {} | ||
try { | ||
return require('@iconify/utils/lib/loader/node-loader.cjs') | ||
} | ||
catch {} | ||
} | ||
return loadIcon | ||
loaders.push(createCDNLoader(cdn)) | ||
|
||
loaders.push(loadIcon) | ||
|
||
return combineLoaders(loaders) | ||
}) | ||
|
||
export default presetIcons |