Skip to content

Instantly share code, notes, and snippets.

@jhesgodi
Last active January 22, 2020 00:57
Show Gist options
  • Save jhesgodi/c7c9fc884a7975fa7ac5776403fe3412 to your computer and use it in GitHub Desktop.
Save jhesgodi/c7c9fc884a7975fa7ac5776403fe3412 to your computer and use it in GitHub Desktop.
SVG Sprite Loader: Multiple Sprites
SVGs Directory:
svg.js
icons/
|--collection1/
|--|--icon1.svg
|--|--icon4.svg
|--|--icon7.svg
|--collection2/
|--|--icon2.svg
|--|--icon5.svg
|--|--icon8.svg
|--collection3/
|--|--icon3.svg
|--|--icon6.svg
|--|--icon9.svg
Dist:
collection1.svg
--icon1.symbol
--icon2.symbol
--icon3.symbol
--icon4.symbol
--icon5.symbol
--icon6.symbol
--icon7.symbol
--icon8.symbol
--icon9.symbol
Expected Dist:
icons.js
collection1.svg
collection2.svg
collection3.svg
function requireAll(r) {
r.keys().forEach(r);
}
requireAll(require.context('./svg', true, /\.svg$/));
const path = require('path');
const webpack = require('webpack');
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
const cwd = process.cwd();
const pkg = require(path.join(cwd, 'package.json'));
const entry = path.join(cwd, 'src', 'icons', 'svgs.js');
const config = {
entry,
output: {
path: path.join(cwd, 'dist'),
filename: `${pkg.shortName}.js`,
libraryTarget: 'commonjs',
library: pkg.shortName
},
resolve: {
extensions: ['.js'],
modules: ['node_modules', path.resolve('..', '..', 'node_modules')]
},
module: {
rules: [
{ test: /\.js$/,
use: { loader: 'babel-loader' },
exclude: /node_modules/ },
{ test: /collection-1\\[a-zA-Z0-9_-]*.svg$/,
loader: 'svg-sprite-loader',
options: { extract: true, spriteFilename: 'collection-1.svg' } },
{ test: /collection-2\\[a-zA-Z0-9_-]*.svg$/,
loader: 'svg-sprite-loader',
options: { extract: true, spriteFilename: 'collection-2.svg' } },
{ test: /collection-3\\[a-zA-Z0-9_-]*.svg$/,
loader: 'svg-sprite-loader',
options: { extract: true, spriteFilename: 'collection-3.svg' } }
]
},
plugins: [
new webpack.LoaderOptionsPlugin(),
new webpack.optimize.UglifyJsPlugin(),
new SpriteLoaderPlugin({
plainSprite: true
})
],
};
module.exports = config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment