学习了webpack
以后,将本身的博客的前端进行重构,因为本身的博客是多页应用,因此研究了下多页应用的打包方案。在这里把最后配置的成果分享下,不足之处,请指正。(文字很少,全是代码,不是配置教程,因此没有特别详细的写,只是一个参考)css
项目地址:github.com/Ray-daydayu…html
首先先看下个人项目的目录结构前端
myblog-webpack
├── dist // 打包输出文件夹
├── src // 源代码
│ ├── api // 请求文件夹,存放封装的请求方法
│ ├── assets // 静态资源文件夹
│ ├── lib // 一些库
│ ├── pages // 页面
│ │ ├── about // 页面名称
│ │ │ ├── index.html // html模板
│ │ │ └── index.js // 入口js
│ │ ├── category
│ │ │ ├── index.html
│ │ │ └── index.js
│ │ ├── detail
│ │ │ ├── index.html
│ │ │ └── index.js
│ │ ├── index
│ │ │ ├── index.html
│ │ │ └── index.js
│ │ └── tag
│ │ ├── index.html
│ │ └── index.js
│ ├── styles // 样式文件
│ └── utils // 工具函数
├── package.json
├── README.md
├── package-lock.json
├── webpack.base.js // 公共配置
├── webpack.dev.js // 开发环境配置
└── webpack.prod.js // 生产环境配置
复制代码
最终打包输出的目录以下html5
dist
├── assets
├── css
│ ├── commons672a1e57.css
│ └── index6085d612.css
├── js
│ ├── aboutfc723f0e.js
│ ├── categorye4be3bd6.js
│ ├── commons78f1dd3f.js
│ ├── detail0df434c5.js
│ ├── indexe1e985d9.js
│ ├── markdown-it
│ │ ├── highlight.vendors.js
│ │ ├── markdown-it-integrated.js
│ │ ├── markdown-it-integrated.min.js
│ │ ├── markdown-it-integrated.min.js.LICENSE.txt
│ │ └── markdown-it.vendors.js
│ └── tagf1c1035c.js
├── about.html
├── category.html
├── detail.html
├── favicon.ico
├── index.html
└── tag.html
复制代码
webpack
配置文件注意:文中各部分所需的包名,并无书写,请在附录中查找!!!相关配置的意义能够查找官方文档node
entry
和设置html-webpack-plugin
多页应用的打包思路是每一个页面对应一个entry
,一个html-webpack-plugin
,但每次新增或删除页面须要改webpack
配置,因此须要根据文件目录动态获取入口,来设置html-webpack-plugin
。webpack
利用一个库glob
来实现对文件目录的读取 具体代码以下,相关配置的意义能够查找官方文档ios
const path = require('path')
const glob = require('glob')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const setMPA = () => {
const entry = {}
const htmlWebpackPlugins = []
const entryFiles = glob.sync(path.join(__dirname, './src/pages/*/index.js')) // 匹配各页面的对应入口文件
entryFiles.forEach((item) => {
const pageName = item.match(/pages\/(.*)\/index.js/)[1] // 获取文件夹名,即页面名称
entry[pageName] = item // 设置入口文件路径
// 设置html-webpack-plugin数组
htmlWebpackPlugins.push(
new HtmlWebpackPlugin({
template: path.join(__dirname, `./src/pages/${pageName}/index.html`),//模板地址
filename: `${pageName}.html`, //输出文件名
chunks: [pageName], // 插入的js chunk名称,和output有关
inject: true,
favicon: path.join(__dirname, './src/assets/favicon.ico'), // 图标
minify: { //压缩代码
html5: true,
collapseWhitespace: true,
preserveLineBreaks: false,
minifyCSS: true,
minifyJS: true,
removeComments: false
}
})
)
})
return {
entry,
htmlWebpackPlugins
}
}
const { entry, htmlWebpackPlugins } = setMPA()
复制代码
ES6
和 async、await
,以及eslint
使用 ES6
和 async、await
以及eslint
,须要借助babel-loader
和 eslint-loader
,其具体配置很简单git
babel
相关的库:@babel/core
、@babel/preset-env
、@babel/plugin-transform-regenerator
、@babel/plugin-transform-runtime
、 eslint
相关的库:babel-eslint
、eslint
github
module.rules
的配置{
test: /\.js$/,
use: ['babel-loader', 'eslint-loader']
}
复制代码
.babelrc
文件的配置{
"presets": [
"@babel/preset-env"
],
"plugins": [ //设置支持async和await
"@babel/plugin-transform-runtime",
"@babel/plugin-transform-regenerator"
]
}
复制代码
.eslintrc.js
的配置module.exports = {
parser: 'babel-eslint',
extends: ['alloy'], // eslint标准请自行选择下载
env: {
browser: true,
node: true
},
rules: {
'no-new': 'off',
'no-proto': 'off'
// 'no-console': 'error'
}
}
复制代码
利用url-loader
,具体module.rules
配置以下web
{
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
esModule: false,
name: '[name][hash:8].[ext]', //文件指纹
limit: 10240, // 转base64
outputPath: './assets/images/' // 图片文件输出目录和publicPath有关
}
}
]
}
复制代码
自动清理打包目录利用clean-webpack-plugin
插件,复制文件利用copy-webpack-plugin
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const basePlugins = [
...htmlWebpackPlugins, //以前配置的htmlWebpackPlugin数组
new CleanWebpackPlugin(), // 自动清理构建目录
new CopyWebpackPlugin({
patterns: [
{
from: path.join(__dirname, 'src/lib/markdown-it'), // 源
to: path.join(__dirname, 'dist/js/markdown-it') // 目标位置
}
]
})
]
复制代码
.browserslistrc
配置# Browsers that we support
last 2 version
> 1%
iOS 7
复制代码
devServer
具体配置以下:
plugins: [...basePlugins, new webpack.HotModuleReplacementPlugin()], //热更新插件
devServer: {
contentBase: 'dist', // 开启服务的目录
hot: true, //热更新开启
proxy: {
'/api': {
target: 'http://raydaydayup.cn:3000', //代理
pathRewrite: { '^/api': '' }
}
}
}
复制代码
CSS
相关配置module.rules
的配置以下
{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
复制代码
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist')
}
复制代码
output: {
filename: 'js/[name][chunkhash:8].js', // 统一输出到js文件夹
path: path.join(__dirname, 'dist'),
publicPath: '/' // 服务路径
}
复制代码
js
代码压缩利用terser-webpack-plugin
插件,配置optimization
const TerserPlugin = require('terser-webpack-plugin')
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
include: [/\.js$/]
})
]
}
复制代码
CSS
相关配置CSS
代码压缩,利用optimize-css-assets-webpack-plugin
和cssnano
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
plugins: [
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano')
})
]
复制代码
CSS
分离单独文件,利用mini-css-extract-plugin
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module: {
rules: [
...baseModuleRules,
{
test: /\.less$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: true
}
},
'css-loader',
'less-loader'
]
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: true
}
},
'css-loader'
]
}
]
},
plugins: [
...basePlugins,
new MiniCssExtractPlugin({
filename: 'css/[name][contenthash:8].css' // 输出文件名和地址
}),
],
复制代码
PostCSS
插件 autoprefixer
自动补齐CSS3
前缀module: {
rules: [
...baseModuleRules,
{
test: /\.less$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: true
}
},
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: () => [require('autoprefixer')]
}
},
'less-loader'
]
},
]
},
复制代码
配置optimization.splitChunks
optimization: {
splitChunks: {
cacheGroups: {
commons: {
name: 'commons',
chunks: 'all',
minChunks: 2 // 最少引用两次
}
}
}
}
复制代码
html-webpack-plugin
模板使用以及html-loader
html
片断,在相应位置写下面的代码就像,须要注意的是,html-loader
加载的html
片断内部<%%>
语法会失效<%= require("html-loader!@/components/recent.html") %>
复制代码
html
中图片的加载。在 html
中相关的img
标签的图片,html-loader
加载时会自动调用url-loader
,可是存在问题,加载出来的路径没有""
。为了不这个问题,我就没有再html-loader
加载的html
片断中使用图片,而是直接再html
模板中直接用require
<img src="<%= require('@/assets/logo362x82.png') %>" alt="" />
复制代码
webpack.base.js
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const path = require('path')
const glob = require('glob')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const setMPA = () => {
const entry = {}
const htmlWebpackPlugins = []
const entryFiles = glob.sync(path.join(__dirname, './src/pages/*/index.js'))
entryFiles.forEach((item) => {
const pageName = item.match(/pages\/(.*)\/index.js/)[1]
entry[pageName] = item
htmlWebpackPlugins.push(
new HtmlWebpackPlugin({
template: path.join(__dirname, `./src/pages/${pageName}/index.html`),
filename: `${pageName}.html`,
chunks: [pageName],
inject: true,
favicon: path.join(__dirname, './src/assets/favicon.ico'),
minify: {
html5: true,
collapseWhitespace: true,
preserveLineBreaks: false,
minifyCSS: true,
minifyJS: true,
removeComments: false
}
})
)
})
return {
entry,
htmlWebpackPlugins
}
}
const { entry, htmlWebpackPlugins } = setMPA()
const baseModuleRules = [
{
test: /\.js$/,
use: ['babel-loader', 'eslint-loader']
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
esModule: false,
name: '[name][hash:8].[ext]',
limit: 10240,
outputPath: './assets/images/'
}
}
]
}
]
const basePlugins = [
...htmlWebpackPlugins,
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: path.join(__dirname, 'src/lib/markdown-it'),
to: path.join(__dirname, 'dist/js/markdown-it')
}
]
})
]
module.exports = {
entry,
baseModuleRules,
basePlugins
}
复制代码
webpack.dev.js
const webpack = require('webpack')
const path = require('path')
const { entry, baseModuleRules, basePlugins } = require('./webpack.base.js')
module.exports = {
mode: 'development',
entry,
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist')
},
module: {
rules: [
...baseModuleRules,
{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
resolve: {
// 设置别名
alias: {
'@': path.join(__dirname, 'src') // 这样配置后 @ 能够指向 src 目录
}
},
plugins: [...basePlugins, new webpack.HotModuleReplacementPlugin()],
devServer: {
contentBase: 'dist',
hot: true,
proxy: {
'/api': {
target: 'http://raydaydayup.cn:3000',
pathRewrite: { '^/api': '' }
}
}
}
}
复制代码
webpack.prod.js
const { entry, baseModuleRules, basePlugins } = require('./webpack.base.js')
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
module.exports = {
mode: 'production',
entry,
output: {
filename: 'js/[name][chunkhash:8].js',
path: path.join(__dirname, 'dist'),
publicPath: '/'
},
resolve: {
// 设置别名
alias: {
'@': path.resolve('src') // 这样配置后 @ 能够指向 src 目录
}
},
module: {
rules: [
...baseModuleRules,
{
test: /\.less$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: true
}
},
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: () => [require('autoprefixer')]
}
},
'less-loader'
]
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: true
}
},
'css-loader'
]
}
]
},
plugins: [
...basePlugins,
new MiniCssExtractPlugin({
filename: 'css/[name][contenthash:8].css'
}),
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano')
})
],
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
include: [/\.js$/]
})
],
splitChunks: {
cacheGroups: {
commons: {
name: 'commons',
chunks: 'all',
minChunks: 2
}
}
}
}
}
复制代码
{
"name": "myblog-webpack",
"version": "1.0.0",
"description": "",
"main": ".eslintrc.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config webpack.prod.js",
"dev": "webpack-dev-server --config webpack.dev.js --open"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.10.2",
"@babel/plugin-transform-regenerator": "^7.10.3",
"@babel/plugin-transform-runtime": "^7.10.3",
"@babel/preset-env": "^7.10.2",
"autoprefixer": "^9.8.4",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^6.0.3",
"css-loader": "^3.6.0",
"cssnano": "^4.1.10",
"eslint": "^7.2.0",
"eslint-config-alloy": "^3.7.2",
"eslint-loader": "^4.0.2",
"file-loader": "^6.0.0",
"glob": "^7.1.6",
"html-loader": "^1.1.0",
"html-webpack-plugin": "^4.3.0",
"less-loader": "^6.1.2",
"mini-css-extract-plugin": "^0.9.0",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"postcss-loader": "^3.0.0",
"style-loader": "^1.2.1",
"terser-webpack-plugin": "^3.0.5",
"url-loader": "^4.1.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"axios": "^0.19.2"
}
}
复制代码