記得某次考試,出國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…許多庫都用不一樣方式寫,而許多都是很奇怪的。。。