Install nuxt-svgo
mainTo add nuxt-svgo to your Nuxt project, use the Nuxt module add command:
npx nuxi@latest module add nuxt-svgorepository·main·Indexed 41 days ago
https://github.com/cpsoinos/nuxt-svgoNuxt module version 4.0.2 that automatically transforms and optimizes SVG files using SVGO, allowing them to be used as Vue components or optimized assets. Supports Vite import queries for loading SVGs as data URIs, raw strings, or components, and provides configurable auto-import paths for icons.
To add nuxt-svgo to your Nuxt project, use the Nuxt module add command:
npx nuxi@latest module add nuxt-svgoAfter installing, add 'nuxt-svgo' to your modules array in nuxt.config.ts. You can then use SVGs in two ways:
.vue file and use it as a component.svgo prefix (e.g., SvgoHome or <svgo-home />). By default, it scans assets/icons/.Note: By default, font size controls the width and height of the icon. You can disable this using the :fontControlled="false" prop.
// nuxt.config.ts
import { defineNuxtConfig } from 'nuxt'
export default defineNuxtConfig({
modules: ['nuxt-svgo'],
})
// In a .vue file
<template>
<div>
<IconHome class="text-xl" />
<IconHome class="w-5 h-5" :fontControlled="false" />
</div>
</template>
<script setup lang="ts">
import IconHome from '~/assets/icon-home.svg'
</script>You can specify which directories are scanned for icons using the autoImportPath option in the svgo configuration. This accepts a single string path or an array of paths. All configured paths are scanned in the root app and in every Nuxt layer.
To disable auto-importing entirely, set autoImportPath to false.
// Single path
export default defineNuxtConfig({
modules: ['nuxt-svgo'],
svgo: {
autoImportPath: './assets/other-icons/',
},
})
// Multiple paths
export default defineNuxtConfig({
modules: ['nuxt-svgo'],
svgo: {
autoImportPath: ['./assets/icons/', './assets/shapes/'],
},
})
// Disable auto-import
export default defineNuxtConfig({
modules: ['nuxt-svgo'],
svgo: {
autoImportPath: false,
},
})When using Vite, you can enable TypeScript declaration generation for SVG import queries by setting dts: true in your svgo configuration. This ensures that queries like import icon from './icon.svg?component' are correctly typed.
// nuxt.config.ts
export default defineNuxtConfig({
svgo: {
dts: true
}
})export default defineNuxtConfig({
svgo: {
dts: true
}
})When configuring the SVG loader, you can provide an options object of type SvgLoaderOptions to control how SVGs are processed and imported.
Key options include:
customComponent: The name of the component (in CapitalCase) used when using the componentext import type. Defaults to NuxtIcon.defaultImport: The fallback import type if no query is provided. Options: 'url', 'url_encode', 'raw', 'raw_optimized', 'component', 'skipsvgo', or 'componentext'.autoImportPath: A string, array of strings, or false. If provided, SVGs within these paths are automatically processed. If false, only explicit imports are handled.explicitImportsOnly: If true, the loader only works if an import query is explicitly used, unless the SVG is within an autoImportPath.svgo: Boolean. Enables or disables SVGO optimization. Defaults to true (implied by logic).svgoConfig: An object of type Config from svgo to customize the optimization process.export interface SvgLoaderOptions {
autoImportPath?: string | string[] | false
customComponent: string
defaultImport?:
| 'url'
| 'url_encode'
| 'raw'
| 'raw_optimized'
| 'component'
| 'skipsvgo'
| 'componentext'
explicitImportsOnly?: boolean
svgo?: boolean
svgoConfig?: Config
}The nuxt-svgo module is configured via the svgo key in your nuxt.config.ts. You can control how SVGs are loaded, how they are transformed via SVGO, and how components are auto-imported.
| Option | Type | Default | Description |
|---|---|---|---|
svgo | boolean | true | Whether to use SVGO for processing SVGs. |
defaultImport | string | 'componentext' | The default import query used for SVGs. |
autoImportPath | string | string[] | './assets/icons/' | Paths to directories containing SVGs that should be auto-imported as components. |
svgoConfig | Config | undefined | Custom SVGO configuration object. |
global | boolean | true | Whether auto-imported components should be registered globally. |
customComponent | string | 'NuxtIcon' | The name of the internal component used for rendering. |
componentPrefix | string | 'svgo' | The prefix applied to auto-imported SVG components. |
dts | boolean | false | Whether to generate TypeScript declaration files for SVG import queries (Vite only). |
export default defineNuxtConfig({
svgo: {
svgo: true,
componentPrefix: 'my-icon',
autoImportPath: ['./assets/icons', './assets/logos'],
svgoConfig: {
plugins: ['preset-default']
}
}
})You can append specific queries to your SVG import statements to change how the file is loaded. Supported queries include:
?url: Uses the default SVG loader (returns a URL).?url_encode: Returns the SVG as a data URI (encoded).?raw: Returns the raw SVG string.?raw_optimized: Returns the raw SVG string after SVGO optimization.?component: Returns the SVG as a Vue component.?skipsvgo: Returns the SVG without applying SVGO optimization.?componentext: Returns a wrapper component that uses the customComponent (e.g., NuxtIcon) to render the SVG via its icon slot/prop.import myIcon from './icon.svg?component'
import iconUrl from './icon.svg?url'
import iconRaw from './icon.svg?raw'
import iconDataUri from './icon.svg?url_encode'
import iconExt from './icon.svg?componentext'When using Vite, nuxt-svgo supports specific import queries to change how SVG files are loaded. To ensure TypeScript recognizes these imports, the module generates declaration files based on your ModuleOptions.
Depending on your configuration, the following query suffixes are supported:
?url_encode: Loads the optimized SVG as a data URI (using svgo and mini-svg-data-uri).?raw: Loads the raw SVG contents as a string.?raw_optimized: Loads the SVGO-optimized SVG contents as a string.?skipsvgo: Loads the SVG as a Vue component (unoptimized, without the customComponent wrapper).?component: Loads the optimized SVG as a Vue component.?componentext: Loads the optimized SVG as a Vue component that inherits props from your configured customComponent (via #components), but omits the icon prop.If explicitImportsOnly is set to true in your module configuration, TypeScript will only recognize these specific query suffixes and will not provide a default declaration for plain *.svg imports.
// Example of using different import queries in your code
import dataUri from '~/assets/logo.svg?url_encode';
import rawText from '~/assets/logo.svg?raw';
import OptimizedComponent from '~/assets/logo.svg?component';
import ExtendedComponent from '~/assets/logo.svg?componentext';If you do not provide a svgoConfig, the module uses a default configuration that includes the following plugins:
preset-defaultremoveDimensionsprefixIds: A custom plugin that prefixes IDs in the SVG using a hash of the file path to prevent ID collisions.export const defaultSvgoConfig: Config = {
plugins: [
'preset-default',
'removeDimensions',
{
name: 'prefixIds',
params: {
prefix(_, info) {
return 'i' + hashCode(info.path)
},
},
},
],
}export const defaultSvgoConfig: Config = {
plugins: [
'preset-default',
'removeDimensions',
{
name: 'prefixIds',
params: {
prefix(_, info) {
return 'i' + hashCode(info.path)
},
},
},
],
}