Vue SVG Loader

0.16.0 · active · verified Tue Apr 21

`vue-svg-loader` is a Webpack loader designed to transform SVG files directly into functional Vue components, allowing developers to easily embed, style, and dynamically manipulate SVG assets within their Vue applications. The current stable version is 0.16.0, which primarily supports Vue 2 projects. However, version 0.17.0 is actively being developed in beta, with a strong focus on introducing robust Vue 3 compatibility. While its release cadence has historically been somewhat sporadic, recent activity indicates renewed development efforts, particularly for Vue 3. Key differentiators include its seamless integration into the Vue build process, automatic SVGO optimization (with an option to disable it), and the generation of functional components utilizing ESM exports since version 0.11.0, which contributes to better performance and tree-shaking capabilities.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure Webpack with `vue-svg-loader` and use an imported SVG file as a functional Vue component within a template, including basic styling.

import Vue from 'vue';
import App from './App.vue';
import TestIcon from './assets/test.svg';

// webpack.config.js (excerpt)
// module.exports = {
//   module: {
//     rules: [
//       { test: /\.vue$/, loader: 'vue-loader' },
//       { test: /\.js$/, loader: 'babel-loader' },
//       { test: /\.svg$/, use: ['vue-loader', 'vue-svg-loader'] },
//     ],
//   },
//   resolve: {
//     extensions: ['.js', '.vue', '.json', '.svg']
//   }
// };

// App.vue
<template>
  <div id="app">
    <h1>Welcome to Vue SVG Loader Example</h1>
    <p>Here's a dynamically styled SVG icon:</p>
    <TestIcon class="my-icon" :width="100" :height="100" />
    <p>Another icon, with default sizing:</p>
    <OtherIcon />
  </div>
</template>

<script>
import OtherIcon from './assets/other.svg';

export default {
  name: 'App',
  components: {
    TestIcon, // Register the imported SVG as a component
    OtherIcon
  }
}
</script>

<style>
.my-icon {
  fill: blueviolet;
  stroke: darkblue;
  stroke-width: 2px;
  transition: all 0.3s ease;
}
.my-icon:hover {
  fill: hotpink;
  transform: scale(1.1);
}
</style>

new Vue({
  render: h => h(App),
}).$mount('#app');

view raw JSON →