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

two-faces-svg-to-vue-component

v0.3.1

Published

Compile SVG files to Vue SFC.

Downloads

29

Readme

svg-to-vue-component

NPM version NPM downloads CircleCI donate chat

Why

When you import the .svg file as a Vue component instead of using the URL to the file, you can style it with CSS and add addtional DOM properties or event handlers to the component directly.

The differences between this project and vue-svg-loader are:

  • This one has built-in hot reloading support for webpack since the SVG code is compiled via vue-loader.
  • The latter only supports class and style attributes on the generated component while we support all DOM props and events.
  • This one supports project-wise and file-relative configuration for SVGO.

Install

yarn add svg-to-vue-component --dev

Usage

With Webpack

// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.svg$/,
        // If you want to import svg in css files
        // You need to configure another rule to use file-loader for that
        issuer: /\.(vue|js|ts|svg)$/,
        use: [
          // This loader compiles .svg file to .vue file
          // So we use `vue-loader` after it
          'vue-loader',
          'svg-to-vue-component/loader'
        ]
      }
    ]
  }
  // ...other configurations
}

Then you can import .svg files directly and use them as Vue components:

<template>
  <!-- Dom props and events are all available here -->
  <CheckIcon width="20px" height="20px" @click="handleClick" />
</template>

<script>
import CheckIcon from './check-icon.svg'

export default {
  components: {
    CheckIcon
  },

  methods: {
    handleClick() {
      console.log('It works!')
    }
  }
}
</script>

With Vue CLI

In your vue.config.js:

module.exports = {
  chainWebpack(config) {
    // Only convert .svg files that are imported by these files as Vue component
    const FILE_RE = /\.(vue|js|ts|svg)$/

    // Use vue-cli's default rule for svg in non .vue .js .ts files
    config.module.rule('svg').issuer(file => !FILE_RE.test(file))

    // Use our loader to handle svg imported by other files
    config.module
      .rule('svg-component')
      .test(/\.svg$/)
      .issuer(file => FILE_RE.test(file))
      .use('vue')
      .loader('vue-loader')
      .end()
      .use('svg-to-vue-component')
      .loader('svg-to-vue-component/loader')
  }
}

With Poi

In your poi.config.js:

module.exports = {
  plugins: ['svg-to-vue-component/poi'],

  // Or with options for the webpack loader
  plugins: [
    {
      resolve: 'svg-to-vue-component/nuxt',
      options: {}
    }
  ]
}

Nuxt.js 2

In your nuxt.config.js:

module.exports = {
  modules: ['svg-to-vue-component/nuxt'],

  // Or with options for the webpack loader
  modules: [
    ['svg-to-vue-component/nuxt',
      {
        // ...
      }
    ]
  ]
}

Loader Options

Pass loader options like this:

// ...
{
  test: /\.svg$/,
  use: [
    'vue-loader',
    {
      loader: 'svg-to-vue-component/loader',
      options: {
        // ...Your options here
      }
    }
  ]
}

| Option | Description | | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | svgoConfig | Project-wise configuration for SVGO, if you want file-relative configuration, use the config file instead, supported format: .svgo.{yml,js,json}, see here for an example file. If you want to turn off SVGO entirely, pass false here. |

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

svg-to-vue-component © EGOIST, Released under the MIT License. Authored and maintained by EGOIST with help from contributors (list).

Website · GitHub @EGOIST · Twitter @_egoistlily