网页
综合
更新时间 2026-11-14 03:58:12
Concepts | webpack
自动蜘蛛
webpack.js.org
正文约 6000 字
At its core, webpack is a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph from one or more entry points and then combines every module your project needs into one or more bundles, which are static assets to serve your content from.
Learn more about JavaScript modules and webpack modules here.
Since version 4.0.0, webpack does not require a configuration file to bundle your project. Nevertheless, it is incredibly configurable to better fit your needs.
To get started you only need to understand its Core Concepts:
This document is intended to give a high-level overview of these concepts, while providing links to detailed concept-specific use cases.
For a better understanding of the ideas behind module bundlers and how they work under the hood, consult these resources:
An entry point indicates which module webpack should use to begin building out its internal dependency graph. Webpack will figure out which other modules and libraries that entry point depends on (directly and indirectly).
By default its value is ./src/index.js, but you can specify a different (or multiple) entry points by setting an entry property in the webpack configuration. For example:
When running webpack without a configuration file, the entry defaults to './src', which webpack resolves to ./src/index.js through resolve.mainFiles and resolve.extensions. If that file does not exist — even if your src/ directory does — webpack will throw: Module not found: Error: Can't resolve './src' in '/path/to/project' The error names './src' because that is the actual entry request; index.js is only the default file looked up inside it. To use a different entry filename, add a webpack.config.js as shown in the example above.
Learn more in the entry points section.
The output property tells webpack where to emit the bundles it creates and how to name these files. It defaults to ./dist/main.js for the main output file and to the ./dist folder for any other generated file.
You can configure this part of the process by specifying an output field in your configuration:
In the example above, we use the output.filename and the output.path properties to tell webpack the name of our bundle and where we want it to be emitted to. In case you're wondering about the path module being imported at the top, it is a core Node.js module that gets used to manipulate file paths.
The output property has many more configurable features. If you want to learn about the concepts behind it, you can read more in the output section.
Out of the box, webpack understands JavaScript, JSON and WebAssembly files, as well as CSS and HTML through its built-in support (Native CSS, Native HTML), which is enabled by default. Loaders allow webpack to process other types of files and convert them into valid modules that can be consumed by your application and added to the dependency graph.
One of webpack's specific features is the ability to import any type of module, e.g. .css files, which may not be supported by other bundlers or task runners. We feel this extension of the language is warranted as it allows developers to build a more accurate dependency graph.
At a high level, loaders have two properties in your webpack configuration:
The configuration above has defined a rules property for a single module with two required properties: test and use. This tells webpack's compiler the following:
"Hey webpack compiler, when you come across a path that resolves to a '.js' file inside of a require()/import statement, use the babel-loader to transform it before you add it to the bundle."
It is important to remember that when defining rules in your webpack config, you are defining them under module.rules and not rules. For your benefit, webpack will warn you if this is done incorrectly.
Keep in mind that when using regex to match files, you may not quote it. i.e /\.txt$/ is not the same as '/\.txt$/' or "/\.txt$/". The former instructs webpack to match any file that ends with .txt and the latter instructs webpack to match a single file with an absolute path '.txt'; this is likely not your intention.
You can check further customization when including loaders in the loaders section.
While loaders are used to transform certain types of modules, plugins can be leveraged to perform a wider range of tasks like bundle optimization, asset management and injection of environment variables.
Check out the plugin interface and how to use it to extend webpack's capabilities.
In order to use a plugin, you need to import it and add it to the plugins array. Most plugins are customizable through options. Since you can use a plugin multiple times in a configuration for different purposes, you need to create an instance of it by calling it with the new operator.
In the example above, the ProgressPlugin customizes how build progress is reported. Your src/index.html is the entry: webpack bundles the scripts and styles it references and rewrites those URLs to the built filenames, so no plugin is needed to produce the page — see Native HTML.
There are many plugins that webpack provides out of the box! Check out the list of plugins.
Using plugins in your webpack configuration is straightforward. However, there are many use cases that are worth further exploration. Learn more about them here.
By setting the mode parameter to either development, production or none, you can enable webpack's built-in optimizations that correspond to each environment. The default value is production.
Learn more about the mode configuration here and what optimizations take place on each value.
Webpack supports all browsers that are ES5-compliant (IE8 and below are not supported). Webpack needs Promise for import() and require.ensure(). If you want to support older browsers, you will need to load a polyfill before using these expressions.
Webpack 5 requires Node.js version 10.13.0 or later.
Copyright OpenJS Foundation and webpack contri
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。