All Projects → iest → babel-plugin-inline-svg

iest / babel-plugin-inline-svg

Licence: other
Babel plugin to optimise and inline svg

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to babel-plugin-inline-svg

nornj
More exciting JS/JSX based on Template Engine, support control flow tags, custom directives, two-way binding, filters and custom operators.
Stars: ✭ 97 (+94%)
Mutual labels:  babel-plugin
svg-gobbler
Open source browser extension that makes designing and developing easier by finding, processing, exporting, optimizing, and managing SVG content.
Stars: ✭ 272 (+444%)
Mutual labels:  inline-svg
babel-plugin-transform-react-qa-classes
Add component's name in `data-qa` attributes to React Components
Stars: ✭ 41 (-18%)
Mutual labels:  babel-plugin
idomizer
An HTML template parser compiling an incremental-dom render factory.
Stars: ✭ 15 (-70%)
Mutual labels:  babel-plugin
babel-plugin-solid-undestructure
A Babel plugin for SolidJS that allows you to destructure component props without losing reactivity.
Stars: ✭ 45 (-10%)
Mutual labels:  babel-plugin
babel-plugin-storybook-csf-title
A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.
Stars: ✭ 17 (-66%)
Mutual labels:  babel-plugin
babel-plugin-rewire-exports
Babel plugin for stubbing [ES6, ES2015] module exports
Stars: ✭ 62 (+24%)
Mutual labels:  babel-plugin
sprockets-bumble d
Sprockets plugin to transpile modern javascript using Babel, useful while migrating to ES6 modules
Stars: ✭ 32 (-36%)
Mutual labels:  babel-plugin
react-crud-icons
57 SVG icons for CRUD applications, packaged as a React component with light & dark themes and tooltip.
Stars: ✭ 19 (-62%)
Mutual labels:  inline-svg
babel-plugin-transform-amd-to-commonjs
✨ Babel plugin that transforms AMD to CommonJS
Stars: ✭ 44 (-12%)
Mutual labels:  babel-plugin
babel-plugin-transform-for-of-as-array
Transform all for-of loops into the equivalent array for loop
Stars: ✭ 14 (-72%)
Mutual labels:  babel-plugin
babel-plugin-solid-labels
Simple, reactive labels for SolidJS
Stars: ✭ 127 (+154%)
Mutual labels:  babel-plugin
babel-plugin-console-source
Add the file name and line numbers to all console logs.
Stars: ✭ 38 (-24%)
Mutual labels:  babel-plugin
penv.macro
A macro used with babel-plugin-macros to write configurations for multiple environments, and remove configurations are irrelevant with the specified environment from your codes finally.
Stars: ✭ 73 (+46%)
Mutual labels:  babel-plugin
babel-plugin-detective
Babel plugin that scans the AST for require calls and import statements
Stars: ✭ 26 (-48%)
Mutual labels:  babel-plugin
babel-plugin-hyperscript-to-jsx
This plugin transforms react-hyperscript into JSX. Intended to be used as codemod.
Stars: ✭ 20 (-60%)
Mutual labels:  babel-plugin
babel-plugin-transform-rename-properties
A Babel plugin for renaming JavaScript properties
Stars: ✭ 19 (-62%)
Mutual labels:  babel-plugin
babel-plugin-transform-hoist-nested-functions
Babel plugin to move nested functions to outer scopes
Stars: ✭ 32 (-36%)
Mutual labels:  babel-plugin
babel-plugin-object-styles-to-template
Babel plugin to transpile object styles to template literal
Stars: ✭ 33 (-34%)
Mutual labels:  babel-plugin
babel-plugin-react-directives
A babel plugin that provides some directives for react(JSX), similar to directives of vue.
Stars: ✭ 80 (+60%)
Mutual labels:  babel-plugin

babel-plugin-inline-svg

NPM version Downloads Dependency status

Import raw SVG files into your code, optimising with SVGO, and removing ID namespace conflicts.

What it do

1. Turns import statements into inline SVG strings

So this:

import someSvg from "some-svg.svg";

Becomes this:

var someSvg =
  '<svg width="50" height="50" viewBox="0 0 50 50" xmlns="http://www.w3.org/2000/svg"><title>home</title><path d="M37.6 24.104l-4.145-4.186v-6.389h-3.93v2.416L26.05 12.43a1.456 1.456 0 0 0-2.07 0L12.43 24.104a1.488 1.488 0 0 0 0 2.092c.284.288.658.431 1.031.431h1.733V38h6.517v-8.475h6.608V38h6.517V26.627h1.77v-.006c.36-.01.72-.145.995-.425a1.488 1.488 0 0 0 0-2.092" fill="#191919" fill-rule="evenodd" id="someSvg-someID"/></svg>';

So you can do something like this maybe:

import React from "react";
import someSvg from "some-svg.svg";

const NaughtyUsage = () => (
  <span
    dangerouslySetInnerHTML={{
      __html: someSvg,
    }}
  />
);

2. Optimises the SVG through SVGO

Does what it says on the tin. You can pass options to the SVGO processor with an svgo object in options.

You can also disable this option if you really want, with disableSVGO: true.

3. Namespaces id’s to prevent conflicts

If you inline a lot of SVGs you might get namespace conflicts, which could be really annoying if you're styling your SVG in CSS and whatnot. This plugin solves that with some some regex trickery. The namespace of the ID comes from the name of import/require variable.

So given this simple cheese.svg file:

<svg><circle cx="10" cy="10" r="50" id="CIRCLE"></circle></svg>

Which you then import like so:

import wheelOfCheese from "cheese.svg";

You get the following output:

var wheelOfCheese =
  '<svg><circle cx="10" cy="10" r="50" id="wheelOfCheese-CIRCLE"></circle></svg>';

To disable this feature, pass disableNamespaceIds: true in the options.

4. Exporting as dataURI format

If you need to use the output directly in an image source (<img src=> or in a css background-image, for example), you can pass exportDataURI: true in the options. The output will be encoded as base64 and prefixed with data:image/svg+xml;base64,, so you can do something like:

import logo from "./logo.svg";

const Logo = () => <img src={logo} />;

Installation

npm install --save-dev babel-plugin-inline-svg

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["inline-svg"]
}

Options

  • ignorePattern - A string regex that imports will be tested against so you can ignore them
  • disableSVGO - set to false to disable running the svg through SVGO
  • disableNamespaceIds - set to false to leave all id's as they are
  • svgo - an object of SVGO options
  • exportDataURI - set to true to export a base64-encoded SVG, prefixed with data:image/svg+xml;base64,

Example .babelrc:

{
  "plugins": [
    [
      "inline-svg",
      {
        "ignorePattern": "icons",
        "disableNamespaceIds": true,
        "svgo": {
          "plugins": [
            {
              "removeDimensions": true,
            }
          ]

        }
      }
    ]
  ]
}

Note: To function correctly, this babel plugin disables the cleanupIDs SVGO plugin by default (to facilitate the ID namespacing). When passing your own SVGO options you might want to remove the cleanupIDs plugin so namespacing still works.

Also note: the ID namespaceing can be done with a similar SVGO plugin, prefixIds — however this prefix is a static string so you might still end up with namespace conflicts.

Via CLI

$ babel --plugins inline-svg script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["inline-svg"],
}); // => { code, map, ast };
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].