javascript - 重新导出模块对 tree-shaking 有害吗?

标签 javascript webpack ecmascript-6 es6-modules tree-shaking

我和我的同事发生了争执,我们似乎无法从任何官方来源(MDN、webpack 文档等)找到答案。我的研究hasn't yielded much任何一个。好像有疑问even when it comes to importing

我们的设置是 Webpack、Babel 和典型的 React/Redux 应用程序。举个例子:

export * from './actions';
export * from './selectors';
export * from './reducer';

export { default } from './reducer';

这允许我将 Redux 模块分成逻辑部分,使代码更易于阅读和维护。

然而,我的一些同事认为 export * from 实际上可能会通过欺骗 webpack 来损害它的 tree-shaking 能力相信导出实际上只是被重新导出。

所以我的问题是,是否有任何事实证明或反驳这一点?

最佳答案

It is believed that export * from may harm webpack's tree-shaking capabilities, by tricking it into believing an export is used when it is in fact just being re-exported.

不,导入/导出声明除了为导出的变量设置别名外什么都不做,它们不算作“使用”。鉴于它们的语义,它们会被任何打包器专门跟踪,并且不会对 tree-shaking 产生不利影响。

即使是正确完成的重命名别名也不会破坏它:

export { X as Y } from '…';

import { X as Y } from '…';
export { Y }

import { X } from '…';
export { X as Y }

但语句中的使用将算作使用并中断(非复杂的)优化:

import { X } from '…';
export const Y = X; // avoid!

So my question is, are there any facts proving or disproving this?

您可以尝试一下,看看它是否有效。

关于javascript - 重新导出模块对 tree-shaking 有害吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54793402/

相关文章:

javascript - 如何按特定键在数组中按字母顺序对对象进行分组?

javascript - 对对象列表进行排序 javascript

javascript - 等待 async firebase foreach?

java - MongoDB 映射缩减,给出错误 "No converter found capable of converting from type java.lang.Boolean to type int"

reactjs - webpack:SASS 加载器失败 "Module build failed (from ./node_modules/sass-loader/lib/loader.js)"

node.js - React router 版本 4 没有显示任何内容

javascript - Ember CLI 将 ES6 文件导入 ember-cli-builds.js

javascript - jQuery 手机 : absolute external panel on right side not working

javascript - 使用对象作为 ES2015 映射键

javascript - sw-precache 离线模式不获取 js 或 css