前言:都2020年了,感受是時候該學一波webpack了,趁着最近有時間,就學了一下,等把官網上的webpack結構和模塊大概看了一遍以後,就感受能夠開始搭個項目實戰一下了,從0開始,一步步記錄下來
使用版本: webpack4.xcss
* html:
html-webpack-plugin clean-webpack-plugin
* css:
style-loader css-loader sass-loader node-sass postcss-loader autoprefixer
* js:
babel-loader @babel/preset-env @babel/core @babel/polyfill core-js@2 @babel/plugin-transform-runtime
@babel/runtime @babel/runtime-corejs2
* vue:
vue-loader vue-template-compiler vue-style-loader vue vue-router axios vuex
* webpack:
file-loader url-loader webpack-dev-server webpack-merge copy-webpack-plugin happypack HardSourceWebpackPlugin
webpack-bundle-analyzer optimize-css-assets-webpack-plugin portfinder FriendlyErrorsPlugin
複製代碼
-- 生成hmtl模板
-- 刪除上一次的dist文件
-- 自動添加瀏覽器前綴
-- 使用sass預編譯器
-- 轉換ES6,7,8,9語法爲ES5
-- 大於10k文件打包到dist,小於10k轉換爲base64
-- 兼容vue-- 拷貝靜態文件
-- 熱更新
-- 區分當前環境
-- 多線程打包
-- 緩存未改變模塊
-- g-zip壓縮
-- 獲取本機ip
-- 打包大小分析
-- 壓縮css
-- 檢查端口是否存在複製代碼
1. 新建一個文件 取名爲webpack-vue
2. cd webpack-vue npm init -y npm i -D webpack webpack-cli
3. 新建 src/main.js ,裏面隨便寫點 console.log('hello,webpack')
4. 修改 package.json - >scripts ,添加 "build":"webpack src/main.js"
5. 而後 npm run build 若是多了一個dist文件,那麼初次打包就成功了複製代碼
const path=require('path')
module.exports = {
mode:'development',
entry:path.resolve(__dirname,'../src/main.js'), //入口文件
output:{
filename:'[name].[hash:8].js', //打包後的名字 生成8位數的hash
path.resolve(__dirname,'../dist') //打包的路徑
}
}
而後修改 package.json ->scripts,修改成: "build":"webpack --config build/webpack.config.js"
而後npm run build複製代碼
3. 咱們生成了main.js以後,不可能每次都手動在index.html裏面引入,因此咱們須要這個插件來幫咱們自動引入html
先安裝插件: vue
npm i -D html-webpack-plugin複製代碼
根目錄新建一個 public/index.htmlnode
修改webpack.config.js:jquery
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin') //這裏引入插件
module.exports = {
mode:'development', // 開發模式
entry: path.resolve(__dirname,'../src/main.js'), // 入口文件
output: {
filename: '[name].[hash].js', // 打包後的文件名稱
path: path.resolve(__dirname,'../dist') // 打包後的目錄
},
//插件注入
plugins:[
new HtmlWebpackPlugin({
template:path.resolve(__dirname,'../public/index.html')
})
]
}複製代碼
而後npm run build 就會發現dist裏面多了index.html,而且已經自動幫咱們引入了main.jswebpack
4. 因爲hash每次生成的不一樣,致使每次打包都會將新的main.js打包到dist文件夾,因此咱們須要一個插件來打包前刪除dist文件ios
先安裝插件:
nginx
npm i -D clean-webpack-plugin複製代碼
webpack.config.js
const {CleanWebpackPlugin} = require('clean-webpack-plugin') //引入
plugins:[
new HtmlWebpackPlugin({
template:path.resolve(__dirname,'../public/index.html')
}),
new CleanWebpackPlugin()
]
複製代碼
5.咱們通常把不須要打包的靜態文件放在public裏面,這個地方不會被打包到dist,因此咱們須要使用插件來把這些文件複製過去git
先安裝插件:github
npm i -D copy-webpack-plugin複製代碼
webpack.config.js
const CopyWebpackPlugin = require('copy-webpack-plugin') // 複製文件
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, '../public'),
to: path.resolve(__dirname, '../dist')
}
]
})
]複製代碼
6. 爲了讓webpack識別css,咱們須要安裝loader,並將解析後的css插入到index.html裏面的style
先安裝:
npm i -D style-loader css-loader複製代碼
webpack.config.js
module.exports = {
module:{
rules:[{
test:/\.css$/,
use:['style-loader','css-loader'] // 從右向左解析原則
}]
}
}複製代碼
7.咱們這裏可使用預編譯器更好的處理css,我這裏使用的是sass
先安裝:
npm install -D sass-loader node-sass
webpack.config.js
module:{
rules: [{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader'] // 從右向左解析原則
}]
}複製代碼
8. 自動添加瀏覽器前綴
先安裝:
npm i -D postcss-loader autoprefixer
webpakc.config.js
module: {
rules: [
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader'] // 從右向左解析原則 sass-loader必須寫後面
}]
}
在根目錄新建 .browserslistrc
這個裏面配置兼容的瀏覽器
這裏貼下個人默認配置,能夠根據實際狀況本身去配置
> 1%
last 2 versions
not ie <= 8
再新建一個postcss.config.js
module.exports = {
plugins: [require('autoprefixer')] // 引用該插件便可了
}
這樣打包後就自動幫你添加瀏覽器前綴了複製代碼
9.以前的style-loader只是把css打包到index.html裏面的style-loader裏面,若是css特別多這種辦法確定不行,因此咱們須要單獨抽離提取css
先安裝:
npm i -D mini-css-extract-plugin
webpack.config.js
const MiniCssExtractPlugin = require("mini-css-extract-plugin") //提取css
module: {
rules: [{
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader'] // 從右向左解析原則
}]
}
plugins: [
new MiniCssExtractPlugin({
filename: "css/[name].[hash:8].css",
chunkFilename: "[id].css",
})
]
這樣打包後就將css打包到css文件下里面了複製代碼
10. 咱們爲了減小圖片字體等打包的大小,咱們可使用url-loader將少於指定大小的文件轉換爲base64,使用file-loader將大於指定大小的文件 移動到指定的位置
先安裝:
npm i -D file-loader url-loader
webpack.config.js
module:{
rules:[
{
test:/\.(jpe?g|png|gif|svg)(\?.*)?$/ //圖片
use:[{
loader:'url-loader',
options:{
limit:10240,
fallback:{
loader:'file-loader',
options:{
name:'img/[name].[hash:8].[ext]',
publicPath: '../' //爲了防止圖片路徑錯誤
}
}
}
}]
},{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
use: [{
loader: 'url-loader',
options: {
limit: 10240,
fallback: {
loader: 'file-loader',
options: {
name: 'media/[name].[hash:8].[ext]',
publicPath: '../'
}
}
}
}]
},{
test:/\.(woff2?|eot|ttf|otf)(\?.*)$/,
loader:'url-loader',
options:{
limit:10240,
fallback:{
loader:'file-loader, options:{ name:'fonts/[name].[hash:8].[ext]', publicPath: '../' } } } } ] } webpack只會轉換和移動項目中使用了的圖片 打包後發現圖片字體等也打包進去了複製代碼
11.爲了兼容瀏覽器,咱們須要將ES6,7,8,9轉換爲Es5
先安裝:
npm install -D babel-loader @babel/preset-env @babel/core複製代碼
webpack.config.js
module: {
rules: [{
test: /\.js$/,
use: ['babel-loader']
}]
}
根目錄新建 .babelrc
{
"presets": [
"@babel/preset-env"
]
}
一些新的api如Promise,set,Maps等還不支持轉換,因此咱們須要另外一個插件babel/polyfill,可是這個插件會將全部的poly都打包到
mian.js裏面,因此咱們須要另外一個插件 core-js@2 來按需加載
`npm i -s @babel/polyfill core-js@2`
修改.babelrc
{
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"useBuiltIns": "usage",
"corejs": 2,
"targets": {
"browsers": [
"last 2 versions",
"ie >= 10"
]
}
}
]
]
}
還有一個問題,會影響全局依賴,因此咱們須要另外一個插件來解決這個問題
npm i @babel/plugin-transform-runtime -D
npm i --save @babel/runtime
npm i --save @babel/runtime-corejs2
修改.babelrc
{
"presets": [
"@babel/preset-env"
],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"helpers": true,
"regenerator": true,
"useESModules": false,
"corejs": 2
}
]
]
}
這樣就完美解決ES6新api的兼容問題了複製代碼
12.兼容vue
先安裝:
npm i -D vue-loader vue-template-compiler vue-style-loader
npm i -S vue 複製代碼
webpack.config.js
const vueLoaderPlugin=require('vue-loader/lib/plugin')
function resolve(dir) {
return path.join(__dirname, '..', dir)
}
module:{
rules:[{
test:/\.vue$/,
use:[vue-loader]
}]
},
resolve:{
alias:{
'vue$':'vue/dist/vue.runtime.esm.js',
'@':path.resolve(__dirname,'../src')
},
extensions: ['.js', '.vue', '.json'],
},
plugins:[
new vueLoaderPlugin()
]
這樣配置完成以後就能夠webpack就識別了vue文件複製代碼
13.熱更新
先安裝:
npm i -D webpack-dev-server複製代碼
wepack.config.js
const webpack = require('webpack')
devServer: {
compress: true,
port: 8989,
hot: true,
inline: true,
hotOnly: true, //當編譯失敗時,不刷新頁面
overlay: true, //用來在編譯出錯的時候,在瀏覽器頁面上顯示錯誤
publicPath: '/', //必定要加
open: true,
watchOptions: {
// 不監聽的文件或文件夾,支持正則匹配
ignored: /node_modules/,
// 監聽到變化後等1s再去執行動做
aggregateTimeout: 1000,
// 默認每秒詢問1000次
poll: 1000
}
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.NoEmitOnErrorsPlugin()
]
在package.json裏面配置: "dev":"webpack-dev-server --config build/webpack.config.js"
在main.js裏面
import Vue from "vue"
import App from "./App"
new Vue({
render:h=>h(App)
}).$mount('#app')
src文件夾內新建一個APP.vue,內容自定義
而後npm run dev 頁面就運行起來了複製代碼
14.區分開發環境和生產環境
build文件夾內新建 webpack.dev.js webpack.prod.js複製代碼
開發環境:
1.不須要壓縮代碼
2.熱更新
3.完整的soureMap
...
生產環境:
1.壓縮代碼
2.提取css文件
3.去除soureMap (根據我的須要)
...複製代碼
咱們這裏須要安裝 webpack-merge 來合併配置項
先安裝:
npm i -D webpack-merge 複製代碼
webpack.dev.js
const webpackConfig = require('./webpack.config')
const merge = require('webpack-merge')
const webpack = require('webpack')
module.exports = merge(webpackConfig, {
mode: 'development',
devtool: 'cheap-module-eval-source-map',
devServer: {
compress: true,
port: 8989,
hot: true,
inline: true,
hotOnly: true, //當編譯失敗時,不刷新頁面
overlay: true, //用來在編譯出錯的時候,在瀏覽器頁面上顯示錯誤
publicPath: '/', //必定要加
open: true,
watchOptions: {
// 不監聽的文件或文件夾,支持正則匹配
ignored: /node_modules/,
// 監聽到變化後等1s再去執行動做
aggregateTimeout: 1000,
// 默認每秒詢問1000次
poll: 1000
}
},
module: {
rules: [
{
test: /\.js$/,
use: ['babel-loader']
},
{
test: /\.css$/,
use: ['vue-style-loader', 'css-loader', 'postcss-loader'],
},
{
test: /\.scss$/,
use: ['vue-style-loader', 'css-loader', 'postcss-loader', 'sass-loader'],
exclude: /node_modules/
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.NoEmitOnErrorsPlugin()
]
})複製代碼
webpack.prod.js
const webpackConfig = require('./webpack.config')
const merge = require('webpack-merge')
const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin') //清除dist
const MiniCssExtractPlugin = require("mini-css-extract-plugin") //提取css
function resolve(dir) {
return path.join(__dirname, '..', dir)
}
module.exports = merge(webpackConfig, {
mode: "production",
devtool: 'none',
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: {
name: 'vendors',
test: /[\\\/]node_modules[\\\/]/,
priority: -10,
chunks: 'initial'
},
common: {
name: 'chunk-common',
minChunks: 2,
priority: -20,
chunks: 'initial',
reuseExistingChunk: true
}
}
}
},
module: {
rules: [
{
test: /\.js$/,
use: ['babel-loader']
exclude: /node_modules/,
include: [resolve('src'), resolve('node_modules/webpack-dev-server/client')]
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
}
}, 'css-loader', 'postcss-loader'],
},
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
}
}, 'css-loader', 'postcss-loader', 'sass-loader'],
exclude: /node_modules/
}
]
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: 'css/[name].[hash].css',
chunkFilename: 'css/[name].[hash].css',
})
]
})
複製代碼
webpack.config.js
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin') //這裏引入插件
const vueLoaderPlugin = require('vue-loader/lib/plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin') // 複製文件
function resolve(dir) {
return path.join(__dirname, '..', dir)
}
module.exports = {
mode: 'development',
entry: path.resolve(__dirname, '../src/main.js'),
output: {
filename: 'js/[name].[hash:8].js',
path: path.resolve(__dirname, '../dist'),
chunkFilename: 'js/[name].[hash:8].js', //異步加載模塊
publicPath: './'
},
externals: {},
module: {
noParse: /jquery/,
rules: [
{
test: /\.vue$/,
use: [{
loader: 'vue-loader',
options: {
compilerOptions: {
preserveWhitespace: false
}
}
}]
},
{
test: /\.(jpe?g|png|gif)$/i, //圖片文件
use: [
{
loader: 'url-loader',
options: {
limit: 10240,
fallback: {
loader: 'file-loader',
options: {
name: 'img/[name].[hash:8].[ext]',
publicPath: '../'
}
}
}
}
]
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, //媒體文件
use: [
{
loader: 'url-loader',
options: {
limit: 10240,
fallback: {
loader: 'file-loader',
options: {
name: 'media/[name].[hash:8].[ext]',
publicPath: '../'
}
}
}
}
]
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/i, // 字體
use: [
{
loader: 'url-loader',
options: {
limit: 10240,
fallback: {
loader: 'file-loader',
options: {
name: 'font/[name].[hash:8].[ext]',
publicPath: '../'
}
}
}
}
]
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
},
extensions: ['.js', '.vue', '.json'],
},
//插件注入
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, '../public/index.html')
}),
new vueLoaderPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, '../public'),
to: path.resolve(__dirname, '../dist')
}
]
})
]
}複製代碼
默認爲production,webpack4.x默認會壓縮代碼和去除無用的代碼
可選參數:production, development
ps:以前我認爲只須要設置mode爲production,就不用使用壓縮css和js的插件,但結果發現我錯了,仔細比較了下,仍是要安裝的
先安裝打包css的:
npm i -D optimize-css-assets-webpack-plugin複製代碼
webpack.prod.js
const optimizeCss = require('optimize-css-assets-webpack-plugin');
plugins:[
new optimizeCss({
cssProcessor: require('cssnano'), //引入cssnano配置壓縮選項
cssProcessorOptions: {
discardComments: { removeAll: true }
},
canPrint: true //是否將插件信息打印到控制檯
})
]
複製代碼
壓縮js和js打包多線程的暫時沒有添加,在網上搜有的說不用添加,有的說仍是要安裝插件,等實際項目中我用完以後再來添加
alias 能夠告訴webpack去指定文件夾去尋找,儘可能多使用
include exclude 包括和過濾
noParse 當咱們代碼中使用到import jq from 'jquery'時,webpack會去解析jq這個庫是否有依賴其餘的包。這個能夠告訴webpack沒必要解析
extensions 使用頻率高的寫在前面
複製代碼
webpack處理文本,圖片,css的時候,因爲js單線程的特性,只能一個一個文件的處理,HappyPack能夠將這個任務 分解到多個子線程裏面,子線程完畢後會把結果發送到主線程,從而加快打包速度
先安裝:
npm i -D happypack
webpack.prod.js
const HappyPack = require('happypack') //單進程轉多進程
const os = require('os')
const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length })
module: {
rules: [{
test: /\.js$/,
use: ['happypack/loader?id=happyBabel'],
exclude: /node_modules/,
include: [resolve('src'), resolve('node_modules/webpack-dev-server/client')]
}]
}
plugins:[
new HappyPack({
id: 'happyBabel',
loaders: ['babel-loader?cacheDirectory'],
threadPool: happyThreadPool
})
]
複製代碼
將不怎麼改變的第三方依賴,咱們能夠用DllPlugin DllReferencePlugin將它從依賴中抽離出來,這樣每一次打包就不用打包這些文件,加快了打包的速度;
可是webpack4.x的性能已經很好了,參考vue-cli也沒有使用dll抽離,因此咱們這裏也不使用了,這裏咱們使用 另外一個插件:hard-source-webpack-plugin ,這個插件會去對比修改了哪些配置,只去打包修改過了的配置 第一次打包速度正常,第二次打包速度能提高 50%+
npm i -D hard-source-webpack-plugin
webpack.prod.js
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin') //緩存第三方模塊
plugins: [
new HardSourceWebpackPlugin()
]
複製代碼
經過cdn加載的依賴,能夠在這裏設置,就不會經過webpack編譯
g-zip壓縮能夠將已經壓縮過的js,css再次壓縮一遍,減小了打包大小,須要nginx配置
npm i -D compression-webpack-plugin
webpack.prod.js
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ["js", "css"];
plugins:[
new CompressionWebpackPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
threshold: 10240, // 只有大小大於10k的資源會被處理
minRatio: 0.6 // 壓縮比例,值爲0 ~ 1
})
]
複製代碼
webpack.dev.js
const os = require('os')
devServer:{
host:()=>{
var netWork = os.networkInterfaces()
var ip = ''
for (var dev in netWork) {
netWork[dev].forEach(function (details) {
if (ip === '' && details.family === 'IPv4' && !details.internal) {
ip = details.address
return;
}
})
}
return ip || 'localhost'
}
}
複製代碼
根目錄新建vue.config.js 裏面配置一些公共的配置如:
const os = require('os')
module.exports = {
dev: {
host: getNetworkIp(), //端口號
port: 8999,
autoOpen: true, //自動打開
},
build: {
productionGzipExtensions: ["js", "css"], //須要開啓g-zip的文件後綴
productionGzip: false //是否開啓g-zip壓縮
}
}
//獲取本地ip地址
function getNetworkIp() {
var netWork = os.networkInterfaces()
var ip = ''
for (var dev in netWork) {
netWork[dev].forEach(function (details) {
if (ip === '' && details.family === 'IPv4' && !details.internal) {
ip = details.address
return;
}
})
}
return ip || 'localhost'
}
而後webpack.dev.js webpack.prod.js引入這個文件,獲取其中的配置就行了
複製代碼
先安裝:
npm i -D webpack-bundle-analyzer
webpack.prod.js
if (process.env.npm_config_report) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
prodConfig.plugins.push(
new BundleAnalyzerPlugin()
)
}
而後 npm run build --report就會彈出一個頁面,裏面就是打包大小分析
複製代碼
先安裝:
npm i -S vue-router axios vuex
而後在src裏面新建 -> router文件夾 ->新建index.js
index.js
import Vue from "vue"
import Router from "vue-router"
Vue.use(Router)
export default new Router({
mode: 'hash',
routes: [
{
path: '/',
name: 'home',
component: () => import(/* webpackChunkName: "home" */ "@/views/home"),
},
]
})
main.js
import router from "./router"
new Vue({
router,
render: h => h(App)
}).$mount('#app')
新建views -> Home.vue
隨便寫點東西,而後npm run dev 這樣就完成了一個路由了
複製代碼
到這裏webpack搭建vue項目就搭建完成了,咱們接下來就用這個webpack-vue去寫項目
未完待續...
完整的配置已經上傳到了github,有須要的能夠本身去看
github: github.com/biubiubiu01…