该常用配置使用的npm包:
"dependencies": {
"autoprefixer": "^10.4.4",
"clean-webpack-plugin": "^4.0.0",
"css-loader": "^6.7.1",
"html-webpack-plugin": "^5.5.0",
"postcss-loader": "^6.2.1",
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2"
},
"devDependencies": {
"@babel/core": "^7.17.7",
"@babel/plugin-transform-runtime": "^7.17.0",
"@babel/preset-env": "^7.16.11",
"babel-loader": "^8.2.3",
"compression-webpack-plugin": "^9.2.0",
"cross-env": "^7.0.3",
"file-loader": "^6.2.0",
"mini-css-extract-plugin": "^2.6.0",
"progress-bar-webpack-plugin": "^2.1.0",
"style-loader": "^3.3.1",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-dev-server": "^4.7.4"
}
webpack.config.js 实例:
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CompressionPlugin = require("compression-webpack-plugin");
const chalk = require('chalk')
const ProgressBarPlugin = require('progress-bar-webpack-plugin')
console.log('process.env.NODE_ENV=', process.env.NODE_ENV)
const config = {
entry: './src/index.js',
output: {
filename: "[name].[contenthash:8].js",
path: path.join(__dirname, 'dist'),
clean: true
},
devServer: {
hot: true,
open: true,
compress: true,
port: 8080,
static: {
directory: path.join(__dirname, "../public"),
},
},
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: { postcssOptions: { plugins: ['autoprefixer'] } }
}
]
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset',
generator: {
filename: 'static/img/[name].[hash:7][ext]'
}
},
{
test: /(\.jsx|\.js)$/,
use: ['babel-loader'],
exclude: /node_modules/
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
filename: 'index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].[hash:8].css'
}),
new ProgressBarPlugin({
format: `:msg [:bar] ${chalk.green.bold(':percent')} (:elapsed s)`
}),
new CompressionPlugin()
],
resolve: {
extensions: [".js", ".jsx", ".json", ".vue"],
alias: {
"@": path.resolve(__dirname, "../src"),
},
},
optimization: {
splitChunks: {
chunks: "all",
name: "vendor",
cacheGroups: {
lodash: {
name: "lodash",
chunks: "async",
test: /[\\/]node_modules[\\/]lodash[\\/]/,
priority: 40,
},
"async-common": {
chunks: "async",
minChunks: 2,
name: "async-commons",
priority: 30,
},
commons: {
name: "commons",
chunks: "all",
minChunks: 2,
priority: 20,
},
},
},
},
}
module.exports = (env, argv) => {
console.log('argv.mode=', argv.mode)
return config
}
根目录的.babelrc文件 实例:
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-runtime"]
}
package.json 新增serve和build:
"scripts": {
"serve": "cross-env NODE_ENV=dev webpack serve --mode development",
"build": "cross-env NODE_ENV=prod webpack --mode production",
}
package.json 新增browserslist配置:
browserslist与scripts同级
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
nginx配置gzip压缩
nginx配置gzip压缩,静态资源会被自动压缩。不需要所谓的插件,vite-plugin-compression compression-webpack-plugin
#Gzip module
gzip on;
gzip_disable "MSIE [1-6].";
gzip_min_length 1k;
gzip_http_version 1.1;
gzip_types text/css text/xml application/javascript;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)