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

常用的webpack配置

Webpack 常用配置详解

Webpack 是一个现代 JavaScript 应用程序的静态模块打包工具。以下是 Webpack 常用的配置项及其说明:

1. 入口配置 (entry)

module.exports = {entry: {// 单入口(SPA)main: './src/index.js',// 多入口(MPA)app: './src/app.js',admin: './src/admin.js'}
};

2. 输出配置 (output)

const path = require('path');module.exports = {output: {filename: '[name].[contenthash].js', // 使用入口名称和内容hashpath: path.resolve(__dirname, 'dist'),publicPath: '/', // 资源公共路径clean: true, // 清理输出目录 (Webpack 5+)chunkFilename: '[name].[contenthash].chunk.js' // 非入口chunk名称}
};

3. 模式配置 (mode)

module.exports = {mode: 'production', // 或 'development' 或 'none'
};

4. 模块配置 (module)

module.exports = {module: {rules: [{test: /\.jsx?$/,exclude: /node_modules/,use: {loader: 'babel-loader',options: {presets: ['@babel/preset-env', '@babel/preset-react']}}},{test: /\.css$/,use: ['style-loader', 'css-loader']},{test: /\.(png|jpe?g|gif|svg)$/,type: 'asset/resource' // Webpack 5 资源模块}]}
};

5. 插件配置 (plugins)

const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');module.exports = {plugins: [new CleanWebpackPlugin(),new HtmlWebpackPlugin({template: './public/index.html'}),new MiniCssExtractPlugin({filename: '[name].[contenthash].css'})]
};

6. 解析配置 (resolve)

module.exports = {resolve: {extensions: ['.js', '.jsx', '.json'],alias: {'@': path.resolve(__dirname, 'src/'),components: path.resolve(__dirname, 'src/components/')},modules: [path.resolve(__dirname, 'src'),'node_modules']}
};

7. 开发服务器配置 (devServer)

module.exports = {devServer: {static: {directory: path.join(__dirname, 'public'),},compress: true,port: 9000,hot: true,open: true,historyApiFallback: true, // 单页应用路由支持proxy: {'/api': {target: 'http://localhost:3000',changeOrigin: true}}}
};

8. 优化配置 (optimization)

module.exports = {optimization: {splitChunks: {chunks: 'all',cacheGroups: {vendors: {test: /[\\/]node_modules[\\/]/,name: 'vendors',chunks: 'all'},commons: {name: 'commons',minChunks: 2,chunks: 'initial',minSize: 0}}},runtimeChunk: 'single',minimize: true,minimizer: [new TerserPlugin(), // 压缩JSnew CssMinimizerPlugin() // 压缩CSS]}
};

9. 性能配置 (performance)

module.exports = {performance: {hints: 'warning', // 'error' 或 falsemaxEntrypointSize: 512000, // 入口点大小限制 (bytes)maxAssetSize: 512000 // 资源大小限制 (bytes)}
};

10. Source Map 配置 (devtool)

module.exports = {devtool: 'source-map', // 生产环境推荐// 或devtool: 'eval-cheap-module-source-map' // 开发环境推荐
};

11. 外部扩展 (externals)

module.exports = {externals: {jquery: 'jQuery',react: 'React','react-dom': 'ReactDOM'}
};

12. 缓存配置 (Webpack 5+)

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

13. 模块联邦 (Module Federation)

const { ModuleFederationPlugin } = require('webpack').container;module.exports = {plugins: [new ModuleFederationPlugin({name: 'app1',filename: 'remoteEntry.js',exposes: {'./Button': './src/Button'},remotes: {app2: 'app2@http://localhost:3002/remoteEntry.js'},shared: {react: { singleton: true },'react-dom': { singleton: true }}})]
};

14. 环境变量配置

const webpack = require('webpack');module.exports = {plugins: [new webpack.DefinePlugin({'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),'process.env.API_URL': JSON.stringify(process.env.API_URL)})]
};

以上是 Webpack 的常用配置项,实际项目中可以根据需求进行组合和调整。Webpack 5 引入了许多新特性如持久化缓存、资源模块等,可以显著提升构建性能。

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

相关文章:

  • 应用俄文OCR技术,为跨语言交流与数字化管理提供更强大的支持
  • 解数独(C++版本)
  • 关于Xinference 中部署服务不能成功的若干问题整理(持续迭代)
  • 安卓10.0系统修改定制化_____安卓9与安卓10系统文件差异 有关定制选项修改差异
  • NLP:文本特征处理和回译数据增强法
  • uniapp三步完成生成一维码图片
  • C#和SQL Server连接常用通讯方式
  • 基于4.14 kernel ARM V7 单核cpu swi功能的验证方法
  • kong网关基于header分流灰度发布
  • 揭秘图像LLM:从像素到语言的智能转换
  • ClickHouse 入门详解:它到底是什么、优缺点、和主流数据库对比、适合哪些场景?
  • 【K线训练软件研发历程】【日常记录向】1.K线滑动窗口
  • 【数据结构】第七弹——Priority Queue
  • Kafka 消费者组再平衡优化实践指南
  • 赛事开启|第三届视觉语音识别挑战赛 CNVSRC 2025 启动
  • RedisTemplate在Spring Boot中的五种数据结构全面详解
  • 电脑电压过高的影响与风险分析
  • 【Java安全】反射基础
  • ARMv7单核CPU上SWI(软件中断)验证
  • 前端面试专栏-算法篇:20. 贪心算法与动态规划入门
  • SQL Server表分区技术详解
  • 瑞斯拜考研词汇课笔记
  • 基于Java+SpringBoot 的销售项目流程化管理系统
  • 深度学习机器学习比较
  • 【ROS2 自动驾驶学习】02-安装ROS2及其配套工具
  • Java 内存分析工具 Arthas
  • 卷积神经网络:卷积层的核心原理与机制
  • MATLAB | 绘图复刻(二十一)| 扇形热图+小提琴图
  • Spring AOP 设计解密:代理对象生成、拦截器链调度与注解适配全流程源码解析
  • 网络安全之重放攻击:原理、危害与防御之道