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

svgj

v1.0.5

Published

Quickly convert `.svg` files into `jsx`

Downloads

363

Readme

svgj

svgj quickly converts .svg files into jsx. svgj is about 40x faster than svgr, depending on how you measure (benchmarks at bottom).

Input:

<svg>
  <rect x="0" y="0" w="50" h="100" />
</svg>

Output:

import * as React from "React";

export const ReactComponent = ({ ...props }) => (
  <svg {...props}>
    <rect x={`0`} y={`0`} w={`50`} h={`100`} />
  </svg>
);

Compared to svgr, svgj is buggier, has a worse API, fewer features, only returns JSX as strings that need to be transpiled, and prints ugly looking JSX.

But its much faster, thanks to using htmlparser2 and serialization logic from dom-serializer (with modifications to support JSX and passing in custom props based on element). Consequently, this will often work with html.

You probably only want to use this inside of a bundler when importing SVG files as JSX.

Usage

You probably want to use the esbuild plugin instead of the underlying library.

Installation

yarn:

yarn add esbuild-plugin-svgj

npm:

npm install esbuild-plugin-svgj

esbuild-plugin-svgj usage

import { plugin } from "esbuild-plugin-svgj";
import { readFile } from "fs/promises";
import esbuild from "esbuild";

esbuild.build({
  // ...rest of esbuild config
  plugins: [
    plugin({
      readFile: (path) => readFile(path, "utf8"),
    }),
  ],
});

Library usage

export function render(
  content: string,
  displayName: string = "ReactComponent",
  jsxImports: string = "* as React",
  jsxFrom: string = "react",
  exportName = displayName,
  props = defaultProps,
  opts = defaultOpts,
  useMemo = false
): string;

export const defaultOpts: Options = {
  removeAttrs: {
    xmlns: true,
  },
  addProps: {
    svg: {
      props: "...",
    },
  },
};

export const defaultProps: { [key: string]: string } = {
  props: "...",
};

Benchmarks

These svgs are from bootstrap.

To download the test icons:

git submodule update --init --recursive

Then, run node bench.mjs.

The first group renders 10 svgs. The second group renders a single svg. The svg files are read from disk as strings before the benchmark starts.

❯ node bench.mjs

svgj x 10,271 ops/sec ±1.06% (90 runs sampled)
svgr sync x 105 ops/sec ±4.86% (74 runs sampled)
Fastest is svgj
Slowest is svgr sync
svgj x 89,124 ops/sec ±1.45% (90 runs sampled)
svgr sync x 1,199 ops/sec ±2.01% (81 runs sampled)
Fastest is svgj
Slowest is svgr sync

Then, run node bench-async.mjs

benchmark.js is a little tough to get working right with async code, so this one just uses console.time, but with a warmup beforehand. The files in both benchmarks are the same.

❯ node bench-async.mjs

svgj (1 file): 0.128ms
svgr (1 file): 1.675ms
svgj (10 files): 0.376ms
svgr (10 files): 20.193ms
svgj (258 files): 9.493ms
svgr (258 files): 330.877ms

TypeScript

This doesn't generate type definitions.

But, you can google typescript import svg react. Here's the first result.

To save you a click, add this .d.ts and it should work:

declare module "*.svg" {
  import React = require("react");
  export const ReactComponent: React.SFC<React.SVGProps<SVGSVGElement>>;
  const src: string;
  export default src;
}

Keep in mind that this currently won't React.forwardRef. Per create-react-app's defaults, src is the source string and ReactComponent is the JSX source.