0

I am trying to get a list of files that end up in my build after treeshaking. I use conditional compilation constants to remove dev/debug code from bundle and Webpack is doing a good job with treeshaking, I don't find any dev code in the resulted build. But I want to write some code that would check wether some dev files ended up in production build by mistake.

I've tried NormalModuleReplacementPlugin but Webpack still load imports even if they are not used, it has to analyse all the sources to determine whether any of them are a dead code.

I've tried adding plugin to emit stage hook and going through all the modules and checking resources, but I still find references to my dev code file there, even though they don't end up in the resulted build.

class DevImportsMonitorPlugin {
    apply(compiler) {
        compiler.hooks.emit.tapAsync('DevImportsMonitor', (compilation, callback) => {
            compilation.chunks.forEach(chunk => {
                // explore chunk
                chunk.getModules().forEach(module => {
                    // explore modules
                })
            });
            callback();
        });
    }
}

I've put a breakpoint there and explored the context of chunks and modules, but it still has references to the dev/test modules.

Maybe there is a way to determine maybe what module was removed from the output?

n1kk
  • 742
  • 9
  • 9

1 Answers1

0

I just tried this yesterday it's pretty cool; https://www.excitoninteractive.com/articles/read/65/webpack4/step-by-step-bundle-analyzer-plugin

Ted Fitzpatrick
  • 910
  • 1
  • 8
  • 18
  • My goal is to detect presence of a source file in the build programatically and fail the build if so. I can ofc analyze and check it manually, but BundleAnalyzer is not very useful here since my source code ends up being reported as "Main.ts + 134 concatenated modules". – n1kk Oct 17 '19 at 07:44
  • yes, be aware that the bundle-analyizer cannot show the treeshaked modules (because terser does this) – pscheit Oct 17 '20 at 04:59