How to use the ProvidePlugin function from webpack

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

webpack.ProvidePlugin is a plugin in the Webpack module bundler that automatically loads modules and makes them available as variables in every module.

48
49
50
51
52
53
54
55
56
57
// As of Webpack 5, Node APIs are no longer automatically polyfilled.
// resolve.fallback resolves the API to its NPM package, and the plugin
// makes the API available as a global.
const nodePolyfillConfig = {
  plugins: [
    new webpack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer'],
      events: 'events',
      stream: 'stream-browserify',
      path: 'path-browserify',
fork icon444
star icon764
watch icon80

51
52
53
54
55
56
57
58
59
60
forbidEval: true,

webpack: {
  plugins: [
    // required for ansi-colors to work in the browser
    new webpack.ProvidePlugin({ process: 'process/browser' }),

    // uncomment this to see a tree view of what ember-cli-auto-import builds
    // new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)(),
  ],
fork icon321
star icon0
watch icon270

How does webpack.ProvidePlugin work?

webpack.ProvidePlugin works by taking an object as input, where each key is a variable name and each value is the name of a module.

When Webpack encounters a reference to one of the provided variable names in a module, it automatically loads the corresponding module and makes it available as a variable in that module.

This can save developers the effort of manually importing and requiring modules in each module where they are needed.

For example, if we provide jQuery as a variable name and jquery as the module name, then we can use $ or jQuery in our code without having to explicitly import the jQuery module.

webpack.ProvidePlugin is useful when working with libraries and frameworks that rely on global variables or modules, such as jQuery and React.

By using webpack.ProvidePlugin, we can easily ensure that the necessary modules are available in every module without the need for repetitive imports or requires.

93
94
95
96
97
98
99
100
101
102
      },
    ],
  }),
);
config.plugins.push(
  new ProvidePlugin({
    Buffer: ['buffer', 'Buffer'],
  }),
);
return config;
fork icon0
star icon1
watch icon1

324
325
326
327
328
329
330
331
332
333
    'scheduler/tracing': 'scheduler/tracing-profiling',
  }),
  ...(modules.webpackAliases || {}),
},
plugins: [
  // new webpack.ProvidePlugin({
  //   Buffer: ['buffer', 'Buffer'],
  // }),
  // Prevents users from importing files from outside of src/ (or node_modules/).
  // This often causes confusion because we only process files within src/ with babel.
fork icon0
star icon0
watch icon2

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const webpack = require("webpack");

const config = {
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: __dirname + "/dist",
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
    }),
  ],
};

module.exports = config;

In this example, we use webpack.ProvidePlugin to automatically load the jquery module and make it available as a variable in every module. We define a configuration object for Webpack that specifies the entry point and output file, as well as a plugins array that contains a new instance of webpack.ProvidePlugin. We pass an object to the ProvidePlugin constructor with two key-value pairs: $ mapped to "jquery" and jQuery also mapped to "jquery". This tells Webpack to load the jquery module whenever it encounters references to $ or jQuery in any module, without the need for explicit imports or requires. By using webpack.ProvidePlugin in this way, we can simplify our code and ensure that the necessary modules are available in every module without the need for repetitive imports or requires.

40
41
42
43
44
45
46
47
48
49
      './stylesheet/styles.scss'
    ]
  },
  plugins: [
    // Make process Browser
    new Webpack.ProvidePlugin({
      process: 'process/browser'
    })
  ]
});
fork icon0
star icon0
watch icon0

105
106
107
108
109
110
111
112
113
114
new webpack.BannerPlugin({
  banner: '"use weex:vue";',
  raw: true,
  exclude: 'Vue'
}),
new webpack.ProvidePlugin(provide),
new WebpackErrorsPlugin({
  onErrors,
  onWarnings
}),
fork icon0
star icon0
watch icon506

63
64
65
66
67
68
69
70
71
72
if (onlyPrintingGraph) {
  plugins.push(new AccessDependenciesPlugin());
}

if (targetDeletesNodeGlobals) {
  plugins.push(new webpack.ProvidePlugin({
    Buffer: ['@electron/internal/common/webpack-provider', 'Buffer'],
    global: ['@electron/internal/common/webpack-provider', '_global'],
    process: ['@electron/internal/common/webpack-provider', 'process']
  }));
fork icon0
star icon0
watch icon0

+ 5 other calls in file