Upgrade to Pro — share decks privately, control downloads, hide ads and more …

webpack 簡介

webpack 簡介

about webpack module bundler

Avatar for Patrick Wang

Patrick Wang

October 24, 2014
Tweet

More Decks by Patrick Wang

Other Decks in Programming

Transcript

  1. 有點像是 Rails 的 Sprocket //= require jquery //= require utils

    //= require_tree . 打成⼀一包 application.js
  2. ⼜又像 CSS 的 @import @import “base”; @import “header”; @import “content”;

    @import “footer”; 透過 Sass 也可以打成⼀一包
  3. Goal • Split the dependency tree into chunks loaded on

    demand • Keep initial loading time low • Every static asset should be able to be a module • Ability to integrate 3rd-party libraries as modules • Ability to customize nearly every part of the module bundler • Suited for big projects http://webpack.github.io/docs/what-is-webpack.html
  4. ⽤用來解決... • 模組化 // patw.js … 以 CommonJS ⽰示範 require(‘patwWidget.css’);

    var patwWidget = {}; patwWidget.init = function () { console.log(“init”); }; module.exports = patwWidget; // 使⽤用定義好的元件 var patw = require(‘./patw’); patw.init(); // 印出 init // 並套⽤用了 pageWidget.css 的樣式
  5. ⽤用來解決... • 解決載⼊入相依性問題
 
 
 
 
 • 打包 Bundle

    • Cache buster: [name]-[hash].js • ⾮非同步載⼊入 <script src=“jquery.dataTable.js”></script> <!— jQuery 呢?! —> <script src=“jquery.min.js”></script> require([‘jquery’, ‘datatables’], function ($) {
 $(“.data-table”).dataTable();
 }); 確保都載⼊入再執⾏行內容!
  6. • Webpack • CommonJS (require.ensure([], cb)), AMD (require([],cb)) 都⽀支援 •

    可以嵌⼊入 CSS, SCSS, CoffeeScript, 甚⾄至是圖⽚片等各種靜態⽂文件 (眾多 的 Loader) • ⽀支援引⼊入 npm, bower, component 的模組 • 程式碼分割(Code Splitting) • 切成⼩小塊(Chunks)、多個 .js ,可⽤用於多處 • ⾮非同步載⼊入... 只在必要的時候才載⼊入 • 多⼊入⼝口 • ⾃自有 dev-server