webpack 學習筆記

來源:互聯網
上載者:User

標籤:

一個簡單的例子,明白了是怎麼操作,檔案怎麼製作的。在我看來和gulp/grunt區別不算太大,但很多人都說比較好,先進多了

WebPack

官網地址:  http://webpack.github.io/

安裝: #npm install -g webpack

 或       #npm install webpack -g

如果是linux/mac 可以用 #sudo npm install -g webpack

 

建立工程 

(1)建立目錄並進入目錄

#mkdir webpack_test && cd webpack_test

(2)建立package.json

#nam init  

//一路斷行符號就行

(3)下載 工程的webpack

#npm install webpack --save-dev

# npm install jsx-loader  --save-dev  //react jsx 

# npm install style-loader   --save-dev  //style // style css less都有依賴

# npm install css-loader   --save-dev  // css

# npm install less-loader   --save-dev  //less 載入

less

(4)建立webpack.config.js 檔案

#touch webpack.config.js

(5)編輯webpack.config.js 檔案

#vi webpack.config.js

var webpack = require(‘webpack‘);

// var less = require(‘less‘);

module.exports = {

/**

* [context description]base目錄

* @type {String}

*/

context:__dirname+"/src/",

/**

* [entry description] 模組入口

* @type {String 或 Array 或 Object}

* 值可以省略尾碼 .js

*/

entry: ‘./index‘,

 

/**

* [output description]設定輸入目錄和檔案名稱

* 可以設定

* @type {Object}

*/

output:{

path:"./dist",

filename:"[name].min.js"

},

/**

* [module description]載入器配置

* @type {Object} 每一種檔案都需要使用什麼載入器來處理。 所有載入器需要使用npm來載入

*/

module:{

loaders:[

{

test:/\.less$/,loader: "style!css!less"

}

]

},

/**

* [resolve description] 配置別名

* @type {Object}

*/

resolve:{

alias:{

 

}

},

/**

* [plugins description]外掛程式

* @type {Object}

*/

plugins:{

 

}

}

 

 

建立js檔案

#mkdir src && cd src

# touch index.js

#vi index.js

console.log("Hello World!");

 

#webpack

 

#touch main.less

#vi main.less

@bg: #eee;

@fontSize: 40px;

body{

background:@bg;

font-size:@fontSize;

}

#vi index.js

添加require("./main.less");

執行 

#webpack --display-error-details -w //清楚查看錯誤

或 

#webpack 

 

對於配置express+webpack 

我從github上下載了一個例子

網址:https://github.com/kenanpengyou/express-webpack-full-live-reload-example.git

下載後

進入目錄

#npm install

完成後 

 

瀏覽:#node app_browsersync

組建檔案也就是webpack命令#webpack

發布#webpack --config webpack.production.config.js -p 

# node app

 

 

 源碼:連結: https://pan.baidu.com/s/1boFCxJL 密碼: itn8

 

 

webpack 學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.