JetBrains / svg-sprite-loader

Webpack loader for creating SVG sprites.
MIT License
2.01k stars 272 forks source link

Symbol with id "filter" already exists. It happens when you require SVGs with the same file name from different folders. #411

Closed kush1960 closed 3 years ago

kush1960 commented 3 years ago

Hi after much trial and error I've managed to get extract-svg-sprite-webpack-plugin to successfully build a spritesheet from a folder of SVGs and transpile my SCSS path to the correct symbol reference.

However I get this annoying warning every-time the build task runs: WARNING in ./src/svg/filter.svg Module Warning (from ./node_modules/extract-svg-sprite-webpack-plugin/lib/loader.js): Symbol with id "filter" already exists. It happens when you require SVGs with the same file name from different folders. Set "symbolId" option to more specific, e.g. "[path]-[name]-[hash]" @ ./src/sass/style.scss (./node_modules/css-loader/dist/cjs.js!./node_modules/extract-svg-sprite-webpack-plugin/lib/css-loader.js!./node_modules/sass-loader/dist/cjs.js!./src/sass/style.scss) 7:36-64

In my use case, this warning doesn't make sense since there is only one reference to the SVG, but I wonder if I have my config wrong and it's somehow running twice?

FYI I'm using Webpack with Wordpress, so I'm only interested in building a sprite sheet from a folder of icons and fixing my CSS references to the icon id... As I said, that's all working. It's just I get this annoying error!

Here's my webpack.config.js:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const SpritePlugin = require('extract-svg-sprite-webpack-plugin');

module.exports = {
  context: __dirname,
  entry: {
    frontend: ['./src/index.js', './src/sass/style.scss'],
    //customizer: './src/customizer.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name]-bundle.js'
  },
  mode: 'development',
  devtool: 'cheap-eval-source-map',
  module: {
    rules: [
      {
        enforce: 'pre',
        exclude: /node_modules/,
        test: /\.jsx$/,
        loader: 'eslint-loader'
      },
      {
        test: /\.jsx?$/,
        loader: 'babel-loader'
            },
            {
                test: /\.svg$/,
                loader: SpritePlugin.loader,
            },          
      {
        test: /\.s?css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', SpritePlugin.cssLoader, 'sass-loader']
            },
            {
        test: /\.(jpe?g|png|gif|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'image-webpack-loader',
        enforce: 'pre'
            },   
      {
            test: /\.(jpe?g|png|gif)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'img/',
              name: '[name].[ext]'
            }
          },
          'img-loader'
        ]
      },
      {
        test: /\.(woff(2)?|ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'fonts/',
              name: '[name].[ext]'
            }
          }
        ]
      }
    ]
  },
    plugins: [
        new SpritePlugin(
            {
                //symbolId: 'icon-[path]-[name]-[hash]'
                //plainSprite: true
            }
    ),
    new StyleLintPlugin(),
    new MiniCssExtractPlugin({
    filename: '[name].css'
    }),
  new BrowserSyncPlugin({
        files: '**/*.php',
        proxy: 'https://base.six',
        open: false
    })
  ],
  optimization: {
    minimizer: [new UglifyJsPlugin(), new OptimizeCssAssetsPlugin()]
  },
  performance: {
    maxEntrypointSize: 512000,
    maxAssetSize: 512000
  }
};

If anyone can help me, I'll gladly send buy you a coffee/beer!

Thank you, I'm at the end of my knowledge with this :(

kush1960 commented 3 years ago

Looking into this further, it appears that SpritePlugin.loader is generating the sprite sheet twice. If I remove one of the entry points below:

entry: {
        frontend: ['./src/sass/style.scss', './src/index.js'],
}

It works without errors.

kush1960 commented 3 years ago

Ignore me, I just noticed I had this in my index.js file require.context('./svg', true, /\.svg$/);