1.3.0 • Published 5 months ago

esbuild-plugin-svg-bundle v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

esbuild-plugin-svg-bundle

npm CI node version

This is a plugin for esbuild that bundles all SVG files referenced in CSS files into a single SVG bundle consisting nested SVGs accessible via fragment ids. SVGs will be optimized using svgo when the minify option is enabled.

Install

npm i -D esbuild-plugin-svg-bundle

Usage

import esbuild from "esbuild";
import svgBundlePlugin from "esbuild-plugin-svg-bundle";

esbuild.build({
    entryPoints: ["src/app.ts"],
    bundle: true,
    outdir: "dist/",
    plugins: [
      svgBundlePlugin({
        bundleFile: "assets/svg-bundle.svg",
        bundleUrl: "../assets/svg-bundle.svg"
      })
    ]
});

This will create dist/assets/svg-bundle.svg and references in CSS will get remapped from

.element-with-svg-background {
  background-image: url(svg-file.svg);
}

using the specified bundleUrl to

.element-with-svg-background {
  background-image: url(../assets/svg-bundle.svg#svg-file);
}

Configuration

propertyrequired?typedefaultdescription
bundleFilerequiredstring-The filepath where the generated should be stored.
bundleUrlrequiredstring-The relative or absolute URL where the bundle can be loaded from.
hashoptionalstringundefinedTo help with cache busting you can specify a hash value for the bundle. Later versions of this plugin will likely do this automatically based on the bundle content.
gapoptionalnumber1Gap between packed images.