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

02.webpack中多文件打包

1.module,chunk,bundle的区别

  • moudle - 各个源码文件,webpack中一切皆是模块
  • chunk - 多模块合并成的,如entry, import(), splitChunk
  • bundle - 最终的输出文件

2.多文件打包配置

2.1 webpack.common.js

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { srcPath, distPath } = require('./paths')module.exports = {entry: {index: path.join(srcPath, 'index.js'),other: path.join(srcPath, 'other.js')},module: {rules: [// ----- 同上文 ----]},plugins: [// 多入口 - 生成 index.htmlnew HtmlWebpackPlugin({template: path.join(srcPath, 'index.html'),filename: 'index.html',// chunks 表示该页面要引用哪些 chunk (即上面的 index 和 other),默认全部引用// chunks: ['index']  // 只引用 index.js}),// 多入口 - 生成 other.htmlnew HtmlWebpackPlugin({template: path.join(srcPath, 'other.html'),filename: 'other.html',// chunks: ['other']  // 只引用 other.js})]
}
  • 上面的chunks配置,如果不配置chunks,那么打包出来的结果是默认引入全部js
<body><p>webpack demo</p><input type="text"/><script type="text/javascript" src="index.js"></script><script type="text/javascript" src="other.js"></script>
</body>
  • 如果配置了chunks,那么就只引入对应的结果
<body><p>webpack demo</p><input type="text"/><script type="text/javascript" src="index.js"></script>
</body>
2.2 webpack.prod.js
const path = require('path')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const webpackCommonConf = require('./webpack.common.js')
const { smart } = require('webpack-merge')
const { srcPath, distPath } = require('./paths')module.exports = smart(webpackCommonConf, {mode: 'production',output: {// filename: 'bundle.[contentHash:8].js',  // 打包代码时,加上 hash 戳filename: '[name].[contentHash:8].js', // name 即多入口时 entry 的 keypath: distPath,// publicPath: 'http://cdn.abc.com'  // 修改所有静态文件 url 的前缀(如 cdn 域名),这里暂时用不到},module: {rules: [//代码重复]},plugins: [new CleanWebpackPlugin(), // 会默认清空 output.path 文件夹new webpack.DefinePlugin({// window.ENV = 'production'ENV: JSON.stringify('production')})]
})
  • 多入口时,output出口的【name】变量会对应到入口的变量名
http://www.lryc.cn/news/234269.html

相关文章:

  • IEEE Standard for SystemVerilog Chapter 22. Compiler directives
  • 机器学习中的独立和同分布 (IID):假设和影响
  • PTP软硬件时间戳
  • 使用ADS进行serdes仿真时,Tx_Diff中EQ的设置对发送端波形的影响。
  • 数据库迁移(DBeaver版本)
  • 【c++STL常见排序算法sort,merge,random_shuffle,reverse】
  • STM32/N32G455国民科技芯片驱动DS1302时钟---笔记
  • 基于PLC的污水厌氧处理控制系统(论文+源码)
  • Unity之NetCode多人网络游戏联机对战教程(9)--NetworkAnimator组件
  • iceoryx之Roudi
  • .Net(C#)常用转换byte转uint32、byte转float等
  • windows快捷方式图标变成空白
  • 【Linux系统编程十九】:(进程通信)--匿名管道/模拟实现进程池
  • 【全网首发】2023年NOIP真题
  • 【Linux网络】从原理到实操,感受PXE无人值守自动化高效批量网络安装系统
  • Pandas+Matplotlib 数据分析
  • k8s ingress高级用法一
  • C语言--从键盘输入10个数字放在数组中,并输出
  • SSL加密
  • 一个美观且功能丰富的 .NET 控制台应用程序开源库
  • DispatcherSynchronizationContext and Dispatcher
  • java类型属性set方法无法被赋值
  • 【2】SM2验签工具和RSA验签工具
  • Python (十一) 迭代器与生成器
  • 通过maven命令手动上传jar私服Nexus
  • 记一次用jlink调试正常,不进入调试就不能运行的情况
  • 搞科研、写论文,如何正确使用GPT?AIGC技术解析、提示词工程高级技巧、AI绘图、ChatGPT/GPT4应用
  • Java实现的插件化策略模式
  • 【jvm】MinorGC、MajorGC和FullGC
  • Redis:java和SpringBoot中使用Redis