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'
相关推荐
天蓝色的鱼鱼21 小时前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
泯泷1 天前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
妙码生花1 天前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
泯泷1 天前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全
团团崽_七分甜1 天前
Spring Boot 核心知识点总结
前端
lichenyang4531 天前
从一个按钮开始,理解 ASCF 框架到底在做什么
前端
古夕1 天前
第三方 SSO 接入实践:redirect_uri 编码、回调一致性与跨项目联调
前端·vue.js
朦胧之1 天前
页面白屏卡住排查方法
前端·javascript
用户593608741401 天前
Playwright 黑魔法:用 ClipboardEvent 绕过 React 富文本编辑器
前端