New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

vite-plugin-svg

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-svg

Use SVG files as Vue Components

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2K
increased by5.83%
Maintainers
1
Weekly downloads
 
Created
Source

vite-plugin-svg

Extend Vite with ability to use SVG files as Vue components.

Features:

  • SVGO optimization
  • Hot Module Replacement support
  • Default SVG import behavior support

Current state:

POC (please read release notes before updating)

Vite is currently in an experimental state which means this plugin will change accordingly until the plugin API is established and frozen.

Currently supported Vite version:

0.13.0

Install

yarn add --dev vite-plugin-svg @vue/compiler-dom

npm i -D vite-plugin-svg @vue/compiler-dom

Usage

To use SVG file as a Vue component you need to append ?component query string to the path of your file. This gives you more control over how a particular SVG file should be loaded and processed:

// Get URL to SVG file
import myIconUrl from './svgs/my-icon.svg';

const img = document.createElement('img');
img.src = myIconUrl;
.my-icon {
  /* Get URL to SVG file */
  background-image: url("./svgs/my-icon.svg");
}
<template>
  <div>
    <MyIcon />
  </div>
</template>
<script>
import MyIcon from './svgs/my-icon.svg?component'; // Note the `?component` part

export default {
  components: {
    MyIcon,
  },
};
</script>

Setup

dev.js
const { createServer } = require('vite');
const { getDevSvgPlugin } = require('vite-plugin-svg');

createServer({
  plugins: [
    getDevSvgPlugin(),
  ],
}).listen(3000); // Set a port you want to use
build.js
const { build } = require('vite');
const { getBuildSvgPlugin } = require('vite-plugin-svg');

(async () => {
  await build({
    rollupInputOptions: {
      plugins: [getBuildSvgPlugin()],
    },
  });
})();

API

Current version of this plugin exports two functions:

getDevSvgPlugin(options)

Returns Vite plugin that is used for dev server only.

getBuildSvgPlugin(options)

Returns Rollup plugin used during build process.

TODO:

  • Convert plugin to TS
  • Support disabling SVGO
  • Basic test coverage

Keywords

FAQs

Package last updated on 08 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc