当前位置: 首页 > news >正文

Webpack5 新特性与详细配置指南

一、Webpack5 新特性

内置 Asset Modules(资源模块)

  • 替代 file-loader、url-loader、raw-loader 等,统一资源处理方式。
  • 四种类型:
  • asset/resource:导出文件 URL(等同 file-loader)。
  • asset/inline:导出 DataURL(等同 url-loader)。
  • asset/source:导出源码(等同 raw-loader)。
  • asset:自动选择 resource 或 inline(默认 8KB 分界)。

持久化缓存(Persistent Caching)

  • 新增 cache: { type: 'filesystem' },极大提升二次构建速度。

Tree Shaking 更彻底

  • 默认生产模式下更智能地移除无用代码。

Module Federation(模块联邦)

  • 支持多个独立构建的应用共享模块,实现微前端架构。

更快的构建性能

  • 优化了编译流程,支持多进程/多实例(如 thread-loader)。

移除 Node.js Polyfills

  • 不再自动为 Node.js 内置模块做 polyfill,需手动引入。

更智能的默认配置

  • 如 production/development 模式下的优化、缓存、分包等。

更好的资源指纹

  • 支持 chunkhashcontenthashhash,便于缓存管理。

二、详细配置

基础配置

// webpack.config.js
const path = require("path");module.exports = {mode: "production", // 或 'development'entry: {index: "./src/index.js",},output: {path: path.join(__dirname, "dist"),filename: "[name].js",},
};

资源模块(Asset Modules)

module.exports = {module: {rules: [{test: /\.(jpg|jpeg|png|gif|svg)$/i,type: "asset",generator: {filename: "images/[hash][ext][query]",},},{test: /\.(ttf|eot|woff|woff2|otf)$/i,type: "asset/resource",generator: {filename: "fonts/[hash][ext][query]",},},{test: /\.txt$/,type: "asset",generator: {filename: "files/[hash][ext][query]",},parser: {dataUrlCondition: {maxSize: 4 * 1024, // 4kb},},},],},
};

CSS/LESS/PostCSS 配置

const MiniCssExtractPlugin = require("mini-css-extract-plugin");module.exports = {module: {rules: [{test: /\.css$/,use: [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader"],},{test: /\.less$/,use: [MiniCssExtractPlugin.loader,"css-loader","postcss-loader","less-loader",],},],},plugins: [new MiniCssExtractPlugin({filename: "[name]_[contenthash:8].css",}),],
};
PostCSS 配置(postcss.config.js)
module.exports = {plugins: [require("autoprefixer"),// 可选:cssnext、purgecss 等],
};

代码分割与动态 import

// 安装插件
// npm i -D @babel/plugin-syntax-dynamic-import// .babelrc
{"plugins": ["@babel/plugin-syntax-dynamic-import"]
}

动态 import 示例:

methods: {dynamicImportFn() {import('./Dynamic.vue').then(component => {this.dynamicComponent = component.default;});}
}

缓存与构建优化

module.exports = {cache: {type: "filesystem",buildDependencies: {config: [__filename],},version: "1.0",},
};

多进程/多实例

module.exports = {module: {rules: [{test: /\.js$/,use: [{loader: "thread-loader",options: { workers: 2 },},"babel-loader",],},],},
};

体积分析与速度分析

const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
const BundleAnalyzerPlugin =require("webpack-bundle-analyzer").BundleAnalyzerPlugin;const smp = new SpeedMeasurePlugin();module.exports = smp.wrap({// ...webpack 配置plugins: [new BundleAnalyzerPlugin()],
});

生产环境优化

  • JS 压缩:内置 TerserPlugin
  • CSS 压缩:css-minimizer-webpack-plugin
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");module.exports = {optimization: {minimize: true,minimizer: [new CssMinimizerPlugin({ parallel: true })],},
};

DLL 预编译

// webpack.dll.js
const path = require("path");
const webpack = require("webpack");module.exports = {mode: "production",entry: ["vue"],output: {path: path.resolve(process.cwd(), "dll"),filename: "vendor.js",library: "dllExample",},plugins: [new webpack.DllPlugin({name: "dllExample",path: path.resolve(process.cwd(), "dll/manifest.json"),}),],
};

三、使用示例

资源引用

  • 图片、字体、文本等资源直接 import 即可,Webpack5 自动处理。

代码分割

  • 使用 import() 实现懒加载和动态组件。

性能分析

  • 运行 npm run build 后自动弹出体积分析报告。

参考资料

  • Webpack 官方文档
  • Webpack5 迁移指南

 Webpack5 新特性与详细配置指南 - 高质量源码分享平台-免费下载各类网站源码与模板及前沿技术分享

http://www.lryc.cn/news/591067.html

相关文章:

  • 基于LSTM的机场天气分析及模型预测
  • Python eval函数详解 - 用法、风险与安全替代方案
  • Go语言学习日志(一)
  • Python应用进阶DAY7--面向对象编程基本特性和super函数
  • 电子电路中的电压符号命名约定
  • FreeSWITCH配置文件解析(6) mod_format_cdr 话单中字段解析
  • 浅谈自动化设计最常用的三款软件catia,eplan,autocad
  • 云服务器如何设置防火墙和安全组规则?
  • Linux内核网络栈深度剖析:inet_connection_sock.c的服务器端套接字管理
  • 【算法训练营Day13】二叉树part3
  • 华为P30/pro (ELE-AL00) 鸿蒙4.2降级 EMUI 9
  • 服务器数据恢复—raid5磁盘阵列崩溃如何恢复数据?
  • 集群聊天服务器各个类进行详解
  • Cookie 与 Session概述
  • 【神经网络在MATLAB中是如何实现的?】
  • 构建可扩展的测试体系,从设计、优化到持续维护
  • 2D视觉系统标定流程与关键要求
  • VSCODE调教
  • 《前端基础核心知识笔记:HTML、CSS、JavaScript 及 BOM/DOM》
  • yolov8-pos/yolov11-pos 训练
  • 6、docker network
  • UE5 lumen
  • Linux搭建LAMP环境(CentOS 7 与 Ubuntu 双系统教程)
  • FastAdmin系统框架通用操作平滑迁移到新服务器的详细步骤-优雅草卓伊凡
  • lua(xlua)基础知识点记录二
  • STM32上移植Lua解析器
  • Android15系统实现刷机防呆功能
  • 【JVM】深入理解 JVM 类加载器
  • MySQL如何解决事务并发的幻读问题
  • JVM 内存分配与垃圾回收策略