乙個合格的Webpack4配置工程師素養 第二部分

2021-09-11 11:54:59 字數 2075 閱讀 3374

乙個合格的webpack4配置工程師素養:第一部分

我們給打包的檔案打上hash是為了解決快取更新問題,常見需要打上hash的地方有。

output: 

複製**

// 提取css

new minics***tractplugin()

複製**

但是打上hash我們怎麼引入是乙個問題。

html-webpack-plugin外掛程式可以把js/css注入到乙個模板檔案, 所以不需要再手動更改引用。

npm i -d html-webpack-plugin

複製**

更改配置檔案

const htmlwebpackplugin = require('html-webpack-plugin')

plugins: [

// 打包模板

new htmlwebpackplugin(

})],複製**

設定乙個模板檔案。

// index.html

複製**

打包後的檔案:

複製**

npm i -d clean-webpack-plugin

複製**

修改配置檔案

const cleanwebpackplugin = require('clean-webpack-plugin')

plugins: [

// 清理dist目錄

new cleanwebpackplugin(['dist'])

]複製**

我們這裡只是為了測試, 在index.html模板檔案新增乙個dom去使用。

// index.html

"logo">

// base.scss

.logo

複製**

使用file-loader來處理檔案的匯入

npm i -d file-loader

複製**

修改配置檔案

rules: [

// 處理檔案,},

]},

]

複製**

下面繼續對進行優化和壓縮

npm i -d image-webpack-loader

複製**

修改配置檔案

// 處理檔案

, },

,// optipng.enabled: false will disable optipng

optipng: ,

pngquant: ,

gifsicle: ,

// the webp option will enable webp

webp: }}

]},複製**

壓縮前大小181.46kb.

壓縮後29kb.

通過把一些小的轉為base65(dataurl)可以減少http請求, 提公升訪問效率。

npm i -d url-loader

複製**

修改配置檔案

// 處理檔案

, },

,// optipng.enabled: false will disable optipng

optipng: ,

pngquant: ,

gifsicle: ,

// the webp option will enable webp

webp: }}

]},複製**

這裡測試的話我們需要準備乙個小的即可,如上述配置所述只要小於10kb就會用base64替代。.

字型處理的話配置如下:

},複製**

webpack4配置乙個多頁面

因為專案是用glpu搭建的工作流,在轉jenkins自動發布的時候,考慮到如果需要多頁面動態打包,需要改寫glup配置 不會glup也不想學 就將專案重構為webpack4.掃瞄專案多頁面入口 區分生產和開發環境webpck配置 指定專案打包 使用shell指令進行打包操作 如何處理不同的模組模式 ...

webpack4配置詳解

方式一 單檔案寫法 entry 方式二 多檔案寫法 entry output 常用的有三種 模板描述 hash模組識別符號的hash,一般應用於filename name hash js chunkhash按需載入塊內容的hash,根據chunk自身的內容計算而來,js name chunkhash...

webpack4的簡易配置

scripts 處理html const htmlwebpackplugin require html webpack plugin 清空dist const cleanwebpackplugin require clean webpack plugin 分離css const minics tra...