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'
相关推荐
Java展博21 小时前
[前端够用就好] 跨域报错时,浏览器到底在拦谁
前端·状态模式
用户112936112191 天前
npm 都提示成功了,node_modules 为什么还占着 370M?
linux·前端·后端
掘金安东尼1 天前
Agent Harness 也有 benchmark:评分榜与评测工作台现状
前端·agent
digger_4191 天前
从零理解 Electron:主进程、渲染进程、preload 与 IPC 实战
前端·electron
去伪存真1 天前
别让你的 RAG 平台“花五份钱、答一份结巴的题”:幂等性架构的自我修养
前端·agent
snow@li1 天前
前端:导入EXCEL并解析文件内容,demo
前端·javascript·vue.js
补码缩写1 天前
压缩PNG不损失透明:只量化RGB、alpha原样留的减色流程
前端
TDengine (老段)1 天前
TDengine IDMP 知识库与知识图谱
大数据·前端·物联网·知识图谱·时序数据库·tdengine·涛思数据
goehou1 天前
PNG 转 WebP 不一定变小,我量了三类图
前端·性能优化·webp·sharp
夕水1 天前
一个小小的颜色选择器,差点让我开发出一个组件库来
前端·javascript·typescript