How to use the SourceMapDevToolPlugin function from webpack

Find comprehensive JavaScript webpack.SourceMapDevToolPlugin code examples handpicked from public code repositorys.

webpack.SourceMapDevToolPlugin is a plugin that generates source maps for JavaScript files, allowing for easier debugging of the original code in the browser's developer tools.

194
195
196
197
198
199
200
201
202
203
    })
);

if (options.devtool === "source-map" && this.webpackConfig.devServer.port) {
    this.webpackConfig.plugins.push(
        new webpack.SourceMapDevToolPlugin({
            filename: '[file].map',
            publicPath: `https://localhost:${this.webpackConfig.devServer.port}/assets/`
        })
    );
fork icon138
star icon240
watch icon0

+ 3 other calls in file

88
89
90
91
92
93
94
95
96
97
resolve: {
    extensions: ['.tsx', '.ts', '.js', '.jsx', '.css'],
},
plugins: [
    new MiniCssExtractPlugin(),
    new SourceMapDevToolPlugin({
        filename: '[file].map',
    }),
],
optimization: {
fork icon502
star icon0
watch icon0

How does webpack.SourceMapDevToolPlugin work?

webpack.SourceMapDevToolPlugin is a plugin for the Webpack module bundler that generates source maps for JavaScript files. Source maps are files that provide a mapping between the compiled JavaScript code and the original source code, allowing for easier debugging of the original code in the browser's developer tools.

To use the plugin, you simply add an instance of it to your Webpack configuration file as a plugin, like this:

javascript
const webpack = require('webpack'); module.exports = { // other configuration options... devtool: 'source-map', plugins: [ new webpack.SourceMapDevToolPlugin({}) ] };

In this example, we are creating an instance of webpack.SourceMapDevToolPlugin and adding it to the plugins array in our Webpack configuration file.

We also set the devtool option to source-map, which tells Webpack to generate source maps for our JavaScript files.

Now, when we run the Webpack build process, the plugin will generate source maps for our JavaScript files. These source maps will be saved as separate files alongside the compiled JavaScript files.

When we load our website in the browser and open the developer tools, we can now view and debug the original source code, rather than the compiled code, by simply selecting the source map file in the developer tools. This makes it much easier to locate and fix issues in our code.

53
54
55
56
57
58
59
        test: /\.(jpg|jpeg|png|gif|mp3|svg)$/,
        use: ["file-loader"],
      },
    ],
  },
  plugins: [new webpack.SourceMapDevToolPlugin({})],
}
fork icon0
star icon0
watch icon1

78
79
80
81
82
83
84
85
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: joinPath("src/web-test/index.html"),
    }),
    new webpack.SourceMapDevToolPlugin({}),
  ],
}
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
const webpack = require("webpack");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
  },
  devtool: "source-map",
  plugins: [new webpack.SourceMapDevToolPlugin({})],
};

In this example, we are creating an instance of webpack.SourceMapDevToolPlugin and adding it to the plugins array in our Webpack configuration file. We also set the devtool option to source-map, which tells Webpack to generate source maps for our JavaScript files. Now, when we run the Webpack build process, the plugin will generate source maps for our JavaScript files. These source maps will be saved as separate files alongside the compiled JavaScript files. When we load our website in the browser and open the developer tools, we can now view and debug the original source code, rather than the compiled code, by simply selecting the source map file in the developer tools. This makes it much easier to locate and fix issues in our code.

39
40
41
42
43
44
45
46
47
48
},
// devtool: 'cheap-module-eval-source-map',
// devtool: false,
devtool: 'source-map',
// devtool: 'cheap-module-source-map',
// plugins: [new webpack.SourceMapDevToolPlugin({
//     filename: '[file].map',
//     // exclude: ['ExpenseForm.js'],
//     options: {
//         ignoreWarnings: [/Failed to parse source map/]
fork icon0
star icon0
watch icon0

+ 4 other calls in file

32
33
34
35
36
37
38
39
40
41
    'X-XSS-Protection': '1; mode=block',
    'Referrer-Policy': 'same-origin'
  }
},
plugins: [
  new webpack.SourceMapDevToolPlugin(sourceMapsConfig),
  new webpack.NormalModuleReplacementPlugin(/^any-promise$/, 'bluebird'),
  // new BundleAnalyzerPlugin(),
  new ImageminPlugin({
    disable: process.env.NODE_ENV !== 'production',
fork icon0
star icon0
watch icon0

9
10
11
12
13
14
15
16
17
18
const sourceMapOptions = {
    exclude: [/node_modules/, /vendor/],
    filename: '[file].map',
    moduleFilenameTemplate: `webpack-tabby-${options.name}:///[resource-path]`,
}
let SourceMapDevToolPlugin = webpack.SourceMapDevToolPlugin

if (process.env.CI) {
    sourceMapOptions.append = '\n//# sourceMappingURL=../../../app.asar.unpacked/assets/webpack/[url]'
}
fork icon0
star icon0
watch icon0