cascornelissen / svg-spritemap-webpack-plugin

SVG spritemap plugin for webpack
MIT License
207 stars 50 forks source link

dummy-module chunk is not deleted #117

Closed nikolay-borzov closed 4 years ago

nikolay-borzov commented 4 years ago

webpack: 4.44.1 svg-spritemap-webpack-plugin: 3.5.7

Config like:

module.exports = {
  mode: 'development',
  output: {
    path: '/shop-static/assets/',
    hashDigestLength: 8,
    filename: './js/[name].js',
    chunkFilename: './js/[name].js'
  },
  plugins: [
    new SVGSpritemapPlugin('./src/svg-icons/*.svg', {
      output: {
        filename: 'spritemap',
        svgo: {
          plugins: [{ removeTitle: true }]
        }
      }
    })
  ]
};

When I added console.log here:

...
// Fetch existing filenames from all instances of this plugin
const filenames = compilation.options.plugins.filter((plugin) => plugin instanceof SVGSpritemapPlugin).map((plugin) => plugin.filename);
compilation.chunks.forEach((chunk) => {
  console.log(chunk.name, outputOptions.chunk.name)
...

I saw:

spritemap~._spritemap-dummy-module~1acb1ac1 spritemap
cascornelissen commented 4 years ago

I can't seem to reproduce this. I've modified the simple example to basically match the configuration you provided:

const path = require('path');
const SVGSpritemapPlugin = require('../../lib');

module.exports = {
    mode: 'development',
    output: {
        path: path.join(process.cwd(), '/shop-static/assets/'),
        hashDigestLength: 8,
        filename: './js/[name].js',
        chunkFilename: './js/[name].js'
    },
    plugins: [
        new SVGSpritemapPlugin('src/**/*.svg', {
            output: {
                filename: 'spritemap',
                svgo: {
                    plugins: [{ removeTitle: true }]
                }
            }
        })
    ]
};

And it's not generating a file for the chunk on my end, just a js/main.js and spritemap file. Do you have some more information/could you debug it a bit more yourself?


Couple of notes:

nikolay-borzov commented 4 years ago

I'll try to create a reproducible example in a separate repo. Maybe it's somehow caused by splitting entry point in chunks

nikolay-borzov commented 4 years ago

Here https://github.com/nikolay-borzov/svg-spritemap-webpack-plugin-117. Looks like setting

 optimization: {
    splitChunks: {
      minSize: Infinity,
      maxSize: Infinity
    }
  }

causes the issue

cascornelissen commented 4 years ago

Interesting, any reason you're setting both the minSize and maxSize to Infinity? That doesn't make any sense to me on a logical level, e.g. why would you want to split chunks only when their size is infinite? 🤯

nikolay-borzov commented 4 years ago

It's complicated) Setting Infinity size prevents webpack from spilling chunks into multiple files because of the size. This way we can get the same files number as in the production build. This allows mapping development chunks to production chunks while debugging the production site.

cascornelissen commented 4 years ago

I'll take a look at what happens when using those settings but I don't understand why you wouldn't use mode: 'production' together with sourcemaps in that scenario 🤯

nikolay-borzov commented 4 years ago

why you wouldn't use mode: 'production' together with sourcemaps in that scenario 🤯

"Security" (Customer requirement)

By the way, the issue reproducible even when minSize and maxSize have sensible values like 1024 and 2048

cascornelissen commented 4 years ago

Alright, looks like the splitting of chunks does indeed cause issues with cleaning up, I've added some extra logic 3a9bdb3 that should handle this. Could you test if this solves the issue on your end as well? You can install the version directly from that branch by running

npm install --no-save cascornelissen/svg-spritemap-webpack-plugin#117

"Security" (Customer requirement)

As long as you're not deploying the sourcemaps to your actual production environment I don't see how that's any different to building the application without minification etc. but if it works it works right 🤷🏼‍♂️

nikolay-borzov commented 4 years ago

I confirm dummy-module is no longer remains after the build on my project.

cascornelissen commented 4 years ago

@nikolay-borzov, the fix for this issue has been released in version 3.5.8 🚀 Thanks for reporting the bug and helping me debug it!

nikolay-borzov commented 4 years ago

Thank you for the quick fix!

nikolay-borzov commented 4 years ago

@cascornelissen, is it possible to exclude the dummy module from stats.compilation.chunks?

cascornelissen commented 4 years ago

I'm not sure but I don't think it is. The dummy module needs to be created and we can't get rid of it until the files are emitted and I'm pretty sure the stats are combined before that point...

But if you want to try it out, you can give it a go I guess.

nikolay-borzov commented 4 years ago

I see. Thanks.