Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function registerServerBundle(
return ` '${collection}': createRemoteCollection(${JSON.stringify(getRemoteEndpoint(collection))}),`
}

const path = getCollectionPath(collection)
const path = getCollectionPath(collection, nuxt)

// When in dev mode, we avoid bundling the icons to improve performance
// Get rid of the require() when ESM JSON modules are widely supported
Expand Down
26 changes: 18 additions & 8 deletions src/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ import { isPackageExists } from 'local-pkg'
import { collectionNames } from './collection-names'
import type { CustomCollection, ServerBundleOptions, RemoteCollection } from './types'

export const isFullCollectionExists = isPackageExists('@iconify/json')
function getResolvePaths(nuxt: Nuxt): string[] {
return Array.from(new Set(
[nuxt.options.rootDir, nuxt.options.workspaceDir].filter(Boolean),
))
}

export function hasFullCollection(nuxt: Nuxt): boolean {
return isPackageExists('@iconify/json', { paths: getResolvePaths(nuxt) })
}

export async function resolveCollection(
nuxt: Nuxt,
Expand All @@ -24,8 +32,8 @@ export async function resolveCollection(
return collection
}

export function getCollectionPath(collection: string) {
return isFullCollectionExists
export function getCollectionPath(collection: string, nuxt: Nuxt) {
return hasFullCollection(nuxt)
? `@iconify/json/json/${collection}.json`
: `@iconify-json/${collection}/icons.json`
}
Expand Down Expand Up @@ -117,16 +125,18 @@ async function parseCustomCollection(
return result
}

export async function discoverInstalledCollections(): Promise<ServerBundleOptions['collections']> {
const collections = isFullCollectionExists
export async function discoverInstalledCollections(nuxt: Nuxt): Promise<ServerBundleOptions['collections']> {
const paths = getResolvePaths(nuxt)
const fullCollectionInstalled = hasFullCollection(nuxt)
const collections = fullCollectionInstalled
? collectionNames
: collectionNames.filter(collection => isPackageExists('@iconify-json/' + collection))
if (isFullCollectionExists)
: collectionNames.filter(collection => isPackageExists('@iconify-json/' + collection, { paths }))
if (fullCollectionInstalled)
logger.success(`Nuxt Icon discovered local-installed ${collections.length} collections (@iconify/json)`)
else if (collections.length)
logger.success(`Nuxt Icon discovered local-installed ${collections.length} collections:`, collections.join(', '))

if (isFullCollectionExists)
if (fullCollectionInstalled)
logger.warn('Currently all iconify collections are included in the bundle, which might be inefficient, consider explicit name the collections you use in the `icon.serverBundle.collections` option')

return collections
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class NuxtIconModuleContext {
if (!resolved.collections)
resolved.collections = resolved.remote
? collectionNames
: await discoverInstalledCollections()
: await discoverInstalledCollections(this.nuxt)

const collections = await Promise.all(
(resolved.collections || [])
Expand Down
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default defineNuxtModule<ModuleOptions>({

const collections = bundle.collections
.filter(collection => typeof collection === 'string')
.map(collection => getCollectionPath(collection))
.map(collection => getCollectionPath(collection, nuxt))
const resolvedPaths = await Promise.all(
collections.map(collection => resolvePath(collection, {
url: nuxt.options.rootDir,
Expand Down
Loading