SVG to Vue Component Compiler

0.3.8 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure webpack to process SVG files into Vue components and then how to import and use such a component within a Vue SFC, including passing props and handling events.

/* 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>

view raw JSON →