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-react-svgs

v0.1.13-react

Published

A vite plugin, can import svg as react component and edit svg color by props

Downloads

75

Readme

vite-plugin-react-svgs

English | 简体中文

优雅的在 React 中使用 SVG

  • 🌟 导入 Svg 图标为 React 组件
  • 🌈 使用 Props 优雅的修改 SVG 的颜色与大小。

安装插件

  1. 安装

    npm i vite-plugin-react-svgs --save-dev
    yarn add vite-plugin-react-svgs -D
    pnpm i vite-plugin-react-svgs -D
  2. Vite 设置

    添加以下代码到 vite.config.js / vite.config.ts 文件中:

    import { createSvgIconsPlugin } from 'vite-plugin-react-svgs';
    import { defineConfig } from 'vite';
    
    export default defineConfig({
      plugins: [
        react(),
        createSvgIconsPlugin({
          defaultImport: 'component',
        }),
      ],
    });
  3. TS 类型文件 (optional)

    如果你正在使用 Typescript,你需要额外的类型文件去使 Typescript 可以准确地理解导入的 SVG 文件。

    /// <reference types="vite-plugin-react-svgs/index" />

    或者

    declare module '*.svg?component' {
      import { FunctionComponent } from 'react';
      const src: FunctionComponent<{ width?: string, height?: string, color?: string }>;
      export default src;
    }
    
    declare module '*.svg?url' {
      const src: string;
      export default src;
    }
    
    declare module '*.svg?raw' {
      const src: string;
      export default src;
    }
    
    declare module 'vite-plugin-react-svgs' {
      import { Plugin } from 'vite';
      function createSvgIconsPlugin(options?: {
        defaultImport?: 'raw' | 'component' | 'url';
      }): Plugin;
      export default createSvgIconsPlugin;
    }
  4. 使用插件

    import ReactSvg from './assets/react.svg?component';
    import RainSvg from './assets/rain.svg?component';
    import React from 'react';
    import './App.css';
    
    function App() {
      return (
        <div className="container">
          <RainSvg color="blue" height="5em" width="5em" />
          <ReactSvg width="10em" height="10em" />
          <RainSvg color="#f0f0f0" height="5em" width="5em" />
        </div>
      );
    }
    
    export default App;

    预期的结果:

    image.png

许可

MIT License ChaconneLuo