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

esbuild-svgr-plugin

v0.2.0

Published

An esbuild plugin that transforms SVGs into React components using SVGR.

Downloads

4,407

Readme

esbuild-svgr-plugin

Another esbuild plugin that uses SVGR to transform SVGs into React components.

Other implementations lacked support for falling back to the default SVG loader whenever SVGR is inadequate. This version only transforms imports on files that are known to support React components, keeping itself configurable if needed.

Install

npm:

npm i -D esbuild-svgr-plugin

yarn:

yarn add -D esbuild-svgr-plugin

Usage

import esbuild from 'esbuild'
import { svgrPlugin } from 'esbuild-svgr-plugin'

esbuild.build({
    entryPoints: ['src/app.tsx'],
    bundle: true,
    outdir: 'dist/',
    plugins: [svgrPlugin()],
})

SVG imports found by esbuild-svgr-plugin are transformed to React components.

// app.tsx

import ReactDOM from 'react-dom'
import AppIcon from './icons/app-icon.svg'

import './app.css'

ReactDOM.render(<AppIcon />, null)

esbuild-svgr-plugin parses the content of ./icons/app-icon.svg and wraps it in a React component. The output can be visualized as follows:

function AppIcon {
    return (
        <svg>
            {/** ... */}
        </svg>
    )
}

export default AppIcon

By default, esbuild-svgr-plugin only transforms on a set of filetypes (.js, .ts, .tjx, .tsx). Doing it for others can lead to unexpected file loader errors. For instance, if there's an url-import in the .css file imported by app.tsx, the React component output is meaningless for this import type, thus breaking it.

/* app.css */

.app::before {
    background-image: url('./images/illustration.svg');
    /* ... */
}

As .css imports are not parsed by esbuild-svgr-plugin, they fallback to the rest of the esbuild plugin chain loaders, or if there's none, to the default file loader.

Configuration

esbuild.build({
    entryPoints: ['src/app.tsx'],
    bundle: true,
    outdir: 'dist/',
    plugins: [
        svgrPlugin({
            /**
             * A regular expression that indicates which assets
             * should be converted to a React component
             */
            filter: /\.(svg|xml)$/,
            /**
             * A regular expression that controls on which
             * files the imports should be transformed
             */
            issuer: /\.js/,
        }),
    ],
})