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

svg-mixed-loader

v0.8.3

Published

Webpack loader resolving svg into url string and multiple component formats.

Downloads

271

Readme

svg-mixed-loader

Webpack loader resolving svg into url string and multiple component formats.

Background

In community svg files are commonly used to introduce icons to web apps, in this case developers are tend to import and svg as a component:

import IconPlus from './icon-plus.svg';

const CreateButton = () => (
    <Button>
        <IconPlus />
        Create New
    </Button>
);

Also, svg, when itself is a common image format, may used as an image:

import logoURL from './logo.svg';

const Header = () => (
    <div style={{display: 'flex'}}>
        <img width={180} height={30} src={logoURL} />
        <div style={{flex: 1}}>
            {/* navigation}*/}
        </div>
    </div>
);

As these use cases are conflicting to each other, this loader sovles this by export multiple possible formats from an svg file.

Install

svg-mixed-loader requires url-loader and file-loader as peer dependencies.

npm install -D svg-mixed-loader url-loader file-loader

Usage

// webpack.config.js
module.exports = {
    module: {
        rules: [
            {
                test: /\.svg$/,
                use: [
                    {
                        loader: 'svg-mixed-loader',
                        options: {
                            // options
                        },
                    },
                    // You may add other preprocess loaders like svgo-loader or img-loader here
                ],
            },
        ],
    },
};

Output

This loader currently create a default export of image URL, and a named export ReactComponent as a react component.

import React from 'react';

export default 'data:image/svg;...';

export const ReactComponent = props => React.createElement(/* ... */);
ReactComponent.displayName = 'AutoResolvedComponentName';

Options

{
    // This option will be forwarded to url-loader, can set to false to disable generation of default export
    url: {
        limit: 1024,
    },
    // Can be:
    //
    // 1. true: generate react component as ReactComponent named export with default option
    // 2. false: don't generate react component
    // 3. object: customize react code generation, see below
    //
    // Default to false.
    react: {
        // Can be multiple formats:
        //
        // 1. true: auto resolve a component name from resource path
        // 2. false: remove display name from output
        // 3. string: a static display name
        // 4. function: customize display by (resourcePath) => string
        //
        // Default to true.
        displayName: true,
    }
}

By default svg-mixed-loader generates only default export, behaves exactly the same as url-loader, if ReactComponent named export is wanted, pass {react: true} as options is a quick solution.