生产模式介绍
生产模式是开发完成代码后,需要得到代码将来部署上线
这个模式下我们主要对代码进行优化,让其运行性能更好
优化从两个角度出发:
- 优化代码运行性能
- 优化代码打包速度
生产模式准备
分别准备两个不同的配置文件来存放不同的配置
1. 文件目录
├── webpack-test (项目根目录)
├── config (Webpack配置文件目录)
│ ├── webpack.dev.js(开发模式配置文件)
│ └── webpack.prod.js(生产模式配置文件)
├── node_modules (下载包存放目录)
├── src (项目源码目录,除了html其他都在src里面)
│ └── 略
├── public (项目html文件)
│ └── index.html
├── .eslintrc.js(Eslint配置文件)
├── babel.config.js(Babel配置文件)
└── package.json (包的依赖管理配置文件)
2.修改webpack.dev.js
因为文件目录变了,所以所有绝对路径需要回退一层目录才能找到对应的文件
js
const path = require('path')
const ESLintWebpackPlugin = require('eslint-webpack-plugin')
const HTMLWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/main.js',
output: {
path: undefined,
filename: 'static/js/index.js',
clean: true
},
module: {
rules: [{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
},{
test: /\.s[ac]ss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
},{
test: /\.styl$/,
use: ['style-loader', 'css-loader', 'stylus-loader']
},{
test: /\.(png|jpe?g|gif|webp)$/,
type: 'asset',
parse: {
dataUrlCondition: {
maxSize: 10* 1024
}
},
generator: {
filename: 'static/images/[hash:10][ext][query]'
}
},{
test: /\.(ttf|woff2?|avi|mp3|mp4)$/,
type: 'asset/resource',
generator: {
filename: 'static/media/[hash:10][ext][query]'
}
},{
test: /\.js$/,
exclude: 'node_module',
loader: 'babel-loader'
}]
},
loaders: [
new ESLintWebpackPlugin({
context: path.resolve(__dirname, '../src')
}),
new HTMLWebpackPlugin({
template: path.resolve(__dirname, '../public/index.html')
})
],
devServer: {
host: 'localhost',
port: '8080',
open: true
},
mode: 'development'
}
运行开发模式的代码
npx webpack server --config ./config/webpack.dev.js
3. 修改webpack.prod.js
js
const path = require('path')
const ESLintWebpackPlugin = require('eslint-webpack-plugin')
const HTMLWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/main.js',
output: {
// 需要输出
path: path.resolve(__dirname, '../dist'),
filename: 'static/js/index.js',
clean: true
},
module: {
rules: [{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
},{
test: /\.s[ac]ss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
},{
test: /\.styl$/,
use: ['style-loader', 'css-loader', 'stylus-loader']
},{
test: /\.(png|jpe?g|gif|webp)$/,
type: 'asset',
parse: {
dataUrlCondition: {
maxSize: 10* 1024
}
},
generator: {
filename: 'static/images/[hash:10][ext][query]'
}
},{
test: /\.(ttf|woff2?|avi|mp3|mp4)$/,
type: 'asset/resource',
generator: {
filename: 'static/media/[hash:10][ext][query]'
}
},{
test: /\.js$/,
exclude: 'node_module',
loader: 'babel-loader'
}]
},
loaders: [
new ESLintWebpackPlugin({
context: path.resolve(__dirname, '../src')
}),
new HTMLWebpackPlugin({
template: path.resolve(__dirname, '../public/index.html')
})
],
// 无需开发服务器
// devServer: {
// host: 'localhost',
// port: '8080',
// open: true
// },
mode: 'production'
}
运行生产模式需要的代码
npx webpack --config ./config/webpack/prod.js
4. 配置运行指令
为了方便运行不同模式的指令,我们将指令定义在package.json中的scripts里面
json
// package.json
{
"scripts": {
"start": "npm run dev",
"dev": "npx webpack server --config ./config/webpack.dev.js",
"build": "npx webpack --config ./config/webpack.prod.js"
}
}
以后启动指令:
- 开发模式:npm start 或 npm run dev
- 生产模式:npm run build