前言——webpack如今已成爲前端自動化、模塊化不可或缺的一款工具,咱們能夠把它看作是一種模塊打包機,它來分析你的項目結構、找到JS模塊以及一些瀏覽器不能直接運行的拓展語言(如SASS、LESS等),而後將其打包爲合適的格式供瀏覽器使用......css
首先要確保你的計算機安裝了nodeJS而後咱們就能夠開始咱們的webpack之路了~~~先來按照以下步驟建立個項目吧:html
npm init -y
複製代碼
npm install webpack webpack-cli -D
複製代碼
const path = require('path');
module.exports = {
entry:'',
output:{
},
module:{
},
plugins:{
},
devServer:{
}
}
複製代碼
這裏先說下幾個核心的配置前端
entry:'../main.js',
output:{
path:path.join(__dirname,'../dist'),
filename:'bundle.js'
},
複製代碼
(1) test:這裏是一個正則用於匹配轉換的文件(這裏是以.css後綴結尾的文件)vue
(2) loader:若是你只須要一個loader來處理那麼就傳一個字符串,若是有多個則寫一個數組(這裏須要兩個css-loader解析css裏的路徑如背景圖路徑等等,style-loader用來把css文件內容變爲style標籤並插入)轉換的時候是按數組從右往左去轉換的。node
module:{
rules:[
{
test:/\.css$/,
loader:['style-loader','css-loader']
}
]
}
複製代碼
npm install html-webpack-plugin -D
複製代碼
使用的時候使用new關鍵字,同時能夠傳遞參數 (1)template:指定產出的html模板react
(2)filename:產出html文件名webpack
(3)hash:在引入的JS文件加入查詢字符串避免緩存web
(4)minify:壓縮npm
咱們也能夠按需引入其餘的變量參數json
plugins:[
new HtmlWebpackPlugin({
template:'./src/index.html',
filename:'index.html',
title:'入門webpack',
hash:true,
minify:{
removeAttributeQuotes:true
}
})
]
複製代碼
"dev":"webpack-dev-server --open --mode development"
複製代碼
這樣咱們就可使用npm run dev來啓動項目了,咱們還須要在webpack.config.js中的devServer中來配置:
(1) contentBase:配置靜態文件根目錄,也就是你打包後的目錄 (2) host:主機(這裏使用localhost) (3) port:端口(默認8080) (4) compress:服務器返回瀏覽器是否使用gzip壓縮(布爾值)
devServer:{
contentBase:'../dist',
host:'localhost',
port:'8080',
compress:true
}
複製代碼
在使用webpack-dev-server啓動服務後,會注入一個websocket客戶端,主要用來檢測到咱們修改內容後通知webpack來從新編譯並刷新頁面。
截止到這裏,已經能夠配置一個簡單的webpack用來打包啦,不過這只是一個入門級的,在實際項目中還遠遠不夠,到這裏若是你還有興趣,請繼續往下走~~
entry: {
index: './src/index.js',
main:'./src/main.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[hash:8].js',
}
複製代碼
當咱們有多入口的時候,出口文件應該像這樣命名,而且能夠定義一個hash值,而且可使用冒號加數字來規定hash的長度。 2. 圖片的支持問題。咱們可使用兩個loader來處理圖片的打包問題,第一個是file-loader來解決CSS等文件中的引入圖片路徑問題,第二個是url-loader當圖片較小的時候會把圖片BASE64編碼。
{
test:/\.(jpg|png|gif|svg)$/,
use:'file-loader',
include:path.join(__dirname,'./src'),
}
複製代碼
const cssExtract=new ExtractTextWebpackPlugin('css.css');
const lessExtract=new ExtractTextWebpackPlugin('less.css');
const sassExtract=new ExtractTextWebpackPlugin('sass.css');
{
test:/\.less$/,
use: lessExtract.extract({
use:['css-loader','less-loader']
}),
include:path.join(__dirname,'./src'),
exclude:/node_modules/
},
{
test:/\.scss$/,
use: sassExtract.extract({
use:['css-loader','sass-loader']
}),
include:path.join(__dirname,'./src'),
exclude:/node_modules/
},
複製代碼
{
test:/\.jsx?$/,
use: {
loader: 'babel-loader',
options: {
presets: ["env","stage-0","react"]
}
},
include:path.join(__dirname,'./src'),
exclude:/node_modules/
},
複製代碼
proxy: {
'/api': {
target: 'http://172.1.1.1:5000',
pathRewrite: {'^/api' : '/api'},
changeOrigin: true
}
}
複製代碼
target的值就是你要請求的IP地址,能夠按需自行配置。 在進階中的代碼中,有配置loader的地方加了一個exclude屬性,這個的做用主要是指定哪些目錄下的文件不須要進行loader轉換。暫且說這麼多吧,一時就想起了這麼多,有須要的能夠留言一塊兒探討,下面再介紹些經常使用的loader和plugin吧~~
若有使用vue的項目能夠配置以下module規則,不須要的可自行刪減
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
css: 'vue-style-loader!css-loader',
less: 'vue-style-loader!css-loader!less-loader'
},
postLoaders: {
html: 'babel-loader'
}
}
},
{
test: /iview\/.*?js$/,
loader: 'happypack/loader?id=happybabel',
exclude: /node_modules/
},
{
test: /\.js$/,
loader: 'happypack/loader?id=happybabel',
exclude: /node_modules/
},
{
test: /\.js[x]?$/,
include: [resolve('src')],
exclude: /node_modules/,
loader: 'happypack/loader?id=happybabel'
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: ['css-loader?minimize', 'autoprefixer-loader'],
fallback: 'style-loader'
})
},
{
test: /\.less$/,
use: ExtractTextPlugin.extract({
use: ['css-loader?minimize','autoprefixer-loader', 'less-loader'],
fallback: 'style-loader'
}),
},
{
test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
loader: 'url-loader?limit=1024'
},
{
test: /\.(html|tpl)$/,
loader: 'html-loader'
}
]
}
複製代碼
暫時就寫到這裏了,能夠作一個入門瞭解,webpack還有好多好多東西....有時間整理下再繼續更新~~