You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
5.0 KiB
JavaScript

2 years ago
// 引入一个包
const path = require("path");
// 引入HTML插件
const HTMLWebpackPlugin = require("html-webpack-plugin");
// 引入EsLint插件
const ESLintWebpackPlugin = require("eslint-webpack-plugin");
// 引入CSS单独打包插件
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// 引入CSS压缩打包插件
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
2 years ago
// webpack中的所有配置信息
module.exports = {
// 模式development开发、production生产
mode: "production",
2 years ago
// 指定入口文件
entry: "./src/main.ts",
2 years ago
// 指定打包文件所在目录
output: {
// 指定打包文件的目录
path: path.resolve(__dirname, "../dist"),
// 打包后的入口脚本文件名动态设置文件名并增加文件内容hash值的后缀
filename: "static/js/[name].[contenthash:10].js",
// 自动清空上一次打包的内容webpack4需使用扩展包clean-webpack-plugin插件来进行自动清空操作
clean: true,
2 years ago
// 不使用箭头函数的方式定义
environment: {
arrowFunction: false,
const: false
}
},
// 指定webpack打包时要使用的模块
module: {
// 指定要加载的规则
rules: [
{
// 使用oneOf去优化打包速度当文件对应的处理loader被检测到就不用再去遍历其他的loader提升打包速度
oneOf: [
2 years ago
{
// 指定规则生效的文件
test: /\.ts$/,
// 要使用的loader
use: [
// 配置babel
{
// 指定加载器
loader: "babel-loader",
// 设置babel
options: {
// 开启babel缓存
cacheDirectory: true,
// 关闭缓存文件压缩
cacheCompression: false,
// 减少代码体积
plugins: ["@babel/plugin-transform-runtime"],
}
},
"ts-loader"
],
// 要排除的文件
exclude: /node_modules/
2 years ago
},
// 设置less文件的处理
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
// 引入postcss
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
"postcss-preset-env"
]
}
}
},
"less-loader"
]
}
2 years ago
]
}
]
},
// 配置webpack插件
plugins: [
new HTMLWebpackPlugin({
template: "./public/index.html"
}),
new ESLintWebpackPlugin({
// 指定检查文件的根目录
context: path.resolve(__dirname, "../src"),
// 排除检查目录
exclude: "node_modules",
// 开启缓存
cache: true,
// 缓存输出文件路径
cacheLocation: path.resolve(__dirname, "../node_modules/.cache/eslintcache")
}),
// 提取css成单独文件
new MiniCssExtractPlugin({
// 定义输出文件名和目录动态设置文件名并增加文件内容hash值的后缀
filename: "static/css/[name].[contenthash:10].css"
})
2 years ago
],
// 配置压缩插件webpack5官方建议使用这种方式
optimization: {
minimizer: [
// css压缩
new CssMinimizerPlugin()
],
// 代码分割配置单入口时默认配置会将代码中引用node_modules包中的功能时单独打包及import动态引入的功能进行单独打包
// 本项目中因使用了any-touch则会将这部分功能单独打包成js进行引入
splitChunks: {
chunks: "all"
},
// 将代码依赖单独记录到runtime文件中保证依赖文件变更时只改变它本身的hash和runtime的hash而不改变依赖者的hash
runtimeChunk: {
name: (entrypoint) => `runtime~${entrypoint.name}.js`
}
},
2 years ago
// 设置引用模块
resolve: {
extensions: [".ts", ".js"]
}
};