question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Generate SVG sprite without JS import

See original GitHub issue

Is there a way to generate the sprite without importing each icon in my JavaScript file?

This is my webpack config and it does not work unless I use import icon from './path/to/icon' although I set include.

module: {
    rules: [
      {
        test: /\.svg$/,
        loader: 'svg-sprite-loader',
        include: path.resolve(__dirname, 'source/assets/images/icons'),
        options: {
          extract: true,
          spriteFilename: 'assets/images/icons.svg',
        },
      },
    ],
  },
  plugins: [
    new SpriteLoaderPlugin(),
  ],

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
kisenkacommented, Nov 11, 2017

You can create entrypoint dynamically:

const glob = require('glob').sync;

module.exports = {
  entry: {
    sprite: glob('./source/assets/images/icons/*.svg')
  },

  module: {
    rules: [
      {
        test: /\.svg$/,
        loader: 'svg-sprite-loader',
        include: path.resolve(__dirname, 'source/assets/images/icons'),
        options: {
          extract: true,
          spriteFilename: 'assets/images/icons.svg'
        },
      },
    ],
  },

  plugins: [
    new SpriteLoaderPlugin()
  ]
};
5reactions
kisenkacommented, Nov 10, 2017

You can use require.context webpack feature. Somewhere in entrypoint of your application:

const svgModules = require.context('source/assets/images/icons', false, /\.svg$/);
svgModules.keys().forEach(svgModules);
Read more comments on GitHub >

github_iconTop Results From Across the Web

SVG sprite in external file - Stack Overflow
Try this: Create an SVG file, sprites.svg. Place the following in it: <svg version="1.1" xmlns="http://www.w3.org/2000/svg" ...
Read more >
Icon System with SVG Sprites - CSS-Tricks
Combine the .svg files · Inject that SVG at the top of the document · Use the icons wherever · Yay: you can...
Read more >
Support SVG's in JS - Malibu - Quintype for Developers
In this chapter, we are going to discuss how to use SVGs in JS without ... Thus we are using SVG sprite loader...
Read more >
Bootstrap Icons · Official open source SVG icon library for ...
Include them anyway you like—SVGs, SVG sprite, or web fonts. Use them with or without Bootstrap in any project. npm i bootstrap-icons.
Read more >
svg-sprite - npm
svg -sprite is a low-level Node.js module that takes a bunch of SVG files, optimizes them and bakes them into SVG sprites of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found