Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

vite-plugin-solid-svg


Extend Vite with ability to use SVG files as Solid.js components.

Features:


SVGO optimization
Hot Module Replacement support
Support for ?component-solid query string
SSR

Currently supported Vite version:


2.4.4 or above

Install


  1. ``` shell
  2. yarn add --dev vite-plugin-solid-svg

  3. pnpm i -D vite-plugin-solid-svg
  4. ```

Setup


  1. ``` js
  2. // vite.config.js
  3. import solidPlugin from 'vite-plugin-solid'
  4. import solidSvg from 'vite-plugin-solid-svg'
  5. import { defineConfig } from 'vite'

  6. export default defineConfig({
  7.   plugins: [solidPlugin(), solidSvg()],
  8. })
  9. ```

  1. ``` json
  2. // tsconfig.json
  3. "compilerOptions": {
  4.   // ...
  5.   "types": ["vite/client", "vite-plugin-solid-svg/types"],
  6. },
  7. ```

Options


  1. ``` js
  2. SolidSvg({
  3.   /**
  4.    * If true, will export as JSX component if `as` isn't specified.
  5.    *
  6.    * Otherwise will export as url, or as JSX component if '?as=component-solid'
  7.    */
  8.   defaultAsComponent: true,

  9.   svgo: {
  10.     enabled: true, // optional, by default is true
  11.     svgoConfig: <svgo config>  // optional, if defined, the file svgo.config.js is not loaded.
  12.   }
  13. })
  14. ```

If you need to configure svgo, you can also create a config file svgo.config.js in the project's root, following the instructions at svgo docs. The svgo.svgoConfig has precedence over the file.

Usage


Import as a Solid.js component:

  1. ``` tsx
  2. import MyIcon from './my-icon.svg?component-solid';
  3. // or './my-icon.svg' if `defaultAsComponent` is `true`
  4. import MyIcon from './my-icon.svg';

  5. const App = () => {
  6.   return (
  7.     <div>
  8.         <h1> Title </h1>
  9.         <MyIcon />
  10.     </div>
  11.   );
  12. };

  13. export default App;
  14. ```

To import all svg inside a folder, use import.meta.glob('@/svgs/*.svg', { as: 'component-solid' }). See Vite docs for more details.

  1. ``` tsx
  2. const icons = import.meta.glob('./*.svg', { as: 'component-solid' })

  3. /*
  4.   icons = {
  5.     icon1: () => import("./icon1.svg"),
  6.     icon2: () => import("./icon2.svg")
  7.   }
  8. */

  9. const App = () => {
  10.   const Icon1 = lazy(() => iconsDic.icon1())
  11.   return (
  12.     <div>
  13.         <p>hello</p>>
  14.     </div>
  15.   );
  16. };

  17. export default App;
  18. ```

Typescript considerations


vite add its own definition for "*.svg" and it define them as string. So far as I know this can't be overrided. To propertly have type inference, you must use the imports with querystring.

  1. ``` ts
  2. import MyIcon from './my-icon.svg';     // <-- this will match vite module definition, and therefore identified as string
  3. import MyIcon from './my-icon.svg?component-solid';     // <-- this will match the definition in this plugin, and therefore identified as Solid Component
  4. ```

Why


In a Solidjs + vite project, you can easily add an svg image using:

  1. ``` tsx
  2. import iconUrl from './icon.svg'

  3. const App = () => {
  4.   return (
  5.     <div>
  6.       <img href={iconUrl}>
  7.     </div>
  8.   )
  9. }
  10. ```

However the fill color of the image cannot be overriden. Importing the svg as a component solves this and allows css styles to cascade into the svg content. Furthermore there may be situations where you'll need to ensure the image has been fully loaded, for example, before starting an animation. This module gives you a component that you can control when it's loaded.

Credits


This plugin is based on the work from the following projects:

https://github.com/visualfanatic/vite-svg
https://github.com/cobbcheng/vite-plugin-svgstring
Last Updated: 2023-05-23 11:11:51