npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-svg-sfc

v2.0.1

Published

Convert SVGs to Vue single file component(SFC), support <style> tag

Downloads

59

Readme

vite-plugin-svg-sfc

Npm Version Test codecov

Vite (also support Rollup) plugin to convert SVGs to Vue single file components (SFC).

example

🚀 Features

  • Extract <style> tags from SVG to scoped SFC style block.
  • Hot Module Replacement support.
  • Minification with SVGO.

Usage

Install:

npm i -D vite-plugin-svg-sfc

Then add the plugin to your vite.config.js:

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import svgSfc from "vite-plugin-svg-sfc";

// If in commonjs module
// const { default: svgSfc } = require("vite-plugin-svg-sfc");

export default defineConfig({
    plugins: [svgSfc(), vue()],
});

SVG files can be imported as Vue component using the ?sfc query:

<template>
    <MyIconComponent/>
    <img :src="myIconUrl" alt="icon">
    <pre>{{ myIconXml }}</pre>
</template>

<script setup>
import MyIconComponent from "../assets/my-icon.svg?sfc";

// vite-plugin-svg-sfc does not affect Vite default asset handling.
import myIconUrl from "../assets/my-icon.svg";
import myIconRaw from "../assets/my-icon.svg?raw";
</script>

If you are using TypeScript, vite-plugin-svg-sfc/client can be added to d.ts declaration file.

/// <reference types="vite-plugin-svg-sfc/client" />

Build a component library:

// index.js
export { default as FooIcon } from "./icons/foo.svg?sfc";
export { default as BarIcon } from "./icons/bar.svg?sfc";
import { defineConfig } from "vite";
import svgSfc from "vite-plugin-svg-sfc";
import vue from "@vitejs/plugin-vue";

export default defineConfig({
    plugins: [svgSfc(), vue()],
    build: {
        rollupOptions: {
            external: "vue",
        },
        lib: {
            formats: ["es"],
            entry: "index.js",
        },
    },
});

Without Vite

SVGSFCConvertor is exported for convert SVG to SFC code without build tools.

import { SVGSFCConvertor } from "vite-plugin-svg-sfc";

const svg2sfc = new SVGSFCConvertor();
const vueCode = svg2sfc.convert("<svg>...</svg>");
// <template><svg>...</svg></template>...

Options

mark

Type: string

Default: sfc

SVG will be imported as SFC using the query parameter.

// vite.config.js
export default defineConfig({
    plugins: [svgSfc({ mark: "component" }), vue()],
});

// Vue component.
import Icon from "../assets/my-icon.svg?component";

extractCSS

Type: boolean

Default: true

When set to true, extract all style elements in the svg and put their content into a scoped SFC style block.

Vue template compiler will throw an error when the template contains <style>, so we need to move them to top level.

You may notice that SVGO has a inlineStyles plugin that avoid <style> in the SVG by move styles onto the style attribute, but some features (e.g. media query) can not be inlined.

minify

Type: boolean

Default: true on production mode and false otherwise.

Perform minification for SVG.

responsive

Type: boolean

Default: true

When set to true, some attributes on <svg> will be replaced with reactive value:

  • set width & height to "1em".
  • set fill and stroke to "currentColor" if it's not transparent。

svgProps

Type: Record<string, string>

Default: undefined

Add props to the root SVG tag.

svgo

Type: OptimizeOptions | false

Default: {}

Specify the SVGO config to use, set to false to disable processing SVG data.

If svgo.plugins is specified, the extractStyles, minify, svgProps, responsive options and builtin plugins are ignored, you can add them manually:

import svgSfc from "vite-plugin-svg-sfc";

svgSfc({
    svgo: {
        plugins: [
            "responsiveSVGAttrs",
            "extractCSS",
            "preset-default",
            {
                name: "modifySVGAttrs",
                params(attrs) {
					delete attrs.xmlns;
					delete attrs.version;
					delete attrs["xml:space"];
                }
            }
        ]
    }
});