include ../_util-fns
// The docs standard h4 style uppercases, making code terms unreadable. Override it.
style.
h4 {font-size: 17px !important; text-transform: none !important;}
.syntax { font-family: Consolas, 'Lucida Sans', Courier, sans-serif; color: black; font-size: 85%; }
:marked
[**Webpack**](https://webpack.github.io/) is a popular module bundler,
a tool for bundling application source code in convenient _chunks_
and for loading that code from a server into a browser.
[**Webpack**](https://webpack.github.io/)是一个广受欢迎的模块打包器,
这个工具用来把程序源码打包到一些方便易用的_块_中,以便把这些代码从服务器加载到浏览器中。
It's an excellent alternative to the *SystemJS* approach used elsewhere in the documentation.
This guide offers a taste of Webpack and explains how to use it with Angular applications.
它是我们在文档中到处使用的*SystemJS*的一个优秀替代品。这篇指南会带我们尝尝Webpack的滋味,并解释如何在Angular程序中使用它。
a#top
:marked
# Contents
# 目录
*[What is Webpack?](#what-is-webpack)
[什么是Webpack?](#what-is-webpack)
* [Entries and outputs](#entries-outputs)
[入口与输出](#entries-outputs)
* [Multiple bundles](#multiple-bundles)
[多重包](#multiple-bundles)
* [Loaders](#loaders)
[加载器](#loaders)
* [Plugins](#plugins)
[插件](#plugins)
* [Configuring Webpack](#configure-webpack)
[配置Webpack](#configure-webpack)
* [Polyfills](#polyfills)
* [Common configuration](#common-configuration)
[公共配置](#common-configuration)
* [Inside `webpack.common.js`](#inside-webpack-commonjs)
[深入`webpack.common.js`](#inside-webpack-commonjs)
* [entry](#common-entries)
[入口](#common-entries)
* [resolve extension-less imports](#common-resolves)
[解析无扩展名的导入](#common-resolves)
* [`module.rules`](#common-rules)
* [Plugins](#plugins)
[插件](#plugins)
* [`CommonsChunkPlugin`](#commons-chunk-plugin)
* [`HtmlWebpackPlugin`](#html-webpack-plugin)
* [Environment specific configuration](#environment-configuration)
[针对特定环境进行配置](#environment-configuration)
* [Development configuration](#development-configuration)
[开发环境配置](#development-configuration)
* [Production configuration](#production-configuration)
[生产环境配置](#production-configuration)
* [Test configuration](#test-configuration)
[测试环境配置](#test-configuration)
* [Trying it out](#try)
[试一下](#try)
* [Highlights](#highlights)
[重点](#highlights)
* [Conclusion](#conclusion)
[总结](#conclusion)
You can also download the final result.
你还可以点这里下载最终结果。
.l-main-section
:marked
## What is Webpack?
## 什么是Webpack?
Webpack is a powerful module bundler.
A _bundle_ is a JavaScript file that incorporates _assets_ that *belong* together and
should be served to the client in a response to a single file request.
A bundle can include JavaScript, CSS styles, HTML, and almost any other kind of file.
Webpack是一个强力的模块打包器。
所谓_包(bundle)_就是一个JavaScript文件,它把一堆_资源(assets)_合并在一起,以便它们可以在同一个文件请求中发回给客户端。
包中可以包含JavaScript、CSS样式、HTML以及很多其它类型的文件。
Webpack roams over your application source code,
looking for `import` statements, building a dependency graph, and emitting one or more _bundles_.
With plugins and rules, Webpack can preprocess and minify different non-JavaScript files such as TypeScript, SASS, and LESS files.
Webpack会遍历你应用中的所有源码,查找`import`语句,构建出依赖图谱,并产出一个(或多个)_包_。
通过插件和规则,Webpack可以对各种非JavaScript文件进行预处理和最小化(Minify),比如TypeScript、SASS和LESS文件等。
You determine what Webpack does and how it does it with a JavaScript configuration file, `webpack.config.js`.
我们通过一个JavaScript配置文件`webpack.config.js`来决定Webpack做什么以及如何做。
a(id="entries-outputs")
.l-main-section
:marked
### Entries and outputs
### 入口与输出
You supply Webpack with one or more *entry* files and let it find and incorporate the dependencies that radiate from those entries.
The one entry point file in this example is the application's root file, `src/main.ts`:
我们给Webpack提供一个或多个*入口*文件,来让它查找与合并那些从这些入口点发散出去的依赖。
在下面这个例子中,我们的入口点是该应用的根文件`src/app.ts`:
+makeExample('webpack/ts/config/webpack.common.js', 'one-entry', 'webpack.config.js (single entry)')(format=".")
:marked
Webpack inspects that file and traverses its `import` dependencies recursively.
Webpack探查那个文件,并且递归遍历它的`import`依赖。
+makeExample('webpack/ts/src/app/app.component.ts', 'component', 'src/main.ts')(format=".")
:marked
It sees that you're importing `@angular/core` so it adds that to its dependency list for potential inclusion in the bundle.
It opens the `@angular/core` file and follows _its_ network of `import` statements until it has built the complete dependency graph from `main.ts` down.
这里,Webpack看到我们正在导入`@angular/core`,于是就这个文件加入到它的依赖列表里,为(有可能)把该文件打进包中做准备。
它打开`@angular/core`并追踪由_该文件的_`import`语句构成的网络,直到构建出从`main.ts`往下的整个依赖图谱。
Then it **outputs** these files to the `app.js` _bundle file_ designated in configuration:
然后它把这些文件**输出**到当前配置所指定的_包文件_`app.js`中:
.code-example
code-example(name="webpack.config.js (single output)" language="javascript").
output: {
filename: 'app.js'
}
:marked
This `app.js` output bundle is a single JavaScript file that contains the application source and its dependencies.
You'll load it later with a `