在前端工作中,开发vue的话,基本上使用的是基于vue-cli vite等构建的,基于其原理,借鉴了很多大佬的经验,本文记录了使用Webpack从零开始搭建一个TS+Vue3项目的步骤,给大家分享一下。文章后面有源码,包版本。
使用的node和pnpm版本如下
1.创建一个目录 vue-project 进行初始化
cd vue-project
npm init
tsc --init
2.ts.config.json配置
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"lib": [
"ESNext",
"DOM",
"DOM.Iterable"
],
"useDefineForClassFields": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
// "allowImportingTsExtensions": true,
// "noEmit": true,
"moduleResolution": "Bundler",
"resolveJsonModule": false,
"isolatedModules": false,
"jsx": "preserve",
"strict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noFallthroughCasesInSwitch": false,
"baseUrl": ".",
"paths": {
"@/*": [
"src/*"
],
}
},
"include": [
"src/**/*.ts",
"src/**/*.js",
"src/**/*.d.ts",
"src/**/*.vue",
"build/**/*.ts",
],
"exclude": [
"node_modules",
"dist"
]
}
3.创建文件结构目录
vue-project > build > webpack.base.js 针对webpack的基本配置
vue-project > build > webpack.dev.js 针对开发环境的配置
vue-project > build > webpack.prod.js 针对正式环境的配置
vue-project > src > main.ts 主入口
import { createApp } from 'vue';
import App from '@/App.vue';
const app = createApp(App);
app.mount('#app')
vue-project > src > App.vue 入口模板
<template> <div> webpack project </div> </template> <script setup lang="ts"> </script> <style scoped></style>
vue-project > public > index.html html入口
/*模板语法 可读取在htmlWebpackPlugin的配置*/
/*部分节点省略*/
<title><%= htmlWebpackPlugin.options.title %></title>
<div id="app"></div> 作为vue的挂载节点
编辑类型声明文件 src > shims.vue.d.ts 对.vue文件的声明
declare module '*.vue' {
import type { DefineComponent } from 'vue';
const component: DefineComponent<{}, {}, any>
export default component;
}
4. 安装插件 html-webpack-plugin vue-loader
pnpm add @types/node html-webpack-plugin vue-loader webpack webpack-cli -D
//webpack.base.js 文件
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');
/**
* 类型推导
* @type {import('webpack').Configuration}
*/
const config = {
optimization:{
minimize: false,//这个选项是不压缩代码 会导致文件过大的警告 可用于测试
},
mode: "production",//定义一个模式 不然会报错
entry: path.resolve(__dirname, '../src/main.ts'),//入口
output: {
path: path.join(__dirname, '../dist'), //打包目录
filename: 'js/[name].[contenthash:14].js', //打包后的文件名
clean: true,//每次打包时清除之前文件夹
// publicPath: '/',//配置的前置目录 可以视情况配置 在开发环境下应该默认不配置
},
resolve: {
alias: {
'@': path.resolve(__dirname, '../src'), //配置别名
vue$: 'vue/dist/vue.runtime.esm-bundler.js',//针对vue
},
extensions: ['.js', '.ts', '.vue', '.json'], //文件名后缀
},
plugins: [
//处理html模板
new HtmlWebpackPlugin({
template: path.resolve(__dirname, '../public/index.html'),
title: "Webpack Vue Template",//注入的标题
}),
new VueLoaderPlugin(),
],
module: {
rules: [
{
test: /\.vue$/, use: 'vue-loader',
},
],
},
}
module.exports = config;
5.尝试打包
确保全局安装了webpack
webpack -c build/webpack.base.js
6. 配置 ts-loader
在main.ts中书写ts代码时,运行打包会报错,需要处理ts代码。
pnpm add ts-loader -d
在rules数组中添加配置 再次打包正常输出。
rules:[
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true, //关闭类型监察
// configFile: path.resolve(process.cwd(), 'tsconfig.json'),//读取配置文件
appendTsSuffixTo: ['\\.vue$'], //给vue文件添加后缀
},
},
]
},
]
7. babel 代码降级 兼容性处理
pnpm add babel-loader @babel/core @babel/preset-env @babel/preset-typescript @babel/runtime-corejs3 @babel/plugin-transform-runtime -D
pnpm add core-js regenerator-runtime
针对兼容性有二种不同方式的处理
//babel.config.js文件 方案二选一
module.exports = {
presets: [
[
"@babel/preset-env",//使用babel的预设 基本语法的处理 变量的转换
//方案(1)//会实现array的原型
// {
// //为 false 全部引入
// //为 entry 在入口文件中必须指定引入
// // import "core-js/stable";
// // import "regenerator-runtime/runtime";
// // usage 按需引入
// useBuiltIns: "usage",// polifill(兼容性垫片)
// corejs: 3,//指定core-版本
// },
],
[
"@babel/preset-typescript",//使用babel-ts的预设处理
{
allExtensions: true, //支持所有文件扩展名
},
],
],
plugins: [
// 方案(2)使用这个插件可以避免array原型的实现
[
'@babel/plugin-transform-runtime',
{
corejs: 3,
}
],
],
}
8. .browserslistrc 配置
主要是为了兼容其他浏览器等
//.browserlistrc文件
> 1% #代表全球超过1%使用的浏览器
last 3 versions #最后的一个版本
not ie < 12 #ie12以前不处理
not dead
9. 配置合并
$ pnpm add webpack-merge -D
把 webpack.base.js 合并到 开发配置和生产配置
//webpack.dev.js文件 与 webpack.prod.js 文件一致
const baseConfig = require('./webpack.base');
const { merge } = require('webpack-merge');
/**
* 类型推导
* @type {import('webpack').Configuration}
*/
const config = {
mode: 'development',
//mode: 'production',//生产环境配置
};
module.exports = merge(baseConfig, config);
10. 修改script脚本搭建开发环境
webpack开发服务器
pnpm add webpack-dev-server -D
//package.json文件 配置脚本
"scripts": {
"dev": "webpack-dev-server -c build/webpack.dev.js",
"build": "webpack -c build/webpack.prod.js"
},
//webpack.dev.js文件 添加配置
config = {
devServer: {
open: true,
historyApiFallback: true,// 所有404页面指向index.html,
devMiddleware: {
stats: 'errors-only', // 只在发生错误时输出
}
},
},
运行 pnpm run serve 发现找不到页面,原因是因为之前在 output > publicPath 配置了前置目录 注释后可以就可以运行开发服务器
output: {
// publicPath: '/',//配置的前置目录 可以视情况配置 在开发环境下应该默认不配置
},
配置一下命令行信息使用插件
pnpm add friendly-errors-webpack-plugin -D
//webpack.dev.js文件
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const os = require('os');
// 获取所有网络接口信息
const networkInterfaces = os.networkInterfaces();
let ipAddress; //本机IP地址
Object.keys(networkInterfaces).forEach((interfaceName) => {
const interfaces = networkInterfaces[interfaceName];
// 遍历每个网络接口
interfaces.forEach((iface) => {
if (iface.family === 'IPv4' && !iface.internal) {
// 如果是 IPv4 且不为内部接口(通常指回环接口)则将其设置为本机IP地址
ipAddress = iface.address;
}
});
});
//增加插件配置
const config = {
plugins: [
new FriendlyErrorsWebpackPlugin({
compilationSuccessInfo: {
messages: [
`APP running at:`,
`- Local: ${"http://localhost:8080/"}`,
`- Network: http://${ipAddress}:8080/`
],
},
clearConsole: true,//是否每次编译之间清除控制台
}),
]
};
11. 报错信息 控制台警告
//webpack.dev.js文件 添加配置
const config = {
//开发环境下,一般使用 eval-cheap-module-source-map 选项
devtool: 'eval-cheap-module-source-map',
};
//webpack.pord.js文件 添加配置
const config = {
//生产环境,一般不配置
// 如果要做前端监控,可以使用 hidden-source-map或者nosources-source-map
// devtool: 'source-map',
};
控制有打印警告,原因是因为又要必要变量没有配置,使用webpack默认插件设置变量
//webpack.base.js文件 添加配置
const webpack = require('webpack');
const config = {
...
plugins: [
new webpack.DefinePlugin({
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: false,
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false,
}),
],
},
12. 环境变量
用于读取环境变量
pnpm install dotenv -D
需要注意读取到的配置都是字符串类型,如果需要使用其原始值,需要用JSON.parse包裹
//package.json文件 更换配置脚本
//注入环境变量 不同模式使用不同的环境 --node-env development
"scripts": {
"serve": "webpack-dev-server -c build/webpack.dev.js --node-env development",
"build:prod": "webpack -c build/webpack.prod.js --node-env production"
},
# .env文件
VUE_APP_TITLE = Webpack + TS + Vue + 默认项目
BASE_APP_API = http://liujianboke.top
NODE_ENV = development
在客户端中可以访问 process.env 是因为 webpack 嵌入了该变量
//NODE_ENV BASE_APP_ VUE_APP_ 等开头需要静态的嵌入到客户端中
//可以避免意外公开机器上可能具有相同名称的私钥
//webpack.base.js 文件 增加 修改plugins配置
const fs = require('fs');
const dotenv = require('dotenv')
// 当前的环境 production || development
const NODE_ENV = process.env.NODE_ENV;
// 环境变量文件可以如下
const dotenvFileArr = [
`.env`,
`.env.${NODE_ENV}`,
`.env.local`,
`.env.${NODE_ENV}.local`,
];
const envObj = {};
dotenvFileArr.forEach(async dotenvFile => {
if (fs.existsSync(dotenvFile)) {
//使用dotenv写入process.env中
dotenv.config({ path: dotenvFile })
const envObjFile = dotenv.parse(fs.readFileSync(dotenvFile));
for (const key in envObjFile) {
if (/^(VUE_APP_|BASE_URL|NODE_ENV).?/.test(key)) {
envObj['process.env.' + key] = JSON.stringify(process.env[key]);
}
}
}
});
const config = {
...
plugins: [
new webpack.DefinePlugin({
...envObj,//注入客户端
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: false,
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false,
}),
],
};
运行pnpm run serve 或 pnpm run build:prod 打印 process.env 会在环境变量中发现在.env中的配置
13. 样式处理
1.css处理
创建一个css文件,在main.ts中引入,项目报错,无法识别css文件。
pnpm add style-loader css-loader -D
//webpack.base.js文件 增加配置
rules:[
{
test: /\.css$/, use: ['style-loader', 'css-loader'],
},
]
2. scss预处理器
如果要使用css预处理器,如sass等,添加相应的加载器即可
pnpm add sass sass-loader -D
rules:[
{
test: /\.s[ac]ss$/, use: ['style-loader', 'css-loader','sass-loader'],
},
]
3. 生成单个的css文件 处理 /\.module\.css/ 文件 使用css后处理器
pnpm add mini-css-extract-plugin postcss postcss-loader postcss-preset-env style-resources-loader -D
在开发环境下不生成文件,提高效率构建效率,通过后处理器兼容浏览器
注意 loader 是从后往前运行的 顺序有误会报错
//webpack.base.js文件
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
//处理css文件
const getStyleLoader = (cssFileType) => {
const loaderArr = [
NODE_ENV === 'development' ? 'style-loader' : MiniCssExtractPlugin.loader,
];
if (cssFileType === 'moduleCss') {
loaderArr.push({
loader: "css-loader",
options: {
modules: {
localIdentName: '[name]_[local]_[hash:base64:8]'
}
},
},)
} else {
loaderArr.push('css-loader')
}
loaderArr.push('postcss-loader');
if (cssFileType === 'sass') {
loaderArr.push('sass-loader')
}
return loaderArr;
}
//更改配置
rules:[
...
{
test: /\.css$/i,
exclude: [/node_modules/, /\.module\.css$/i],
use: getStyleLoader(),
},
{
test: /\.module\.css$/i,
exclude: [/node_modules/],
use: getStyleLoader('moduleCss'),
},
{
test: /\.s[ac]ss$/,
use: [
...getStyleLoader('sass'),
{
//这个加载器是用于加载一些sass变量以供全局使用,可视文件夹目录自行配置
//在vue-cli中也提供了类似的选项
loader: 'style-resources-loader',
options: {
patterns: [
//改目录下的var.scss内的变量,全局scss文件都可以访问
path.resolve(__dirname, '../src/assets/style/var.scss'),
]
}
}
],
},
]
针对postcss配置
//postcss.config.js文件
module.exports = {
plugins: [
['postcss-preset-env'],
]
}
14. 静态资源处理
pnpm add copy-webpack-plugin -D
注意这个插件不同的版本配置不一样
//webpack.base.js文件添加
const CopyWebpackPlugin = require('copy-webpack-plugin');
const config = {
plugins: [
...
//使用这个插件复制public目录的其他文件到输出目录 比如 favicon.ico 文件
new CopyWebpackPlugin({
patterns: [{
from: path.resolve(__dirname, "../public"),//当前工作路径是在dist文件夹内,搜易这里的from就是项目目录/public文件夹内。(dist和public是同级的)
to: './',//放到output文件夹下,也就是当前工作文件夹dist内
globOptions: {
dot: true,
gitignore: false,
ignore: [ // 配置不用copy的文件
'**/index.html',
'**/DS_Store',
]
}
}]
}),
],
module:{
rules:[
...
{
//相对路径 webpack 会默认引用
//绝对路径则会 找不到 會被 webpack 拷貝
test: /\.(png|jpe?g|gif|webp|svif)(\?.*)?$/,
type: 'asset',//默认8kb一下的会转换为base64
generator: {
filename: 'img/[name][ext][query]'
}
},
{
// 处理字体文件
test: /\.(woff2?|eot|otf|ttf)(\?.*)?$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 20 * 1024,//20kb一下hi被转换成base64
}
},
generator: {
filename: 'fonts/[name][ext][query]',
}
},
{
//处理视频,音频
test: /\.(mp4|webm|mp3|ogg|wav|flac|acc|rmvb)(\?.*)?$/,
type: 'asset',
generator: {
filename: 'movie/[name][ext][query]',
}
},
]
}
}
15. eslint 配置
pnpm add eslint eslint-webpack-plugin eslint-plugin-vue @typescript-eslint/eslint-plugin @typescript-eslint/parser -D
vscode 也会读取eslint的配置文件,提供更好的代码检查
//webpack.base.js
const EslintWebpackPlugin= require('eslint-webpack-plugin');
const config = {
...
plugin:[
new EslintWebpackPlugin({
extensions: [
'.js', '.jsx', '.vue', '.ts', '.tsx'
],
// 指定检查文件的根目录
context: path.resolve(__dirname, "../src"),
exclude: "node_modules",
cache: true,//开启缓存 用于优化
//缓存目录
cacheLocation: path.resolve(__dirname, "../node_modules/.cache/.eslintcache")
}),
]
}
//.eslintrc.js
//配置可视情况修改 也可以关闭检查
module.exports = {
root: true,
env: {
node: true,
browser: true,
es6: true,
"vue/setup-compiler-macros": true
},
parser: 'vue-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser',
ecmaVersion: 2020,
sourceType: 'module',
jsxPragma: 'React',
ecmaFeatures: {
jsx: true,
},
},
extends: [
"plugin:vue/vue3-recommended",
"plugin:@typescript-eslint/recommended",
],
rules: {
"vue/html-self-closing": ["error", {//html标签自动关闭
"html": {
"void": "always",
"normal": "never",
"component": "always"
},
"svg": "always",
"math": "always"
}],
//值可以是 off error warn
// eslint (http://eslint.cn/docs/rules)
"no-var": "error", // 要求使用 let 或 const 而不是 var
"no-multiple-empty-lines": ["error", { max: 1 }], // 不允许多个空行
"prefer-const": "off", // 使用 let 关键字声明但在初始分配后从未重新分配的变量,要求使用 const
"no-use-before-define": "off", // 禁止在 函数/类/变量 定义之前使用它们
"no-irregular-whitespace": "off", // 禁止不规则的空白
// typeScript (https://typescript-eslint.io/rules)
"@typescript-eslint/no-unused-vars": "error", // 禁止定义未使用的变量
"@typescript-eslint/prefer-ts-expect-error": "off", // 禁止使用 @ts-ignore
"@typescript-eslint/no-inferrable-types": "off", // 可以轻松推断的显式类型可能会增加不必要的冗长
"@typescript-eslint/no-namespace": "off", // 禁止使用自定义 TypeScript 模块和命名空间。
"@typescript-eslint/no-explicit-any": "off", // 禁止使用 any 类型
"@typescript-eslint/ban-types": "off", // 禁止使用特定类型
"@typescript-eslint/explicit-function-return-type": "off", // 不允许对初始化为数字、字符串或布尔值的变量或参数进行显式类型声明
"@typescript-eslint/no-var-requires": "off", // 不允许在 import 语句中使用 require 语句
"@typescript-eslint/no-empty-function": "off", // 禁止空函数
"@typescript-eslint/no-use-before-define": "off", // 禁止在变量定义之前使用它们
"@typescript-eslint/ban-ts-comment": "off", // 禁止 @ts-<directive> 使用注释或要求在指令后进行描述
"@typescript-eslint/no-non-null-assertion": "off", // 不允许使用后缀运算符的非空断言(!)
"@typescript-eslint/explicit-module-boundary-types": "off", // 要求导出函数和类的公共类方法的显式返回和参数类型
// vue (https://eslint.vuejs.org/rules)
"vue/require-valid-default-prop": "off", // 强制 props 默认值有效
"vue/script-setup-uses-vars": "error", // 防止<script setup>使用的变量<template>被标记为未使用,此规则仅在启用该no-unused-vars规则时有效。
"vue/v-slot-style": "error", // 强制执行 v-slot 指令样式
"vue/no-mutating-props": "off", // 不允许组件 prop的改变
"vue/no-v-html": "off", // 禁止使用 v-html
"vue/custom-event-name-casing": "off", // 为自定义事件名称强制使用特定大小写
"vue/attributes-order": "off", // vue api使用顺序,强制执行属性顺序
"vue/one-component-per-file": "off", // 强制每个组件都应该在自己的文件中
"vue/html-closing-bracket-newline": "off", // 在标签的右括号之前要求或禁止换行
"vue/max-attributes-per-line": "off", // 强制每行的最大属性数
"vue/multiline-html-element-content-newline": "off", // 在多行元素的内容之前和之后需要换行符
"vue/singleline-html-element-content-newline": "off", // 在单行元素的内容之前和之后需要换行符
"vue/attribute-hyphenation": "off", // 对模板中的自定义组件强制执行属性命名样式
"vue/require-default-prop": "off", // 此规则要求为每个 prop 为必填时,必须提供默认值
"vue/multi-word-component-names": "off", // 要求组件名称始终为 “-” 链接的单词
"vue/html-indent": "off",//每行的缩进
}
};
//.eslintignore 文件
*.sh
node_modules
*.md
*.woff
*.ttf
.vscode
.idea
dist
/public
/docs
.husky
.local
/bin
mock
16. 使用webpack-chain
webpack-chain 但是这个包已经不在更新了,webpack5使用需要考虑有些配置没有。
17. 优化-预加载和分包
为什么要做代码拆分:
1.业务代码-同步 异步。
2.第三方包代码。
3.webpack的runtime代码,链接模快化应用产生的代码。
目的有以下几点,是为了浏览器的长效缓存机制、将大的chunk拆分为若干个小的chunk,可以缩短单个资源下载的时间、将公共资源分离出来,避免重复打包同样的代码,减小代码体积。
webpack.optimization 优化章节
1.路由分包
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
// webpack 默认会对异步的操作分包
component: () => import(
/* webpackChunkName: "about" */ //魔术注释 在打包时会使用这个名字
/* webpackPrefetch: true */ // 浏览器空闲时 预加载
'../views/AboutView.vue')
}
]
2.代码拆分
module.exports = {
//优化
optimization: {
chunkIds: "named",
runtimeChunk: "single",
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: {
name: "vendors",
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: "async",
reuseExistingChunk: true,
},
lib: {
test(module) {
//如果模块大于160kb,就打包到lib.js中
return module.size() > 160000 && module.nameForCondition() && module.nameForCondition().match(/[\\/]node_modules[\\/]/);
},
name(module) {
const basename = path.basename(module.nameForCondition());
const pattern = /[a-z-?A-Z]+(?=\.)/;
console.log(basename.match(pattern));
return basename.match(pattern)[0]
},
minChunks: 1,
priority: 10,
reuseExistingChunk: true,
},
//模块化小于160kb的全部单独打包
module: {
name(module) {
const basename = path.basename(module.nameForCondition());
const pattern = /[a-z-?A-Z]+(?=\.)/;
return basename.match(pattern)[0]
},
test: /[\\/]node_modules[\\/]/,
minChunks: 1,
priority: 5,
reuseExistingChunk: true,
},
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
name: "vendorsDefault",
priority: -10,
reuseExistingChunk: true,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
}
};
分割之后,过多的文件导致浏览器并发限制(http2时代不需要考虑),几百个以上,确实会变慢(视情况处理,不要拆分过细),过多的拆分会导致打包速度过慢,但相比浏览器缓存机制就没有可比性了。
18. 打包时缓存
module.exports = {
cache: {
type: "filesystem",
buildDependencies: {
config: [__filename],
// 默认情况下 webpack 与 loader 是构建依赖。
},
},
},
优点:在二次打包中,未修改的文件会使用上一次的缓存。
缺点:如果构建时报错,可能会在再次打包时应用缓存,导致报错,在node_modules 删除 .cache 即可。
19. 多线程打包
pnpm add thread-loader -D
module.exports = {
module: {
rules: [
{
test: /\.m?jsx$/,
exclude: /node_modules/,
use: [
{
//在其他loader前使用这个loader
loader: 'thread-loader',
}, {
loader: 'babel-loader',
options: {
cacheDirectory: true,
}
}],
},],
},
},
优点:使用多线程构建加速。
缺点:开启多个线程会有大于600ms延迟,小于600ms的构建开启将毫无意义。
20. 使用cdn
module.exports = {
//key是排除的不需要打包的依赖包 值是CDN的全局导出名
externals: {
"lodash": "_",
"vue": "Vue",
"vue-router": "VueRouter"
}
};
<body>
<div id="app"></div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/3.3.4/vue.global.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/vue-router/4.0.3/vue-router.global.js"></script>
</html>
注意视实际需求使用。
21. 静态压缩文件
Nginx默认会开启Gzip,但为了避免Nginx压力过大可以使用静态压缩。
pnpm add compression-webpack-plugin -D
const CompressionPlugin = require("compression-webpack-plugin");
module.exports = {
plugins: [
new CompressionPlugin({
algorithm: "gzip",//压缩算法
test: /\.js$|\.css$|\.html$/,//匹配文件
threshold: 10240,//文件大小
deleteOriginalAssets: false,//是否删除源文件
})
],
};
有兴趣的可以安装我的脚手架进行安装这个项目,使用的是npm包管理器。
全局安装
npm install lj-generator-cli -g
lj-generator-cli create [projectName]