# Webpack: An Introduction # Webpack 简介 [**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} 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`: Webpack inspects that file and traverses its `import` dependencies recursively. Webpack 探查那个文件,并且递归遍历它的 `import` 依赖。 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` 中: output: { filename: 'app.js' } 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 `