📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP
Socket
Sign inDemoInstall
Socket

rollup-plugin-svg-sprites

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rollup-plugin-svg-sprites

Rollup plugin for creating SVG sprites.

1.2.5
latest
npm
Version published
Weekly downloads
735
79.27%
Maintainers
1
Weekly downloads
 
Created
Source

rollup-plugin-svg-sprites

Rollup plugin for creating SVG sprites.

PS:该插件是基于 svg-bakersvg-baker-runtime 的,跟 svg-sprite-loader 同根同源,基本可以认为是从 webpack 转到 rollup 上,可以减少避免自己踩坑。

Installation

The plugin dependencies svg-baker. It requires @rollup/plugin-commonjs.

// npm
npm i rollup-plugin-svg-sprites -D

// yarn
yarn add rollup-plugin-svg-sprites -D

The plugin dependencies svg-baker. It requires @rollup/plugin-commonjs.

// npm
npm i @rollup/plugin-commonjs -D

// yarn
yarn add @rollup/plugin-commonjs -D

Usage

Use in TypeScript

Add the type definitions in *.d.ts:

declare module '*.svg?vueComponent' {
  import { Component } from 'vue'
  const src: Component
  export default src
}

declare module '*.svg?jsx' {
  import { ReactElement } from 'react'
  const src: ReactElement<any, any>
  export default src
}

Basic usage

rollup.config.js:

import svgSprites from 'rollup-plugin-svg-sprites'
import commonjs from '@rollup/plugin-commonjs'

export default {
  input: './src/index.js',
  output: {
    format: 'esm',
    file: 'dist/bundle.js'
  },
  plugins: [
    commonjs(),
    svgSprites()
  ]
}

foo.js:

import MyIcon from './my-icon.svg'

// <svg><use xlink:href="#${MyIcon.id}"></use></svg>

Be use for Vue3.x Vite

vite.config.js :

import svgSprites from 'rollup-plugin-svg-sprites'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    svgSprites({
      exclude: ['node_modules/**']
    })
  ]
})

Vue SFC:

<template>
  <MyIcon />
</template>

<script setup>
import MyIcon from './my-icon.svg?vueComponent'
</script>

Vite + React

  • vite.config.js
import svgSprites from 'rollup-plugin-svg-sprites'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    svgSprites({
      include: ['./icons']
    })
  ]
})
  • App.jsx
import MyIcon from './my-icon.svg?jsx'

function App() {
  return (
    <div className="App">
      <MyIcon />
    </div>
  );
}

export default App;

A large number of SVGs

eg from vfox:

  • rollup.config.js:
import svgSprites from 'rollup-plugin-svg-sprites'
import requireContext from '@godxiaoji/rollup-plugin-require-context'

function kebabCase2PascalCase(name) {
  name = name.replace(/-(\w)/g, (all, letter) => {
    return letter.toUpperCase()
  })
  return name.substr(0, 1).toUpperCase() + name.substr(1)
}

export default {
  input: './src/load-svg.js',
  output: {
    format: 'esm',
    file: `lib/load-svg.js`,
    banner: '/* eslint-disable */'
  },
  plugins: [
    requireContext(),
    svgSprites({
      symbolId(filePath) {
        const paths = filePath
          .replace(/\\/g, '/')
          .split('assets/icons/')[1]
          .split('/')

        const fileName = paths.pop().replace('.svg', '')
        return (
          'icon-' + kebabCase2PascalCase([fileName].concat(paths).join('-'))
        )
      }
    })
  ],
}
  • ./src/load-svg.js:
const req = require.context('./assets/icons', true, /\.svg$/)
  • Output the library:
rollup -c
  • Use in Vue SFC:
<template>
  <svg>
    <use xlink:href="#icon-My"></use>
  </svg>
</template>

<script setup>
import './lib/load-svg'
</script>

Configuration

symbolId: (path: string) => string

How id attribute should be named.

eg:

svgSprite({
  symbolId(path, query) {
    return path.basename(path)
  }
})

exclude: string | string[]

A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore.

include: string | string[]

A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on.

vueComponent: boolean

Default: false

If true, when import ".svg" will return a Vue3.x Component. Priority level is weaker than import ".svg?vueComponent".

jsx: boolean

Default: false

If true, when import ".svg" will return a JSX Function. Priority level is weaker than import ".svg?jsx".

PS:

  • It use the new JSX transform, so the react version requirement is >=17.0.0, >=16.14.0 or >=15.7.0.
  • If JSX is used in Vue, "vueComponent" should be used instead.

LICENSE

MIT

Keywords

rollup

FAQs

Package last updated on 19 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts