@title
Webpack 简介
@intro
使用基于 Webpack 的工具创建 Angular 应用
@description
[**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}
# Contents
# 目录
* [What is Webpack?](guide/webpack#what-is-webpack)
[什么是Webpack?](guide/webpack#what-is-webpack)
* [Entries and outputs](guide/webpack#entries-outputs)
[入口与输出](guide/webpack#entries-outputs)
* [Multiple bundles](guide/webpack#multiple-bundles)
[多重包](guide/webpack#multiple-bundles)
* [Loaders](guide/webpack#loaders)
[加载器](guide/webpack#loaders)
* [Plugins](guide/webpack#plugins)
[插件](guide/webpack#plugins)
* [Configuring Webpack](guide/webpack#configure-webpack)
[配置Webpack](guide/webpack#configure-webpack)
* [Polyfills](guide/webpack#polyfills)
* [Common configuration](guide/webpack#common-configuration)
[公共配置](guide/webpack#common-configuration)
* [Inside `webpack.common.js`](guide/webpack#inside-webpack-commonjs)
[深入`webpack.common.js`](guide/webpack#inside-webpack-commonjs)
* [entry](guide/webpack#common-entries)
[入口](guide/webpack#common-entries)
* [resolve extension-less imports](guide/webpack#common-resolves)
[解析无扩展名的导入](guide/webpack#common-resolves)
* [`module.rules`](guide/webpack#common-rules)
* [Plugins](guide/webpack#plugins)
[插件](guide/webpack#plugins)
* [`CommonsChunkPlugin`](guide/webpack#commons-chunk-plugin)
* [`HtmlWebpackPlugin`](guide/webpack#html-webpack-plugin)
* [Environment specific configuration](guide/webpack#environment-configuration)
[针对特定环境进行配置](guide/webpack#environment-configuration)
* [Development configuration](guide/webpack#development-configuration)
[开发环境配置](guide/webpack#development-configuration)
* [Production configuration](guide/webpack#production-configuration)
[生产环境配置](guide/webpack#production-configuration)
* [Test configuration](guide/webpack#test-configuration)
[测试环境配置](guide/webpack#test-configuration)
* [Trying it out](guide/webpack#try)
[试一下](guide/webpack#try)
* [Highlights](guide/webpack#highlights)
[重点](guide/webpack#highlights)
* [Conclusion](guide/webpack#conclusion)
[总结](guide/webpack#conclusion)
You can also download the final result.
你还可以点这里下载最终结果。
{@a what-is-webpack}
## 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 entries-outputs}
### 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`: