Search code examples
javascriptwebpacksource-maps

Is it possible to remove "sourcesContent" from webpack source maps?


We're working on a large React application and the sourcemaps coming out of our webpack build have reached over 12MB. This seems to be causing problems in the Chrome dev tools where intermittently they fail to parse (Failed to parse SourceMap error).

We're now loading the source files from the local file system and have altered the webpack config to change the reference location like so:

output: {
  devtoolModuleFilenameTemplate: "file://[absolute-resource-path]",
  devtoolFallbackModuleFilenameTemplate: "file://[absolute-resource-path]?[hash]",
}

This is working fine and the devtools now load the source files from the filesystem, but it doesn't stop them from being compiled into the sourcemaps under "sourcesContent". I can't find any reference in the docs and there doesn't seem to be an option to turn this off.


Solution

  • Turns out there is a new(ish) undocumented (at time of writing) way to do this with webpack after all, the noSources option of the webpack.SourceMapDevToolPlugin:

    const webpackConfig = {
        devtool: false,
        plugins: [
            new webpack.SourceMapDevToolPlugin( {
                moduleFilenameTemplate: 'file://[absolute-resource-path]',
                fallbackModuleFilenameTemplate: 'file://[absolute-resource-path]?[hash]',
                noSources: true
            } ) 
        ],
        ...
    };