2017-05-26 21:28:06 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# Webpack: An Introduction
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-30 00:03:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# Webpack简介
  
						 
					
						
							
								
									
										
										
										
											2017-03-30 20:04:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< style >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  h4 {font-size: 17px !important; text-transform: none !important;}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  .syntax { font-family: Consolas, 'Lucida Sans', Courier, sans-serif; color: black; font-size: 85%; }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / style >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								[**Webpack** ](https://webpack.github.io/ ) is a popular module bundler,
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								a tool for bundling application source code in convenient _chunks_ 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								and for loading that code from a server into a browser.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								[**Webpack** ](https://webpack.github.io/ )是一个广受欢迎的模块打包器,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								这个工具用来把程序源码打包到一些方便易用的_块_中, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								它是我们在文档中到处使用的*SystemJS*的一个优秀替代品。这篇指南会带我们尝尝Webpack的滋味, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								{@a  top}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-04 18:46:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								<!-- 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# Contents
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# 目录
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [What is Webpack? ](guide/webpack#what-is-webpack ) 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								   [什么是Webpack?  ](guide/webpack#what-is-webpack )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-11 15:36:40 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Entries and outputs ](guide/webpack#entries-outputs )
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								     [入口与输出 ](guide/webpack#entries-outputs )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Multiple bundles ](guide/webpack#multiple-bundles )
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								     [多重包 ](guide/webpack#multiple-bundles )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-11 15:36:40 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Loaders ](guide/webpack#loaders )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								     [加载器 ](guide/webpack#loaders )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-11 15:36:40 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Plugins ](guide/webpack#plugins )
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								     [插件 ](guide/webpack#plugins )
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [Configuring Webpack ](guide/webpack#configure-webpack ) 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   [配置Webpack ](guide/webpack#configure-webpack )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Polyfills ](guide/webpack#polyfills )
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-11 15:36:40 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Common configuration ](guide/webpack#common-configuration )
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								     [公共配置 ](guide/webpack#common-configuration )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Inside `webpack.common.js` ](guide/webpack#inside-webpack-commonjs )
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								     [深入`webpack.common.js` ](guide/webpack#inside-webpack-commonjs )
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    *  [entry ](guide/webpack#common-entries )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								       [入口 ](guide/webpack#common-entries )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    *  [resolve extension-less imports ](guide/webpack#common-resolves )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								       [解析无扩展名的导入 ](guide/webpack#common-resolves )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    *  [`module.rules` ](guide/webpack#common-rules )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    *  [Plugins ](guide/webpack#plugins )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								       [插件 ](guide/webpack#plugins )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    *  [`CommonsChunkPlugin` ](guide/webpack#commons-chunk-plugin )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    *  [`HtmlWebpackPlugin` ](guide/webpack#html-webpack-plugin )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Environment specific configuration ](guide/webpack#environment-configuration )
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								     [针对特定环境进行配置 ](guide/webpack#environment-configuration )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-11 15:36:40 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Development configuration ](guide/webpack#development-configuration )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								     [开发环境配置 ](guide/webpack#development-configuration )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-11 15:36:40 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Production configuration ](guide/webpack#production-configuration )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								     [生产环境配置 ](guide/webpack#production-configuration )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-11 15:36:40 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  *  [Test configuration ](guide/webpack#test-configuration )
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								     [测试环境配置 ](guide/webpack#test-configuration )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [Trying it out ](guide/webpack#try ) 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   [试一下 ](guide/webpack#try )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [Highlights ](guide/webpack#highlights ) 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   [重点 ](guide/webpack#highlights )
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [Conclusion ](guide/webpack#conclusion ) 
						 
					
						
							
								
									
										
										
										
											2017-03-06 10:43:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   [总结 ](guide/webpack#conclusion )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-04 18:46:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								-->
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-03 13:55:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								You can also < a  href = "generated/zips/webpack/webpack.zip"  target = "_blank" > download the final result.< / a > 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-30 00:03:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								你还可以< a  href = "generated/zips/webpack/webpack.zip"  target = "_blank" > 点这里下载最终结果< / a > 。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								{@a  what-is-webpack}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								## What is Webpack?
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								## 什么是Webpack?   
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Webpack is a powerful module bundler.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								A _bundle_  is a JavaScript file that incorporates _assets_  that *belong*  together and
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Webpack是一个强力的模块打包器。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								所谓_包(bundle)_就是一个JavaScript文件, , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								包中可以包含JavaScript、CSS样式、HTML以及很多其它类型的文件。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								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.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								Webpack会遍历你应用中的所有源码, `import` 语句,构建出依赖图谱,并产出一个(或多个)_包_。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								通过插件和规则, , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You determine what Webpack does and how it does it with a JavaScript configuration file, `webpack.config.js` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								我们通过一个JavaScript配置文件`webpack.config.js` 来决定Webpack做什么以及如何做。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  entries-outputs}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								### Entries and outputs
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### 入口与输出
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								You supply Webpack with one or more *entry*  files and let it find and incorporate the dependencies that radiate from those entries.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The one entry point file in this example is the application's root file, `src/main.ts` :
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								我们给Webpack提供一个或多个*入口*文件,来让它查找与合并那些从这些入口点发散出去的依赖。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								在下面这个例子中,我们的入口点是该应用的根文件`src/app.ts` : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/webpack.common.js"  region = "one-entry"  title = "webpack.config.js (single entry)"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack inspects that file and traverses its `import`  dependencies recursively.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								Webpack探查那个文件, `import` 依赖。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/src/app/app.component.ts"  region = "component"  title = "src/main.ts"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								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.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								这里, `@angular/core` ,于是就这个文件加入到它的依赖列表里,为(有可能)把该文件打进包中做准备。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								它打开`@angular/core` 并追踪由_该文件的_`import` 语句构成的网络,直到构建出从`main.ts` 往下的整个依赖图谱。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Then it **outputs**  these files to the `app.js`  _bundle file_  designated in configuration:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								然后它把这些文件**输出**到当前配置所指定的_包文件_`app.js` 中:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  name = "webpack.config.js (single output)"  language = "javascript" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  output: {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    filename: 'app.js'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2017-03-30 20:04:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								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 `<script>`  tag in the `index.html` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								这个`app.js` 输出包是个单一的JavaScript文件, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  后面我们将在`index.html` 中用`<script>` 标签来加载它。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								{@a  multiple-bundles}
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								#### Multiple bundles
  
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								#### 多重包
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You probably don't want one giant bundle of everything.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								It's preferable to separate the volatile application app code from comparatively stable vendor code modules.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								我们可能不会希望把所有东西打进一个巨型包,而更喜欢把多变的应用代码从相对稳定的第三方提供商模块中分离出来。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Change the configuration so that it has two entry points, `main.ts`  and `vendor.ts` :
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								所以要修改配置,以获得两个入口点:`main.ts` 和`vendor.ts` : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "javascript" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  entry: {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    app: 'src/app.ts',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    vendor: 'src/vendor.ts'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  },
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  output: {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    filename: '[name].js'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack constructs two separate dependency graphs
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								and emits *two*  bundle files, one called `app.js`  containing only the application code and
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								another called `vendor.js`  with all the vendor dependencies.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								Webpack会构造出两个独立的依赖图谱, `app.js` ,它只包含我们的应用代码;另一个叫做`vendor.js` ,它包含所有的提供商依赖。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< div  class = "l-sub-section" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								The `[name]`  in the output name is a *placeholder*  that a Webpack plugin replaces with the entry names,
							 
						 
					
						
							
								
									
										
										
										
											2017-03-11 15:36:40 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								`app`  and `vendor` . Plugins are [covered later ](guide/webpack#commons-chunk-plugin ) in the guide. 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								在输出文件名中出现的`[name]` 是一个Webpack的*占位符*, , `app` 和`vendor` 。插件在本章的[稍后部分 ](guide/webpack#commons-chunk-plugin )讲解。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								To tell Webpack what belongs in the vendor bundle,
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								add a `vendor.ts`  file that only imports the application's third-party modules:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								要想告诉Webpack哪些文件属于vendor包, `vendor.ts` 文件,它只导入该应用的第三方模块:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/src/vendor.ts"  title = "src/vendor.ts"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  loaders}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								### Loaders
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### 加载器(Loader)
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Webpack can bundle any kind of file: JavaScript, TypeScript, CSS, SASS, LESS, images, HTML, fonts, whatever.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack _itself_  only understands JavaScript files.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Teach it to transform non-JavaScript file into their JavaScript equivalents with *loaders* .
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Configure loaders for TypeScript and CSS as follows.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								Webpack可以打包任何类型的文件: 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								但Webpack*本身*只认识JavaScript文件。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								我们要通过*加载器*来告诉它如何把这些文件处理成JavaScript文件。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								在这里, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "javascript" >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  rules: [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								      test: /\.ts$/,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								      loader: 'awesome-typescript-loader'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								      test: /\.css$/,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								      loaders: 'style-loader!css-loader'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ]
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-03 13:55:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								When Webpack encounters `import`  statements like the following,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								it applies the `test`  RegEx patterns.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								当Webpack遇到如下所示的`import` 语句时,它就会调用正则表达式的`test` 方法。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "typescript" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  import { AppComponent } from './app.component.ts';
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  import 'uiframework/dist/uiframework.css';
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								When a pattern matches the filename, Webpack processes the file with the associated loader.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								如果一个模式匹配上文件名, 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								The first `import`  file matches the `.ts`  pattern so Webpack processes it with the `awesome-typescript-loader` .
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The imported file doesn't match the second pattern so its loader is ignored.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								第一个`import` 文件匹配上了`.ts` 模式, `awesome-typescript-loader` 加载器处理它。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								导入的文件没有匹配上第二个模式,于是它的加载器就被忽略了。
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The second `import`  matches the second `.css`  pattern for which you have *two*  loaders chained by the (!) character.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack applies chained loaders *right to left* . So it applies
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								the `css`  loader first to flatten CSS `@import`  and `url(...)`  statements.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Then it applies the `style`  loader to append the css inside `<style>`  elements on the page.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								第二个`import` 匹配上了第二个`.css` 模式,它有两个用叹号字符(`!` )串联起来的加载器。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								Webpack会*从右到左*逐个应用串联的加载器,于是它先应用了`css` 加载器(用来平面化CSS的`@import` 和`url(...)` 语句), 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								然后应用了`style` 加载器(用来把css追加到页面上的*< style> *元素中)。
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  plugins}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								### Plugins
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### 插件
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack has a build pipeline with well-defined phases.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Tap into that pipeline with plugins such as the `uglify`  minification plugin:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								Webpack有一条构建流水线, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								我们可以把插件(比如`uglify` 代码最小化插件)挂到流水线上:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "javascript" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  plugins: [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    new webpack.optimize.UglifyJsPlugin()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ]
							 
						 
					
						
							
								
									
										
										
										
											2017-03-30 20:04:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  configure-webpack}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								## Configuring Webpack
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								## 配置Webpack
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								After that brief orientation, you are ready to build your own Webpack configuration for Angular apps.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								经过简短的培训之后, 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Begin by setting up the development environment.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								从设置开发环境开始。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Create a new project folder.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-30 20:04:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								创建一个新的项目文件夹。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "sh"  class = "code-shell" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  mkdir angular-webpack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  cd    angular-webpack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Add these files:
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								添加下列文件:
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-tabs >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "package.json"  path = "webpack/package.webpack.json" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/tsconfig.json"  path = "webpack/src/tsconfig.1.json" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "webpack.config.js"  path = "webpack/webpack.config.js" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "karma.conf.js"  path = "webpack/karma.webpack.conf.js" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "config/helpers.js"  path = "webpack/config/helpers.js" > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-tabs >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< div  class = "l-sub-section" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Many of these files should be familiar from other Angular documentation guides,
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								especially the [Typescript configuration ](guide/typescript-configuration ) and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								[npm packages ](guide/npm-packages ) guides.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								这些文件很多都很眼熟,它们在其他文档里已经出现过,特别是[_TypeScript配置_ ](guide/typescript-configuration )和[_npm包_ ](guide/npm-packages )这两章里。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Webpack, the plugins, and the loaders are also installed as packages.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								They are listed in the updated `packages.json` .
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								Webpack, , , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Open a terminal window and install the npm packages.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-30 20:04:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								打开命令行窗口并安装这些*npm*包
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "sh"  class = "code-shell" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  npm install
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  polyfills}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								### Polyfills
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-02-28 13:17:39 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### Polyfills 腻子脚本
  
						 
					
						
							
								
									
										
										
										
											2017-08-04 14:06:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You'll need polyfills to run an Angular application in most browsers as explained
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								in the [Browser Support ](guide/browser-support ) guide.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								我们在[_浏览器支持_ ](guide/browser-support )章节里解释过, , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Polyfills should be bundled separately from the application and vendor bundles.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Add a `polyfills.ts`  like this one to the `src/`  folder.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								Polyfills最好跟应用代码和vendor代码区分开来单独打包, `src/` 文件夹里添加一个`polyfills.ts` 文件,代码如下:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/src/polyfills.ts"  title = "src/polyfills.ts"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< div  class = "callout is-critical" >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< header >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Loading polyfills
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / header >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Load `zone.js`  early within `polyfills.ts` , immediately after the other ES6 and metadata shims.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								`polyfills.ts` 文件里,`zone.js` 库须尽早引入,  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Because this bundle file will load first, `polyfills.ts`  is also a good place to configure the browser environment
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								for production or development.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								由于这个包最先加载,所以`polyfills.ts` 非常适合用来配置浏览器环境,如生产环境配置或是开发环境。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  common-configuration}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### Common configuration
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### 通用配置
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Developers typically have separate configurations for development, production, and test environments.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								All three have a lot of configuration in common.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								开发、生产、测试等不同的环境通常会分开配置,但实际上这些配置也有很多地方是通用的。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Gather the common configuration in a file called `webpack.common.js` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								我们可以把这些通用的配置收归到一个文件,命名为`webpack.common.js` 。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/webpack.common.js"  title = "config/webpack.common.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  inside-webpack-commonjs}
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								### Inside _webpack.common.js_
  
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### webpack.common.js解读
  
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Webpack is a NodeJS-based tool that reads configuration from a JavaScript commonjs module file.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								Webpack是基于NodeJS的一个工具, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								The configuration imports dependencies with `require`  statements
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								and exports several objects as properties of a `module.exports`  object.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								这个配置文件是通过`require` 语句导入依赖,然后将多个对象作为`module.exports` 对象的属性导出。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [`entry` ](guide/webpack#common-entries )— the entry-point files that define the bundles. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   [`entries` ](guide/webpack#common-entries ) - 包体的入口文件。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-30 22:10:32 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [`resolve` ](guide/webpack#common-resolves )— how to resolve file names when they lack extensions. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   [`resolve` ](guide/webpack#common-resolves ) - 省略扩展名时如何解释文件名。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [`module.rules` ](guide/webpack#common-rules )—  `module`  is an object with `rules`  for deciding how files are loaded. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   [`module.rules` ](guide/webpack#common-rules ) - `module` 是一个对象,里面的`rules` 属性用来决定文件如何加载。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  [`plugins` ](guide/webpack#common-plugins )— creates instances of the plugins. 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   [`plugins` ](guide/webpack#common-plugins ) - 创建插件的实例。
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  common-entries}
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								#### _entry_
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 14:06:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								#### _entry_ 入口
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The first export is the `entry`  object:
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								如上所述,第一个导出的对象是*entries*: 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/webpack.common.js"  region = "entries"  title = "config/webpack.common.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								This `entry`  object defines the three bundles:
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								`entry` 对象定义了三个包: 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  `polyfills` — the polyfills needed to run Angular applications in most modern browsers. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   `polyfills`  - 使得Angular应用能够运行在大多数的现代浏览器。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  `vendor` — the third-party dependencies such as Angular, lodash, and bootstrap.css. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								   `vendor`  - 第三方依赖, 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  `app` — the application code. 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   `app`  - 应用代码。
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-30 22:10:32 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								{@a  common-resolves}
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								#### _resolve_ extension-less imports
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								#### _resolve_ 无扩展名的文件导入
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The app will `import`  dozens if not hundreds of JavaScript and TypeScript files.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You could write `import`  statements with explicit extensions like this example:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								如果你的应用程序只须`import` 几十个JavaScript或TypeScript文件, , `import` 语句里完整写上扩展名,如:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "typescript" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  import { AppComponent } from './app.component.ts';
							 
						 
					
						
							
								
									
										
										
										
											2017-03-30 20:04:18 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-09 22:01:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								But most `import`  statements don't mention the extension at all.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Tell Webpack to resolve extension-less file requests by looking for matching files with
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								`.ts`  extension or `.js`  extension (for regular JavaScript files and pre-compiled TypeScript files). 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								但实际上大部分`import` 语句都不带扩展名, , , `.ts` 或者`.js` 扩展名来匹配。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/webpack.common.js"  region = "resolve"  title = "config/webpack.common.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< div  class = "l-sub-section" >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								If Webpack should resolve extension-less files for styles and HTML,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								add `.css`  and `.html`  to the list.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								如果我们希望Webapck也能解析不带扩展名的样式和HTML文件, `.css` 和`.html` 即可。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  common-rules}
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								#### _module.rules_
  
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 14:06:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								#### _module.rules_ 规则
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Rules tell Webpack which loaders to use for each file, or module:
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Rules用来告诉Webpack加载不同文件或模块时该用哪个加载器。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/webpack.common.js"  region = "loaders"  title = "config/webpack.common.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  `awesome-typescript-loader` — a loader to transpile the Typescript code to ES5, guided by the `tsconfig.json`  file. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								   `awesome-typescript-loader`  - 一个用于把TypeScript代码转译成ES5的加载器, `tsconfig.json` 文件提供指导
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  `angular2-template-loader` — loads angular components' template and styles. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   `angular2-template-loader`  - 用于加载Angular组件的模板和样式
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  `html-loader` — for component templates. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   `html-loader`  - 为组件模板准备的加载器
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  images/fonts— Images and fonts are bundled as well. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   `images/fonts`  - 图片和字体文件也能被打包。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-03 13:55:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  CSS— the first pattern matches application-wide styles; the second handles 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								component-scoped styles (the ones specified in a component's `styleUrls`  metadata property).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   CSS - 第一个模式匹配应用级样式,第二个模式匹配组件局部样式(就是在组件元数据的`styleUrls` 属性中指定的那些)。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< div  class = "l-sub-section" >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-06 10:43:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The first pattern is for the application-wide styles. It excludes `.css`  files within the `src/app`  directory
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								where the component-scoped styles sit. The `ExtractTextPlugin`  (described below) applies the `style`  and `css` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								loaders to these files.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								第一个模式是给全局样式使用的,它排除了`/src/app` 目录下的`.css` 文件,因为那里放着我们的组件局部样式。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								它只包含了那些位于`/src/app` 及其上级目录的`.css` 文件,那里是应用级样式。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								`ExtractTextPlugin` (后面会讲到)使用`style` 和`css` 加载器来处理这些文件。 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The second pattern filters for component-scoped styles and loads them as strings via the `raw-loader` ,
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								which is what Angular expects to do with styles specified in a `styleUrls`  metadata property.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								第二个模式过滤器是给组件局部样式的,并通过`raw` 加载器把它们加载成字符串 —— 那是Angular期望通过元数据的`styleUrls` 属性来指定样式的形式。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< div  class = "l-sub-section" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Multiple loaders can be chained using the array notation.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								多重加载器也能使用数组形式串联起来。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  common-plugins}
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								#### _plugins_
  
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 14:06:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								#### _插件_
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Finally, create instances of three plugins:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								最后,创建三个插件实例:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/webpack.common.js"  region = "plugins"  title = "config/webpack.common.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  commons-chunk-plugin}
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								#### *CommonsChunkPlugin*
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 14:06:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								#### *CommonsChunkPlugin* 插件
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The `app.js`  bundle should contain only application code. All vendor code belongs in the `vendor.js`  bundle.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								`app.js` 包应该只包含应用代码。所有第三方代码都应该放进`vendor.js` 包中。 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Of course the application code imports vendor code.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								On its own, Webpack is not smart enough to keep the vendor code out of the `app.js`  bundle.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								The `CommonsChunkPlugin`  does that job.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								当然,应用代码中还是要`imports` 第三方代码。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack还没有智能到自动把提供商代码排除在`app.js` 包之外的程度。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								`CommonsChunkPlugin` 插件能完成此工作。 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< div  class = "l-sub-section" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								The `CommonsChunkPlugin`  identifies the hierarchy among three _chunks_ : `app`  -> `vendor`  -> `polyfills` .
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Where Webpack finds that `app`  has shared dependencies with `vendor` , it removes them from `app` .
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								It would remove `polyfills`  from `vendor`  if they shared dependencies, which they don't.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								`CommonsChunkPlugin` 标记出了三个_块_之间的等级体系: `app`  -> `vendor`  -> `polyfills` 。 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								当Webpack发现`app` 与`vendor` 有共享依赖时,就把它们从`app` 中移除。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								在`vendor` 和`polyfills` 之间有共享依赖时也同样如此(虽然它们没啥可共享的)。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-10 16:51:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  html-webpack-plugin}
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								#### _HtmlWebpackPlugin_
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-08-04 14:06:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								#### _HtmlWebpackPlugin_ 插件
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Webpack generates a number of js and CSS files.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You _could_  insert them into the `index.html`  _manually_ . That would be tedious and error-prone.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack can inject those scripts and links for you with the `HtmlWebpackPlugin` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Webpack生成了一些js和css文件。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								虽然我们_可以手动_把它们插入到`index.html` 中,但那样既枯燥又容易出错。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack可以通过`HtmlWebpackPlugin` 自动为我们注入那些`script` 和`link` 标签。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  environment-configuration}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								### Environment-specific configuration
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### 环境相关的配置
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The `webpack.common.js`  configuration file does most of the heavy lifting.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Create separate, environment-specific configuration files that build on `webpack.common` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								by merging into it the peculiarities particular to the target environments.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								`webpack.common.js` 配置做了大部分繁重的工作。 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								通过合并它们特有的配置,我们可以基于`webpack.common` 为目标环境创建独立的、环境相关的配置文件。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								These files tend to be short and simple.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								这些文件越小越简单越好。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  development-configuration}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### Development configuration
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### 开发环境配置
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Here is the `webpack.dev.js`  development configuration file.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								下面是开发环境的而配置文件`webpack.dev.js` : 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/webpack.dev.js"  title = "config/webpack.dev.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								The development build relies on the Webpack development server, configured near the bottom of the file.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								开发环境下的构建依赖于Webpack的开发服务器, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Although you tell Webpack to put output bundles in the `dist`  folder,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								the dev server keeps all bundles in memory; it doesn't write them to disk.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								You won't find any files in the `dist`  folder, at least not any generated from *this development build* .
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								虽然我们告诉Webpack把输出包放到`dist` 目录,但实际上开发服务器把这些包都放在了内存里,而不会把它们写到硬盘中。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								所以在`dist` 目录下是找不到任何文件的(至少现在这个开发环境下构建时没有)。
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The `HtmlWebpackPlugin` , added in `webpack.common.js` , uses the `publicPath`  and the `filename`  settings to generate
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								appropriate `<script>`  and `<link>`  tags into the `index.html` .
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								`HtmlWebpackPlugin` (由`webpack.common.js` 引入)插件使用了*`publicPath` *和*`filename` *设置, 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								来向`index.html` 中插入适当的< script> 和< link> 标签。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								The CSS styles are buried inside the Javascript bundles by default. The `ExtractTextPlugin`  extracts them into
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								external `.css`  files that the `HtmlWebpackPlugin`  inscribes as `<link>`  tags into the `index.html` .
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								默认情况下, `ExtractTextPlugin` 会把它们提取成外部`.css` 文件,
							 
						 
					
						
							
								
									
										
										
										
											2017-07-30 00:03:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								这样`HtmlWebpackPlugin` 插件就会转而把一个< link> 标签写进`index.html` 了。Refer to the [Webpack documentation ](https://webpack.github.io/docs/ ) for details on these and 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								other configuration options in this file.要了解本文件中这些以及其它配置项的详情,请参阅[Webpack文档 ](https://webpack.github.io/docs/ )。
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Refer to the [Webpack documentation ](https://webpack.github.io/docs/ ) for details on these and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								other configuration options in this file.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								要想了解本文件中的这些配置项和其它配置项的详情,请参阅 [Webpack 官方文档 ](https://webpack.github.io/docs/ )。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Grab the app code at the end of this guide and try:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								抓取本指南底部的应用代码,并试一试:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "sh"  class = "code-shell" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  npm start
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  production-configuration}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### Production configuration
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### 产品环境配置
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Configuration of a *production*  build resembles *development*  configuration with a few key changes.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*产品环境*下的配置和*开发环境*下的配置很相似……除了一些关键的改动。
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/webpack.prod.js"  title = "config/webpack.prod.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You'll deploy the application and its dependencies to a real production server.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You won't deploy the artifacts needed only in development.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								我们希望把应用程序及其依赖都部署到一个真实的产品服务器中。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								而不希望部署那些只在开发环境下才用得到的依赖。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Put the production output bundle files in the `dist`  folder.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								把产品环境的输出包放在`dist` 目录下。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack generates file names with cache-busting hash.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Thanks to the `HtmlWebpackPlugin` , you don't have to update the `index.html`  file when the hash changes.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Webpack生成的文件名中带有“缓存无效哈希(cache-busting hash)”。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								感谢`HtmlWebpackPlugin` 插件,当这些哈希值变化时,我们不用去更新`index.html` 了。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								There are additional plugins:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								还有一些别的插件:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  *`NoEmitOnErrorsPlugin` — stops the build if there is an error. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-07 11:25:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   *`NoEmitOnErrorsPlugin`*  - 如果出错就停止构建。*
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  *`UglifyJsPlugin` — minifies the bundles. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-07 11:25:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   *`UglifyJsPlugin`*  - 最小化(minify)生成的包。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  *`ExtractTextPlugin` — extracts embedded css as external files, adding cache-busting hash to the filename. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-07 11:25:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								   *`ExtractTextPlugin`*  - 把内嵌的css抽取成外部文件, 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  *`DefinePlugin` — use to define environment variables that you can reference within the application. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-07 11:25:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   *`DefinePlugin`*  - 用来定义环境变量,以便我们在自己的程序中引用它。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  *`LoaderOptionsPlugins` — to override options of certain loaders. 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-07 11:25:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   *`LoaderOptionsPlugins`*  - 为特定的加载器提供选项。
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Thanks to the `DefinePlugin`  and the `ENV`  variable defined at top, you can enable Angular production mode like this:
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								感谢*DefinePlugin*和顶部定义的`ENV` 变量, : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/src/main.ts"  region = "enable-prod"  title = "src/main.ts"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Grab the app code at the end of this guide and try:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								抓取本指南底部的应用代码,并试一试:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "sh"  class = "code-shell" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  npm run build
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  test-configuration}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### Test configuration
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								### 测试环境配置
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								You don't need much configuration to run unit tests.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You don't need the loaders and plugins that you declared for your development and production builds.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You probably don't need to load and process the application-wide styles files for unit tests and doing so would slow you down;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								you'll use the `null`  loader for those CSS files.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								我们并不需要使用很多配置项来运行单元测试。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								也不需要在开发环境和产品环境下引入的那些加载器和插件。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								如果有可能拖慢执行速度,甚至都不需要在单元测试中加载和处理应用全局样式文件,所以我们用一个`null` 加载器来处理所有CSS。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You could merge the test configuration into the `webpack.common`  configuration and override the parts you don't want or need.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								But it might be simpler to start over with a completely fresh configuration.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								我们可以把测试环境的配置合并到`webpack.common` 配置中,并且改写不想要或不需要的部分。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								但是从一个全新的配置开始可能更简单。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/webpack.test.js"  title = "config/webpack.test.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Reconfigure [Karma ](https://karma-runner.github.io/1.0/index.html ) to use Webpack to run the tests:
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								重新配置[Karma ](https://karma-runner.github.io/1.0/index.html ), : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/karma.conf.js"  title = "config/karma.conf.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You don't precompile the TypeScript; Webpack transpiles the Typescript files on the fly, in memory, and feeds the emitted JS directly to Karma.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								There are no temporary files on disk.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								我们不用预编译TypeScript, , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								硬盘上没有任何临时文件。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The `karma-test-shim`  tells Karma what files to pre-load and
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								primes the Angular test framework with test versions of the providers that every app expects to be pre-loaded.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								`karma-test-shim` 告诉Karma哪些文件需要预加载, :  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  path = "webpack/config/karma-test-shim.js"  title = "config/karma-test-shim.js"  linenums = "false" >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Notice that you do _not_  load the application code explicitly.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								You tell Webpack to find and load the test files (the files ending in `.spec.ts` ).
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Each spec file imports all— and only— the application source code that it tests.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack loads just _those_  specific application files and ignores the other files that you aren't testing.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								注意, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								只是告诉Webpack查找并加载我们的测试文件(文件名以`.spec.ts` 结尾)。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								每个规约(spec)文件都导入了所有(也只有)它测试所需的应用源码。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								Webpack只加载_那些_特定的应用文件, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Grab the app code at the end of this guide and try:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								抓取本指南底部的应用代码,并试一试:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< code-example  language = "sh"  class = "code-shell" >  
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  npm test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-example >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								{@a  try}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								## Trying it out
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								## 试一试
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Here is the source code for a small application that bundles with the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack techniques covered in this guide.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								这里是一个小型应用的全部源码, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-tabs >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/index.html"  path = "webpack/src/index.html" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/main.ts"  path = "webpack/src/main.ts" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/assets/css/styles.css"  path = "webpack/src/assets/css/styles.css" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-tabs >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-tabs >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/app/app.component.ts"  path = "webpack/src/app/app.component.ts" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/app/app.component.html"  path = "webpack/src/app/app.component.html" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/app/app.component.css"  path = "webpack/src/app/app.component.css" > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/app/app.component.spec.ts"  path = "webpack/src/app/app.component.spec.ts" > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/app/app.module.ts"  path = "webpack/src/app/app.module.ts" > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< / code-tabs >  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-06 10:43:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The < code > app.component.html< / code >  displays this downloadable Angular logo
							 
						 
					
						
							
								
									
										
										
										
											2017-04-24 20:23:45 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< a  href = "assets/images/logos/angular/angular.png" >  
						 
					
						
							
								
									
										
										
										
											2017-03-31 02:50:15 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< img  src = "assets/images/logos/angular/angular.png"  height = "40px"  title = "download Angular logo" > < / a > . 
						 
					
						
							
								
									
										
										
										
											2017-05-03 13:55:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Create a folder called `images`  under the project's `assets`  folder, then right-click (Cmd+click on Mac)
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								on the image and download it to that folder.
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code > app.component.html< / code > 显示了这个可下载的Angular Logo 
						 
					
						
							
								
									
										
										
										
											2017-07-30 12:32:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< a  href = "assets/images/logos/angular/angular.png"  target = "_blank" >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								< img  src = "assets/images/logos/angular/angular.png"  height = "40px"  title = "download Angular logo" > < / a > 。 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								在项目的`assets` 目录下创建一个名叫`images` 的文件夹, ( `images` 文件夹中。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{@a  bundle-ts}
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Here again are the TypeScript entry-point files that define the `polyfills`  and `vendor`  bundles.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								这里又是TypeScript的入口点文件, `polyfills` 和`vendor` 这两个包。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< code-tabs >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/polyfills.ts"  path = "webpack/src/polyfills.ts" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-pane  title = "src/vendor.ts"  path = "webpack/src/vendor.ts" > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-pane > 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / code-tabs >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								{@a  highlights}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-26 21:28:06 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< h3  class = "no-toc" > Highlights< / h3 >  
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< h3  class = "no-toc" > 重点< / h3 >  
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  There are no `<script>`  or `<link>`  tags in the `index.html` . 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								The `HtmlWebpackPlugin`  inserts them dynamically at runtime.
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   在`index.html` 中没有< script> 或< link> 标签。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  `HtmlWebpackPlugin` 会在运行时动态插入它们。
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  The `AppComponent`  in `app.component.ts`  imports the application-wide css with a simple `import`  statement. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   `app.component.ts` 中的`AppComponent` 类简单的用一个`import` 语句导入了应用级css。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-03-27 16:08:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								*  The `AppComponent`  itself has its own html template and css file. WebPack loads them with calls to `require()` . 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								Webpack stashes those component-scoped files in the `app.js`  bundle too.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								You don't see those calls in the source code;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								they're added behind the scenes by the `angular2-template-loader`  plug-in.
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   `AppComponent` 组件本身有它自己的HTML模板和CSS文件。Webpack通过调用`require()` 方法加载它们。Webpack还把那些组件内部的文件打包进了`app.js` 中。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								我们在自己的源码中看不到这些调用,这些工作是由幕后的`angular2-template-loader` 插件完成的。
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								*  The `vendor.ts`  consists of vendor dependency `import`  statements that drive the `vendor.js`  bundle. 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The application imports these modules too; they'd be duplicated in the `app.js`  bundle
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								if the `CommonsChunkPlugin`  hadn't detected the overlap and removed them from `app.js` .
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-07 11:25:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								   `vendor.ts` 由`import` 提供商依赖的语句组成,它最终决定了`vender.js` 的内容。
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  本应用也导入这些模块,如果没有`CommonsChunkPlugin` 插件检测出这种重叠,并且把它们从`app.js` 中移除,它们就会同时出现在`app.js` 包中。
							 
						 
					
						
							
								
									
										
										
										
											2018-03-07 11:25:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-04-01 01:57:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								{@a  conclusion}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								## Conclusion
  
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								## 总结
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								You've learned just enough Webpack to configurate development, test and production builds
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								for a small Angular application.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								我们学到了刚好够用来在开发、测试、产品环境下构建一个小型Angular应用的Webpack配置知识。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-22 18:09:39 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								_You could always do more_. Search the web for expert advice and expand your Webpack knowledge.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-23 11:51:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								_但我们还能做得更多_。搜索互联网来获得专家的建议, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								[Back to top ](guide/webpack#top )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-03 21:06:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-07 11:25:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								[回到顶部 ](guide/webpack#top )