webpack在Vue项目当中的应用(2)-生产模式

基本使用:

  1. 下载vue-loader(已下载过就不需要重新下载)
npm install -D vue-loader vue-template-compiler
  1. 下载其他的依赖(plugin , loader等..)(已下载过就不需要重新下载)
什么插件没下就下什么插件
npm i mini-css-extract-plugin css-minimizer-webpack-plugin terser-webpack-plugin image-minimizer-webpack-plugin -D

npm install imagemin-gifsicle imagemin-jpegtran imagemin-optipng imagemin-svgo -D

npm install imagemin-gifsicle imagemin-mozjpeg imagemin-pngquant imagemin-svgo -D

npm i copy-webpack-plugin -D
  1. 配置webpack.prod.js文件
// webpack.prod.js
const path = require("path");
const ESLintWebpackPlugin = require("eslint-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");//css压缩插件
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");//css压缩插件
const TerserWebpackPlugin = require("terser-webpack-plugin");//内置语法插件
const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");//图片压缩插件
const CopyPlugin = require("copy-webpack-plugin")
const { VueLoaderPlugin } = require("vue-loader");
const { DefinePlugin } = require("webpack");//webpack中专门用于定义环境变量的插件

const getStyleLoaders = (preProcessor) => {
return [
MiniCssExtractPlugin.loader,
"css-loader",
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
"postcss-preset-env", // 能解决大多数样式兼容性问题
],
},
},
},
preProcessor,
].filter(Boolean);
};

module.exports = {
entry: "./src/main.js",
output: {
path: undefined,
filename: "static/js/[name].[contenthash:10].js",
chunkFilename: "static/js/[name].[contenthash:10].chunk.js",
assetModuleFilename: "static/js/[hash:10][ext][query]",
clean: true,
},
module: {
rules: [
{
// 用来匹配 .css 结尾的文件
test: /\.css$/,
// use 数组里面 Loader 执行顺序是从右到左
use: getStyleLoaders(),
},
{
test: /\.less$/,
use: getStyleLoaders("less-loader"),
},
{
test: /\.(png|jpe?g|gif|svg)$/,
type: "asset",
parser: {
dataUrlCondition: {
maxSize: 10 * 1024, // 小于10kb的图片会被base64处理
},
},
},
{
test: /\.(ttf|woff2?)$/,
type: "asset/resource",
},
{
test: /\.(jsx|js)$/,
include: path.resolve(__dirname, "../src"),
loader: "babel-loader",
options: {
cacheDirectory: true,
cacheCompression: false,
plugins: [
// "@babel/plugin-transform-runtime" // presets中包含了
],
},
},
// vue-loader不支持oneOf
{
test: /\.vue$/,
loader: "vue-loader", // 内部会给vue文件注入HMR功能代码
options: {
// 开启缓存
cacheDirectory: path.resolve(
__dirname,
"node_modules/.cache/vue-loader"
),
},
},
],
},
plugins: [
new ESLintWebpackPlugin({
context: path.resolve(__dirname, "../src"),
exclude: "node_modules",
cache: true,
cacheLocation: path.resolve(
__dirname,
"../node_modules/.cache/.eslintcache"
),
}),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "../public/index.html"),
}),
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, "../public"),
to: path.resolve(__dirname, "../dist"),
toType: "dir",
noErrorOnMissing: true,
globOptions: {
ignore: ["**/index.html"],
},
info: {
minimized: true,
},
},
],
}),
new MiniCssExtractPlugin({
filename: "static/css/[name].[contenthash:10].css",
chunkFilename: "static/css/[name].[contenthash:10].chunk.css",
}),
new VueLoaderPlugin(),
new DefinePlugin({
__VUE_OPTIONS_API__: "true",
__VUE_PROD_DEVTOOLS__: "false",
}),
],
optimization: {
// 压缩的操作
minimizer: [
new CssMinimizerPlugin(),
new TerserWebpackPlugin(),
new ImageMinimizerPlugin({
minimizer: {
implementation: ImageMinimizerPlugin.imageminGenerate,
options: {
plugins: [
["gifsicle", { interlaced: true }],
["jpegtran", { progressive: true }],
["optipng", { optimizationLevel: 5 }],
[
"svgo",
{
plugins: [
"preset-default",
"prefixIds",
{
name: "sortAttrs",
params: {
xmlnsOrder: "alphabetical",
},
},
],
},
],
],
},
},
}),
],
splitChunks: {
chunks: "all",
},
runtimeChunk: {
name: (entrypoint) => `runtime~${entrypoint.name}`,
},
},
resolve: {
extensions: [".vue", ".js", ".json"],
},
mode: "production",
devtool: "source-map",
};
  1. 配置.eslintrc.js文件
// Eslint语法检查
module.exports = {
root: true,
env: {
node: true,//启用node的环境变量
},
extends: ["plugin:vue/vue3-essential", "eslint:recommended"],//继承vue3的官方规则和eslint默认的规则
parserOptions: {
parser: "@babel/eslint-parser",//解析选项使用babel
},
};
  1. 配置babel.config.js文件
module.exports = {
presets: ["@vue/cli-plugin-babel/preset"],//预设使用vue团队的即可
};
  1. 随便配置点Vue代码
  • main.js
// 使用的是Vue3
import { createApp } from "vue"//引入 createApp
import App from './App'

// 引入路由
import router from './router'

// 创建app并挂载到对应的元素上
createApp(App).use(router).mount(document.getElementById("app"));
  • router.js
import {createRouter , createWebHistory} from 'vue-router'

// 路由懒加载
const Home = () => import('../views/Home')
const About = () => import('../views/About')

export default createRouter({
// 路由模式
history: createWebHistory(),
// 路由规则配置
routes:[
{
path: '/home',
component: Home
},
{
path: '/about',
component: About
}
]
})
  • App.vue
<template>
<h1 class="app">hello! App</h1>
<Home/>

<router-link to="/home">Home</router-link><br><br>
<router-link to="/about">About</router-link>

<!-- 路由显示 -->
<router-view></router-view>
</template>

<script>
import Home from './views/Home.vue'
export default {
name: "App",
components:{
Home,
}
}
</script>

<style scoped>
.app{
color: aqua;
}
</style>
  1. 配置运行指令(再package.json中)
"scripts": {
"start": "npm run dev",
"dev": "cross-env NODE_ENV=development webpack serve --config ./config/webpack.dev.js",
"build": "cross-env NODE_ENV=production webpack --config ./config/webpack.prod.js"
},
  1. 初始化项目以及运行指令
# 初始化项目
npm init -y

# 打包项目
npm build

结果展示:(可以成功运行)

image