include ../_util-fns :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 we use throughout the documentation. In this guide we get a taste of Webpack and how to use it with Angular 2 applications. 它是我们在文档中到处使用的*SystemJS*的一个优秀替代品。这篇指南会带我们尝尝Webpack的滋味,并学习如何在Angular 2程序中使用它。 ## Table of contents ## 目录 [What is Webpack?](#what-is-webpack) [什么是Webpack?](#what-is-webpack) * [Entries and outputs](#entries-outputs) * [入口与输出](#entries-outputs) * [Loaders](#loaders) * [加载器](#loaders) * [Plugins](#plugins) * [插件](#plugins) [Configuring Webpack](#configure-webpack) [配置Webpack](#configure-webpack) * [Common configuration](#common-configuration) * [公共配置](#common-configuration) * [Development configuration](#development-configuration) * [开发环境配置](#development-configuration) * [Production configuration](#production-configuration) * [生产环境配置](#production-configuration) * [Test configuration](#test-configuration) * [测试环境配置](#test-configuration) [Trying it out](#try) [试一下](#try) [Conclusions](#conclusions) [总结](#conclusions) .l-main-section :marked ## What is Webpack? ## 什么是Webpack? Webpack is a powerful module bundler. A _bundle_ is a JavaScript file that incorporate _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 plugin "loaders" Webpack can preprocess and minify different non-JavaScript files such as TypeScript, SASS, and LESS files. Webpack会遍历你应用中的所有源码,查找`import`语句,构建出依赖图谱,并产出一个(或多个)_包儿_。 通过“加载器(loaders)”插件,Webpack可以对各种非JavaScript文件进行预处理和最小化(Minify),比如TypeScript、SASS和LESS文件等。 We 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 ### 入口与输出 We feed Webpack with one or more *entry* files and let it find and incorporate the dependencies that radiate from those entries. In this example, we start from the application's root file, `src/app.ts`: 我们给Webpack提供一个或多个*入口*文件,来让它查找与合并那些从这些入口点发散出去的依赖。 在下面这个例子中,我们的入口点是该应用的根文件`src/app.ts`: +makeExample('webpack/ts-snippets/webpack.config.snippets.ts', 'one-entry', 'webpack.config.js (single entry)')(format=".") :marked Webpack inspects that file and traverses its `import` dependencies recursively. Webpack探查那个文件,并且递归遍历它的`import`依赖。 +makeExample('webpack/ts-snippets/webpack.config.snippets.ts', 'app-example', 'src/app.ts')(format=".") :marked Here it sees that we're importing *@angular/core* so it adds that to its dependency list for (potential) inclusion in the bundle. It opens *@angular/core* and follows _its_ network of `import` statements until it has build the complete dependency graph from `app.ts` down. 这里,Webpack看到我们正在导入*@angular/core*,于是就这个文件加入到它的依赖列表里,为(有可能)把该文件打进包儿中做准备。 它打开*@angular/core*并追踪由_该文件的_`import`语句构成的网络,直到构建出从`app.ts`往下的整个依赖图谱。 Then it **outputs** these files to the `app.js` _bundle file_ designated in configuration: 然后它把这些文件**输出**到当前配置所指定的_包儿文件_`app.js`中: +makeExample('webpack/ts-snippets/webpack.config.snippets.ts', 'one-output', 'webpack.config.js (single output)')(format=".") :marked This `app.js` output bundle is a single JavaScript file that contains our application source and its dependencies. We'll load it later with a <script> tag in our index.html. 这个`app.js`输出包儿是个单一的JavaScript文件,它包含我们程序的源码以及它的所有依赖。 后面我们将在`index.html`中用`