我创建了这个例子
模块.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 文件中使用外部库,除了将代码拆分到不同的文件中之外,还有其他解决方法吗?