TheDocumentation Index
Fetch the complete documentation index at: https://docs.webpack.js.org/llms.txt
Use this file to discover all available pages before exploring further.
ProvidePlugin automatically loads modules instead of having to import or require them everywhere. When webpack encounters an identifier that matches a configured module, it automatically requires that module.
Usage
Configuration
Object mapping identifiers to modules. The value can be:
- A module name (string)
- An array with module name and property path
Examples
jQuery
Automatically load jQuery:$ or jQuery anywhere without importing:
Lodash
Provide lodash utilities:Specific Module Exports
Access specific exports from a module:React Without Import
With React 17+ and the new JSX transform, you don’t need this anymore as JSX no longer requires React to be in scope.
Process and Buffer (Node.js Polyfills)
Nested Properties
Access nested properties:Common Use Cases
Legacy Code Migration
When migrating legacy code that uses global variables:Testing Environment
Provide test utilities globally:Consistent API
Ensure consistent API across modules:How It Works
The plugin works by:- Scanning your code for free variables (identifiers used without being declared)
- When it finds a match with configured identifiers, it injects an import
- The import is added at the top of the module automatically
Tree Shaking
Best Practices
- Use ProvidePlugin sparingly, primarily for:
- Migrating legacy code
- Global utilities used everywhere
- Polyfills and shims
- Prefer explicit imports for better code clarity
- Consider tree shaking implications