记得某次考试,出国N年老师出的卷子全是英语,坑的英语很差的咱们不要不要的。幸好上了专业英语课。最重要的是专业英语对于咱们很重要,好比webpack,一堆博客都是几小时入门,如何会用webpack,当你想用它的某种特性,好比异步加载moudle,用到require.ensure,或者一些很细节的api,一些遇到的问题,都是中文网站,博客解决不了的。曾经嘲笑那些学视频的自认为大神前端,你找个webpack中文详细教学视频看看。javascript
[笑哭].此次,咱们来copy一下webpack官网,学一下英语。css
开始的motivition,why we do it.web app 能作不少事,咱们能在浏览器里用js作更多的事…导致客户端有n多代码,咱们须要一种代码加载,模块加载的东西。html
Module systems offer the option to split your code base into modules.模块系统提供一个选项去把代码划分到模块里。前端
There are multiple(多重的;多个的;复杂的;多功能的;) standards for (标准)how to define dependencies and export values:下面列举了模块的标准,amd,cmd,es6的import等。java
This is the way you would handle a modularized(模块化的)code base if you didn’t use a module system.jquery
<script src="module1.js"></script> <script src="module2.js"></script> <script src="libraryA.js"></script> <script src="module3.js"></script>
他们共同的问题Common problemswebpack
require cmd的同步加载
他们的Pros(优势)Cons(缺点)就不写了。git
import "jquery"; export function doStuff() {} module "localModule" {}
Modules should be executed on the client, so they must be transferred from the server to the browser.es6
There are two extremes on how to transfer modules: 模块应该在客户端执行,因此他们必需要从服务器端转换到浏览器,这有怎样转换模块的两个极端。github
Both are used in the wild, but both are suboptimal: 这俩都是野办法,用的都是(次优的)不咋样。
A more flexible transferring would be better. A compromise between the extremes is better in most cases.一个更加复杂的转换多是更好的,在大多数状况下,极端之间的妥协是更好的。
→ While compiling all modules: Split the set of modules into multiple smaller batches (chunks). 编译全部模块的时候,把这个模块切分到一些更小的分支,也就是每一个chunk
We get multiple smaller requests. Chunks with modules that are not required initially(最初)are only requested on demand(需求), The initial request doesn’t contain your complete code base and is smaller.咱们获得一些更小的请求,modules的chunks没有加载最初的,仅加载了须要的chunk,初始请求不包含你完整的代码库,它更小。
The “split points” are up to the developer and optional.划分红chunk这点是开发者可选的。
→ A big code base is possible!
Note: The idea is from Google’s GWT.
Read more about Code Splitting.
Why should a module system only help the developer with JavaScript? There are many other static resources that need to be handled:为啥模块系统只能服务于js开发者?这里这些静态资源都须要被处理。
And also:
This should be as easy as:
require("./style.css"); require("./style.less"); require("./template.jade"); require("./image.png");
Read more about Using loaders and Loaders.
When compiling all the modules a static analysis tries to find dependencies.当编译全部模块的时候,静态分析试着去寻找依赖。
Traditionally this could only find simple stuff without expression, but i.e. require("./template/" + templateName + ".jade")
is a common construct.
传统上只能找到简单的依赖可是不被解析。可是ie里面require("./template/" + templateName + ".jade")是公认的结构
Many libraries are written in different styles. Some of them are very weird…许多库都用不一样方式写,而许多都是很奇怪的。。。