標籤:方便 檔案 添加 public ttext ini test 更新 type
- 參考:
http://www.jianshu.com/p/c28e5df8647d http://www.jianshu.com/p/397df5dfd09c
1.該案例的目錄結果如下
zpvue
app
main.js
module1.js
module2.js
public
bundle.js
index.html
package.json
webpack.config.js
2.安裝 webpack
npm install webpack --save-dev --registry=https://registry.npm.taobao.org
使用以下命令進行webpack 初始化
npm init
配置webpack.json檔案
{
"name": "zpvue",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start":"webpack -p" //對應5.webpack 的命令
},
"author": "zeopean",
"license": "ISC",
"devDependencies": {
"css-loader": "^0.26.0",
"extract-text-webpack-plugin": "^1.0.1",
"sass-loader": "^4.0.2",
"style-loader": "^0.13.1",
"webpack": "^1.13.3"
}
}
- scripts:start 明亮對應 =>5.webpack 的命令
- scripts.devDependencies 在3.安裝載入器時會主動添加
3.安裝loader 載入器
npm install sass-loader style-loader css-loader extract-text-webpack-plugin --save-dev --registry=https://registry.npm.taobao.org
4.配置webpack.config.js 檔案
/**
* Created by zeopean on 16/11/19.
*/
var webpack = require(‘webpack‘)
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
//頁面入口
entry:{
index: ‘./app/main.js‘
},
//入口檔案輸出配置
output:{
path:‘./public‘,
filename:‘bundle.js‘
},
//載入模組
module:{
loaders:[{
test:‘/\.sass$/‘,
loader:ExtractTextPlugin.extract(‘style-loader‘, ‘css-loader!sass-loader‘)
}]
},
plugins:[
new ExtractTextPlugin(‘[name].css‘)
]
};
- entry 表示入口
- output 表示輸出打包後的bundle.js
- module表示載入器
5.webpack 的命令
關於webpack的啟動方式:
webpack // 最基本的啟動webpack的方法
webpack -w // 提供watch方法;即時進行打封裝更新
webpack -p // 對打包後的檔案進行壓縮
webpack -d // 提供source map,方便調式代碼
6.引入輸出的bundle.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>webpack demo</title>
</head>
<body>
</body>
<script type="text/javascript" src="../public/bundle.js"></script>
</html>
webpack 的簡單使用