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 🙏

© 2025 – Pkg Stats / Ryan Hefner

turbopack-inline-svg-loader

v1.0.2

Published

A Turbopack loader for importing SVG as optimized data URI with dimensions

Downloads

403

Readme

turbopack-inline-svg-loader

npm version license

A Turbopack loader for importing SVGs as optimized data URIs with dimensions. The imported object has the same shape as external image ({ src, width, height }) and can be passed directly to the Next.js <Image /> component.

Example

import myIcon from './icon.svg';

return <Image src={myIcon} alt="my icon" />;

/*
myIcon is an object like:
{
  src: 'data:image/svg+xml,...',
  width: 32,
  height: 32,
}
*/

Why inline SVG?

Inlining small SVGs is beneficial because it eliminates additional HTTP requests, resulting in faster page loads and instant rendering. The slight increase in JavaScript bundle size is usually outweighed by the overall performance gains.

Why not SVGR?

Check out this article.

Installation

Install via any package manager:

# NPM
npm i -D turbopack-inline-svg-loader

# PNPM
pnpm add -D turbopack-inline-svg-loader

# Yarn
yarn add -D turbopack-inline-svg-loader

Configuration

Add the loader configuration to next.config.js. Since Next.js v16, you can conditionally apply a loader only to SVGs smaller than a given size:

const nextConfig = {
  turbopack: {
    rules: {
      '*.svg': {
        loaders: ['turbopack-inline-svg-loader'],
        condition: {
          content: /^[\s\S]{0,4000}$/, // <-- Inline SVGs smaller than ~4Kb (since Next.js v16)
        },
        as: '*.js',
      },
    },
  },
  // ...
};

Usage

Statically import an SVG file and pass it to the <Image/> component:

import Image from 'next/image';
import myIcon from './icon.svg';

export default function Page() {
  return <Image src={myIcon} alt="my icon" />;
}

How to change size?

You can change the image size via the CSS style prop or className:

// Set size via style
return <Image src={myIcon} style={{ width: 64, height: 64 }} alt="my icon" />;

// Set size with Tailwind
return <Image src={myIcon} className="size-64" alt="my icon" />;

How to change color?

For monochrome icons, you can change the color using the CSS mask technique. To achieve this, create a helper component Icon.tsx that renders the SVG as a mask:

/**
 * A component for rendering monochrome SVG icons using the current text color.
 */
import { type ComponentProps } from 'react';
import { type StaticImageData } from 'next/image';

type IconProps = Omit<ComponentProps<'img'>, 'src'> & {
  src: StaticImageData;
};

const EMPTY_SVG = `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E`;

export default function Icon({ src, width, height, style, ...props }: IconProps) {
  return (
    <img
      width={width ?? src.width}
      height={height ?? src.height}
      src={EMPTY_SVG}
      style={{
        ...style,
        backgroundColor: `currentcolor`,
        mask: `url("${src.src}") no-repeat center / contain`,
      }}
      {...props}
    />
  );
}

Now you can render colored icons:

import Icon from './Icon';
import myIcon from './icon.svg';

// Set color with style
return <Icon src={myIcon} style={{ color: 'green' }} />;

// Set color with Tailwind
return <Icon src={myIcon} className="text-green-600" />;

TypeScript

By default, Next.js imports *.svg assets as the any type to avoid conflicts with SVGR. To make *.svg imports behave like other images ({ src, width, height }), create the following svg.d.ts file in the project root:

declare module '*.svg' {
  const content: import('next/image').StaticImageData;
  export default content;
}

and add it to tsconfig.json before next-env.d.ts:

"include": [
+ "svg.d.ts",
  "next-env.d.ts",
  ...
],

Now your SVG imports will be resolved as { src, width, height }.

References

A short background article How I arrived at this package.

Some other resources to dive deep into the rabbit hole of SVGs:

Feedback

Feel free to share your feedback and suggestions in the issues.

License

MIT