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
9,422 changes: 5,136 additions & 4,286 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
"prepack": "nuxt-module-build",
"dev": "nuxi dev playground",
"dev:build": "nuxi build playground",
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
"lint": "nuxi typecheck && eslint ."
},
"dependencies": {
"@nuxt/kit": "^3.0.0"
"@nuxt/kit": "^3.0.0",
"glob": "^10.2.2",
"mkdirp": "^3.0.1"
},
"devDependencies": {
"@nuxt/module-builder": "^0.2.1",
Expand Down
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineNuxtConfig({
modules: [
NuxtIcons
],
NuxtIcons: {
nuxtIcons: {
addPlugin: true
}
})
45 changes: 45 additions & 0 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Nuxt } from '@nuxt/schema/dist/index'
import chalk from 'chalk'
import { saveGeneratedFile } from './output'
import { constructIconNames } from './parser'

type CreateTypedIconsArgs = {
nuxt: Nuxt;
location: string | null;
fileExtension: string;
isHookCall?: boolean;
};

export async function CreateTypedIcons ({
nuxt,
location,
fileExtension,
isHookCall = false
}: CreateTypedIconsArgs): Promise<void> {
try {
if (!isHookCall) {
if (location) {
nuxt.hook('builder:watch', (event, watchedPath) => {
if (watchedPath.startsWith(location)) {
CreateTypedIcons({ nuxt, location, fileExtension, isHookCall: true })
}
})
}

nuxt.hook('modules:done', () => {
CreateTypedIcons({ nuxt, location, fileExtension, isHookCall: true })
})

return
}

if (location) {
const iconNames = await constructIconNames(nuxt.options.rootDir, location, fileExtension)
await saveGeneratedFile(nuxt, iconNames)
} else {
await saveGeneratedFile(nuxt, null)
}
} catch (e) {
console.error(chalk.red('Error while generating icons definition model'), '\n' + e)
}
}
22 changes: 22 additions & 0 deletions src/generator/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Nuxt } from '@nuxt/schema/dist/index'
import { addTemplate } from '@nuxt/kit'

export const saveGeneratedFile = (nuxt: Nuxt, iconNames: string[] | null) => {
addTemplate({
filename: 'nuxt-icons.d.ts',
getContents: () => {
return `// @ts-nocheck
// eslint-disable
/**
* ---------------------------------------------------
* 🚗 Generated by nuxt-icons. Do not modify !
* ---------------------------------------------------
* */

declare module '#app' {
type NuxtIconName = ${iconNames === null ? 'string' : iconNames.map(p => `"${p.replace(/[\\"]/g, '\\$&')}"`).join(' | ')}
}
`
}
})
}
3 changes: 3 additions & 0 deletions src/generator/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { glob } from 'glob'

export const constructIconNames = async (path: string, location: string, fileExtension: string) => (await glob(`${location}/**/**.${fileExtension}`, { cwd: path })).map(p => p.substring(location.length + 1)).map(p => p.substring(0, p.length - 4))
21 changes: 21 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { defineNuxtModule, createResolver, addComponent } from '@nuxt/kit'
import { CreateTypedIcons } from './generator'

export interface ModuleOptions {
// dir: string
/**
* Enables path autocomplete and path validity for programmatic validation
*
* @default true
*/
pathCheck?: boolean;
}

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -12,12 +19,26 @@ export default defineNuxtModule<ModuleOptions>({
nuxt: '^3.0.0'
}
},
defaults: {
pathCheck: true
},
setup (options, nuxt) {
const { resolve } = createResolver(import.meta.url)
addComponent({
name: 'nuxt-icon',
global: true,
filePath: resolve('./runtime/components/nuxt-icon.vue')
})

// Force register of type declaration
nuxt.hook('prepare:types', ({ references }) => {
references.push({ path: resolve(nuxt.options.buildDir, 'nuxt-icons.d.ts') })
})

if (options.pathCheck) {
CreateTypedIcons({ nuxt, location: 'assets/icons', fileExtension: 'svg' })
} else {
CreateTypedIcons({ nuxt, location: null, fileExtension: '' })
}
}
})
11 changes: 6 additions & 5 deletions src/runtime/components/nuxt-icon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
</template>

<script setup lang="ts">
import { NuxtIconName } from '#app'
import { ref, watchEffect } from '#imports'

const props = withDefaults(defineProps<{
name: string;
name: NuxtIconName;
filled?: boolean
}>(), { filled: false })

Expand All @@ -24,11 +25,11 @@ async function getIcon () {
as: 'raw',
eager: false
})
const rawIcon = await iconsImport[`/assets/icons/${props.name}.svg`]()
if (rawIcon.includes('stroke')) { hasStroke = true }
icon.value = rawIcon
const rawIcon = await iconsImport[`/assets/icons/${props.name}.svg`]?.()
if (rawIcon?.includes('stroke')) { hasStroke = true }
icon.value = rawIcon || ''
} catch {
console.error(
throw new Error(
`[nuxt-icons] Icon '${props.name}' doesn't exist in 'assets/icons'`
)
}
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "./playground/.nuxt/tsconfig.json"
"extends": "./playground/.nuxt/tsconfig.json",
"compilerOptions": {
"noUncheckedIndexedAccess": true
}
}