webpack使用tree shaking示例:

在webpack5中,打包模式production时才开启

javascript 复制代码
module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'main.js',
    },
    mode: 'production',
}

webpack在打包时,tree shaking会自动运行,webpack会解析代码 构建依赖图谱 标记未使用的模块和代码,并在最终打包结果中移除未使用的代码。

javascript 复制代码
webpack.config.js
module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'main.js',
    },
    mode: 'production', // 或者 'production'
}
javascript 复制代码
src/index.js
import { ttt } from './util.js'

function testfn() {
    return 'test'
}

let a = 1
a+=1

src/util.js
export const ttt = '123ttt'
相关推荐
光影少年2 分钟前
高级前端需要学习那些东西?
前端·人工智能·学习·aigc·ai编程
jiayong235 分钟前
第 41 课:任务详情抽屉里的快速筛选联动
开发语言·前端·javascript·vue.js·学习
momo(激进版)5 分钟前
常用的skills安装记录
前端
zimoyin9 分钟前
Stoplight Elements WebComponents 原生 WEB 组件化技术生成 Swagger优美界面
前端
zhensherlock19 分钟前
Protocol Launcher 系列:Working Copy 提交与同步全攻略
javascript·git·typescript·node.js·自动化·github·js
天若有情67319 分钟前
【开源推荐】form-validator-cn 轻量级中文表单校验库 | TS 零依赖、极简开箱即用
前端·npm·开源·node·js·表单校验
shjita36 分钟前
maven涉及的配置
java·前端·maven
changshuaihua00142 分钟前
useState 状态管理
开发语言·前端·javascript·react.js
鹏程十八少1 小时前
6. 2026金三银四 面试官最爱的 Binder:一次拷贝、Activity 启动流程,这篇全搞定
前端·面试·前端框架
知识分享小能手1 小时前
ECharts入门学习教程,从入门到精通,综合实战——ECharts数据大屏 - 完整知识点(9)
前端·学习·echarts