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

rollup-plugin-svg-hyperscript

v0.1.2

Published

Rollup plugin to import .svg files as React functional component

Downloads

28

Readme

rollup-plugin-svg-hyperscript

Rollup plugin to import .svg files as React(like) component.

The plugin transforms SVG content to hyperscript (React.createElement) based functional component not JSX

<!-- rainbow.svg -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-14 -14 28 28">
    <circle r="14" fill="#f00"/>
    <circle r="12" fill="#ff7f00"/>
    <circle r="10" fill="#ff0"/>
    <circle r="8" fill="#0f0"/>
    <circle r="6" fill="#00f"/>
    <circle r="4" fill="#2E2B5F" />
    <circle r="2" fill="#8B00FF" />
</svg>
// transformed import
import React from "react";

export default function rainbow(props) {
  return React.createElement(
    "svg",
    props,
    React.createElement("circle", { r: 14, fill: "#f00" }, null),
    React.createElement("circle", { r: 12, fill: "#ff7f00" }, null),
    React.createElement("circle", { r: 10, fill: "#ff0" }, null),
    React.createElement("circle", { r: 8, fill: "#0f0" }, null),
    React.createElement("circle", { r: 6, fill: "#00f" }, null),
    React.createElement("circle", { r: 4, fill: "#2e2b5f" }, null),
    React.createElement("circle", { r: 2, fill: "#8b00ff" }, null)
  );
}
rainbow.defaultProps = {
  xmlns: "http://www.w3.org/2000/svg",
  viewBox: "-14 -14 28 28"
}

Installation

npm install --save-dev rollup-plugin-svg-hyperscript

Usage

By default plugin uses React.createElement and transform properties to camelCase.

// rollup.config.js
import svgImport from "rollup-plugin-svg-hyperscript";

export default {
  input: "src/main.js",
  output: {
    file: "bundle.js",
    format: "esm"
  },
  plugins: [svgImport()]
};
// components/header.jsx
import React from 'react';
import Logo from './company-logo.svg';

export function(props){
  return (
    <header>
      <Logo width={32} height={32} />
      <h1>{props.compnayName}</h1>
    </header>
  );
}

Configurations

// rollup.config.js
import svgImport from "rollup-plugin-svg-hyperscript";

export default {
  input: "src/main.js",
  output: {
    file: "bundle.js",
    format: "esm"
  },
  plugins: [
    svgImport({
      // import declaration for element renderer
      importDeclaration: "import { h } from 'preact'", // Default: "import React from 'react'"

      // Pragma to create element, by default it uses React.createElement
      pragma: "h", // Default: "React.createElement",

      // React requires all properties and attributes in camelCase, e.g. 
      // xmlns:xlink should be provided as xmlnsXlink 
      // and enable-background as enabelBackground.
      // Whereas Preact has raw HTML attribute/property names support and 
      // transformation is not needed.
      // https://preactjs.com/guide/v10/differences-to-react/#raw-html-attributeproperty-names
      transformPropNames: false // Default: true
    })
  ]
};