How to use the NamedModulesPlugin function from webpack

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

webpack.NamedModulesPlugin is a plugin that gives names to unnamed JavaScript modules, making it easier to identify them during debugging.

160
161
162
163
164
165
166
167
168
169
    return 'script';
  },
  include: 'allChunks'
  //include: ['app']
}),
new webpack.NamedModulesPlugin(),
new WorkboxPlugin.GenerateSW({
  clientsClaim: true,
  skipWaiting: true,
  importWorkboxFrom: 'local',
fork icon50
star icon235
watch icon13

37
38
39
40
41
42
43
44
45
46
plugins: [
  new webpack.DefinePlugin({
    'process.env': require('../config/dev.env')
  }),
  new webpack.HotModuleReplacementPlugin(),
  new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
  new webpack.NoEmitOnErrorsPlugin(),
  // https://github.com/ampedandwired/html-webpack-plugin
  new HtmlWebpackPlugin({
    filename: 'index.html',
fork icon2
star icon5
watch icon1

+ 3 other calls in file

How does webpack.NamedModulesPlugin work?

webpack.NamedModulesPlugin is a plugin for the Webpack module bundler that assigns names to unnamed JavaScript modules, making it easier to identify them during debugging.

During the build process, Webpack generates unique module IDs for each module in the dependency graph, which are typically numbers. However, this can make it difficult to identify individual modules when debugging. The NamedModulesPlugin solves this problem by assigning readable names to the modules based on their path or other characteristics.

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... plugins: [ new webpack.NamedModulesPlugin() ] };

Now, when you run the Webpack build process, the NamedModulesPlugin will generate human-readable names for the modules based on their file path or other criteria. These names will be used in the Webpack output files, making it easier to identify and debug specific modules during development.

478
479
480
481
482
483
484
485
486
487
    "main"
  ],
  "minChunks": 2,
  "async": "common"
}),
new NamedModulesPlugin({}),
new AngularCompilerPlugin({
  "mainPath": "main.ts",
  "platform": 0,
  "hostReplacementPaths": {
fork icon0
star icon0
watch icon0

+ 2 other calls in file

Ai Example

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

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

In this example, we are creating an instance of webpack.NamedModulesPlugin and adding it to the plugins array in our Webpack configuration file. Now, when we run the Webpack build process, the plugin will generate human-readable names for the modules based on their file path or other criteria. These names will be used in the Webpack output files, making it easier to identify and debug specific modules during development. For example, instead of seeing a module ID like 0 in the console output during debugging, we might see a more descriptive name like ./src/components/Button.js. This can save time and effort when debugging complex applications with many modules.