SVG to Vue Component Compiler
svg-to-vue-component is a utility that compiles SVG files directly into Vue Single File Components (SFCs). This approach allows developers to import SVG assets as standard Vue components, enabling enhanced styling capabilities with CSS, direct application of DOM properties, and attachment of event handlers to the underlying SVG elements. The current stable version is 0.3.8, released in April 2019, indicating that the project is either abandoned or in a long-term maintenance state with no active development. Key differentiators include its built-in hot-reloading support via `vue-loader`, comprehensive support for all standard DOM props and events (unlike some alternatives that only support `class` and `style`), and flexible configuration options for SVGO, both project-wide and file-relative. It provides specific integration guides for Webpack, Vue CLI, Poi, and Nuxt.js 2 setups.
Common errors
-
Module not found: Error: Can't resolve 'path/to/icon.svg'
cause Webpack is unable to find or correctly process the SVG file due to an incorrect or missing rule configuration for `.svg` files, or conflicts with other SVG handling rules.fixVerify your webpack configuration for `.svg` files. Ensure the `test: /\.svg$/` rule is present, the `issuer: /\.(vue|js|ts|svg)$/` is correctly specified, and `vue-loader` is included in the `use` array before `svg-to-vue-component/loader`. -
Syntax Error: Unexpected token '<' (or similar parsing errors in old browsers like IE)
cause The SVG output generated by the loader might contain features or syntax not compatible with older browser engines, specifically mentioned as a fix in v0.3.5 for IE.fixEnsure you are using at least `v0.3.5` of `svg-to-vue-component`. If issues persist, adjust SVGO loader options (if applicable) to ensure maximum browser compatibility for the SVG output. -
Unknown custom element: <SvgIcon> - did you register the component correctly?
cause The imported SVG component was not correctly registered with the parent Vue component.fixMake sure the component is imported correctly (e.g., `import SvgIcon from './icon.svg'`) and then added to the `components` option in your Vue component: `export default { components: { SvgIcon } }`.
Warnings
- breaking The package's last release was in April 2019. It is highly likely to be incompatible with modern Vue 3 applications, Vue CLI versions 5+, Webpack 5+, and newer major versions of Nuxt.js or other bundlers/frameworks without significant manual configuration adjustments or polyfills. Expect breaking changes in tooling and API compatibility.
- gotcha Webpack configurations for `.svg` files can conflict. It's crucial to define `issuer` rules to ensure `svg-to-vue-component/loader` only processes SVG files imported by Vue, JavaScript, or TypeScript files, preventing clashes with SVG files imported via CSS (which might require `file-loader` or `url-loader`).
- gotcha This loader transforms an SVG into a Vue Single File Component structure. Therefore, it *must* be chained with `vue-loader` to properly interpret the output. Omitting `vue-loader` will result in webpack errors trying to parse a `.vue` file without the appropriate handler.
- deprecated Given the last release was in April 2019 and the low version number, the project appears to be unmaintained. There will be no further bug fixes, feature enhancements, or official support for newer versions of Vue or its ecosystem tools.
Install
-
npm install svg-to-vue-component -
yarn add svg-to-vue-component -
pnpm add svg-to-vue-component
Imports
- svg-to-vue-component/loader
import svgLoader from 'svg-to-vue-component/loader'
module.exports = { module: { rules: [{ test: /\.svg$/, use: ['vue-loader', 'svg-to-vue-component/loader'] }] } } - CheckIcon
import { CheckIcon } from './check-icon.svg'import CheckIcon from './check-icon.svg'
- svg-to-vue-component/nuxt
import NuxtModule from 'svg-to-vue-component/nuxt'
module.exports = { modules: ['svg-to-vue-component/nuxt'] }
Quickstart
/* webpack.config.js */
module.exports = {
module: {
rules: [
{
test: /\.svg$/,
// Ensure this rule only applies to SVG files imported by JS/TS/Vue files
// To avoid conflicts with CSS imports of SVGs (which might need file-loader)
issuer: /\.(vue|js|ts|svg)$/,
use: [
// Processes the SVG file into a Vue SFC template
'vue-loader',
// Compiles the SVG content into a render function/template for Vue
'svg-to-vue-component/loader'
]
}
]
}
// ...other webpack configurations
}
/* MyComponent.vue */
<template>
<div>
<h1>My Awesome Icon</h1>
<!-- Use the imported SVG as a Vue component -->
<CheckIcon width="24" height="24" fill="currentColor" @click="handleIconClick" />
<p>Click the icon!</p>
</div>
</template>
<script>
import CheckIcon from './assets/check-icon.svg' // Path to your SVG file
export default {
components: {
CheckIcon
},
methods: {
handleIconClick() {
console.log('SVG icon clicked!')
}
}
}
</script>