把工做中經常使用的webpack配置,包括loader、plugins、優化等記錄下來,方便學習和查閱。會一直更新本文。javascript
const config = {
entry:{
index:'./index'
},
output:{
filename:'[name].[hash].js', //[name]是entry裏的key,能夠加hash
path:__dirname
}
};
複製代碼
//a.js
const help = requrie('../libs/help/a.js');
//b.js
const help = requrie('../../../libs/help/b.js');
//咱們看到用到help裏某個文件時,要找不少層目錄,能夠在配置文件裏寫入別名。
const config = {
resolve:{
alias:{
help:path.resolve(__dirname,"libs/help")
}
}
};
//a.js
const help = requrie('help/a.js');
//b.js
const help = requrie('help/b.js');
複製代碼
const config = {
module:{
noParse:[/jquery/],
rules:[
{
test:'匹配的文件',
include:'在哪一個目錄查找',
exclude:'排除哪一個目錄',
loader:'use:[{loader}]的簡寫',
use:[
//配置多個loader,從有往左依次執行
{
loader:"須要的loader",
options:{
//loader的相關配置
}
}
]
}
]
}
}
複製代碼
{
module:{
rules:[
{
test: /\.jsx?$/,
exclude:/node_modules/,
include:path.resolve(__dirname,'src'),
use:{
loader:'babel-loader',
options:{
presets:[
'es2015','react'
]
}
}
}
]
}
}
複製代碼
npm install --save-dev less-loader less
複製代碼
npm install sass-loader node-sass webpack --save-dev
複製代碼
const config = {
module:{
rules:[
{
test: /\.css$/,
use: [
'style-loader',
{
loader:'css-loader',
options:{
modules:true //css模塊化
}
}
]
},
{
test: /\.less$/,
use: ['style-loader', 'css-loader', {
loader:'less-loader',
options:{
modifyVars:{
"color":"#ccc" //設置變量
}
}
}]
},
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
}
]
}
}
複製代碼
{
module:{
rules:[
{
test: /\.(png|jpg|gif)$/,
use: ['file-loader']
}
]
}
}
複製代碼
{
module:{
rules:[
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10*1024,
name: '[path][name].[ext]'
}
}
]
}
]
}
}
複製代碼
{
test: /\.(html)$/,
use: {
loader: 'html-loader'
}
}
複製代碼
const CleanWebpackPlugin = requrie('clean-webpack-plugin');
const config = {
plugins:[
new CleanWebpackPlugin(['輸出目錄'])
]
}
複製代碼
const HtmlWebpackPlugin = requrie('html-webpack-plugin');
const config = {
plugins:[
new HtmlWebpackPlugin({
template:'index.html', //設置模版
hash:true, //添加hash
filename:'hello.html', //輸出名字
})
]
}
複製代碼
npm i extract-text-webpack-plugin@next -S
複製代碼
//注:此處的[name]生成後爲入口文件的key
const cssExtract = new ExtractTextPlugin('public/[name].[contenthash:8].css');
const lessExtract = new ExtractTextPlugin('public/[name].[contenthash:8].css');
const scssExtract = new ExtractTextPlugin('public/[name].[contenthash:8].css');
const config = {
module:{
rules:[
{
test: /\.css$/,
use: cssExtract.extract(['css-loader'])
},
{
test: /\.less$/,
use: lessExtract.extract({
use: ['css-loader', 'less-loader']
})
},
{
test: /\.scss$/,
use: scssExtract.extract({
use: ['css-loader', 'sass-loader']
})
}
]
},
plugins:[
cssExtract,
lessExtract,
scssExtract
]
};
複製代碼
const UglifyjsWebpackPlugin = require('uglifyjs-webpack-plugin');
const config = {
module:{
plugins:[
new UglifyjsWebpackPlugin({
test: /\.js($|\?)/i
})
]
}
};
複製代碼
webpack --mode production
複製代碼
const CopyWebpackPlugin = require('copy-webpack-plugin');
{
plugins:[
new CopyWebpackPlugin([
{
from:'./src/test',
to:'./'
}
])
]
}
複製代碼
const config = {
watch:true, //watch爲true時,watchOptions才生效
watchOptions: {
ignored:/node_modules/, //忽略目錄
aggregateTimeout:300, //監聽變化 300ms後再執行
poll:1000 // 默認每秒詢問1000次
}
}
複製代碼
const webpack = require('webpack');
const config = {
devServer: {
hot: true, //寫了這項就不會自動刷新,不寫就會自動刷新,知道緣由的請留言,謝謝!
open: true,
inline:true
},
plugins:[
new webpack.HotModuleReplacementPlugin() //不寫會報錯,不知道爲何。。。
]
}
複製代碼
記錄用到過的webpack loaders、plugins和其餘相關配置。後續還會添加提取共代碼等其餘優化處理。css