小编Pan*_*tou的帖子

从具有其他导入的文件导入时,Webpack Tree Shaking 不起作用

我创建了这个例子

模块.js

import moment from "moment";

export function square(x) {
    return x * x;
}

export function cube(x) {
    return moment.format(x * x * x);
}
Run Code Online (Sandbox Code Playgroud)

main.js

import {square} from "./module";

console.log(square(1));
Run Code Online (Sandbox Code Playgroud)

我注意到它还在我的包中包含了 moment 库,尽管我没有在 square 函数中使用它。如果我从 module.js 中删除 moment 导入,树摇动效果很好,我只在我的包中看到 square 。

这就是摇树的工作原理吗?如果我想在 module.js 文件中使用外部库,除了将代码拆分到不同的文件中之外,还有其他解决方法吗?

javascript webpack tree-shaking

9
推荐指数
1
解决办法
5278
查看次数

标签 统计

javascript ×1

tree-shaking ×1

webpack ×1