GithubHelp home page GithubHelp logo

ezzatomar / vite-plugin-solid-svg Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jfgodoy/vite-plugin-solid-svg

0.0 0.0 0.0 93 KB

Vite plugin to Import SVG files as Solid.js Components

License: MIT License

JavaScript 100.00%

vite-plugin-solid-svg's Introduction

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 ?url and ?component query string
  • SSR

Currently supported Vite version:

2.4.4 or above

Install

yarn add --dev vite-plugin-solid-svg

npm i -D vite-plugin-solid-svg

Setup

// vite.config.js
import solidPlugin from "vite-plugin-solid";
import solidSvg from "vite-plugin-solid-svg";

module.exports = {
  plugins: [
    solidPlugin(),
    solidSvg(),
  ],
};

Options

SolidSvg({
  // Default behavior when importing `.svg` files, possible options are: 'url' and `component`
  defaultExport: 'component',

  svgo: {
    enabled: true, // optional, by default is true
    svgoConfig: <svgo config>  // optional, if defined, the file svgo.config.js is not loaded.
  }
})

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:

import MyIcon from './svgs/my-icon.svg';

const App = () => {
  return (
    <div>
        <h1> Title </h1>
        <MyIcon />
    </div>
  );
};

export default App;

Import as url:

import myIconUrl from './svgs/my-icon.svg?url';

const App = () => {
  return (
    <div>
        <p>url to icon: {myIconUrl} </p>
    </div>
  );
};

export default App;

To import all svg inside a folder, use [name].svg as the file value. This will import an object, where the keys are the matched names (without extension), and the values are the corresponding component or url.

import iconsDic from './svgs/[name].svg';

/*
  iconsDic = {
    icon1: () => import("./svgs/icon1.svg"),
    icon2: () => import("./svgs/icon2.svg")
  }
*/

const App = () => {
  const Icon1 = lazy(() => iconsDic.icon1())
  return (
    <div>
        <p>hello</p><Icon1 />
    </div>
  );
};

export default App;

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.

import MyIcon from './svgs/my-icon.svg';     // <-- this will match vite module definition, and therefore identified as string
import MyIcon from './svgs/my-icon.svg?component';     // <-- this will match the definition in this plugin, and therefore identified as Solid Component

Why

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

import iconUrl from '/svgs/icon.svg'

const App = () => {
  return (
    <div>
      <img href={iconUrl}>
    </div>
  )
}

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:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.