close
Skip to content

Commit 90fafa3

Browse files
fix: detect installed collections from Nuxt rootDir/workspaceDir (#477)
1 parent 50c3b35 commit 90fafa3

4 files changed

Lines changed: 21 additions & 11 deletions

File tree

‎src/bundle-server.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function registerServerBundle(
4848
return ` '${collection}': createRemoteCollection(${JSON.stringify(getRemoteEndpoint(collection))}),`
4949
}
5050

51-
const path = getCollectionPath(collection)
51+
const path = getCollectionPath(collection, nuxt)
5252

5353
// When in dev mode, we avoid bundling the icons to improve performance
5454
// Get rid of the require() when ESM JSON modules are widely supported

‎src/collections.ts‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ import { isPackageExists } from 'local-pkg'
99
import { collectionNames } from './collection-names'
1010
import type { CustomCollection, ServerBundleOptions, RemoteCollection } from './types'
1111

12-
export const isFullCollectionExists = isPackageExists('@iconify/json')
12+
function getResolvePaths(nuxt: Nuxt): string[] {
13+
return Array.from(new Set(
14+
[nuxt.options.rootDir, nuxt.options.workspaceDir].filter(Boolean),
15+
))
16+
}
17+
18+
export function hasFullCollection(nuxt: Nuxt): boolean {
19+
return isPackageExists('@iconify/json', { paths: getResolvePaths(nuxt) })
20+
}
1321

1422
export async function resolveCollection(
1523
nuxt: Nuxt,
@@ -24,8 +32,8 @@ export async function resolveCollection(
2432
return collection
2533
}
2634

27-
export function getCollectionPath(collection: string) {
28-
return isFullCollectionExists
35+
export function getCollectionPath(collection: string, nuxt: Nuxt) {
36+
return hasFullCollection(nuxt)
2937
? `@iconify/json/json/${collection}.json`
3038
: `@iconify-json/${collection}/icons.json`
3139
}
@@ -117,16 +125,18 @@ async function parseCustomCollection(
117125
return result
118126
}
119127

120-
export async function discoverInstalledCollections(): Promise<ServerBundleOptions['collections']> {
121-
const collections = isFullCollectionExists
128+
export async function discoverInstalledCollections(nuxt: Nuxt): Promise<ServerBundleOptions['collections']> {
129+
const paths = getResolvePaths(nuxt)
130+
const fullCollectionInstalled = hasFullCollection(nuxt)
131+
const collections = fullCollectionInstalled
122132
? collectionNames
123-
: collectionNames.filter(collection => isPackageExists('@iconify-json/' + collection))
124-
if (isFullCollectionExists)
133+
: collectionNames.filter(collection => isPackageExists('@iconify-json/' + collection, { paths }))
134+
if (fullCollectionInstalled)
125135
logger.success(`Nuxt Icon discovered local-installed ${collections.length} collections (@iconify/json)`)
126136
else if (collections.length)
127137
logger.success(`Nuxt Icon discovered local-installed ${collections.length} collections:`, collections.join(', '))
128138

129-
if (isFullCollectionExists)
139+
if (fullCollectionInstalled)
130140
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')
131141

132142
return collections

‎src/context.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class NuxtIconModuleContext {
100100
if (!resolved.collections)
101101
resolved.collections = resolved.remote
102102
? collectionNames
103-
: await discoverInstalledCollections()
103+
: await discoverInstalledCollections(this.nuxt)
104104

105105
const collections = await Promise.all(
106106
(resolved.collections || [])

‎src/module.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export default defineNuxtModule<ModuleOptions>({
121121

122122
const collections = bundle.collections
123123
.filter(collection => typeof collection === 'string')
124-
.map(collection => getCollectionPath(collection))
124+
.map(collection => getCollectionPath(collection, nuxt))
125125
const resolvedPaths = await Promise.all(
126126
collections.map(collection => resolvePath(collection, {
127127
url: nuxt.options.rootDir,

0 commit comments

Comments
 (0)