ES 模块:将命名导出导入为模块?

Dar*_*Teo 6 javascript typescript ecmascript-6 es6-modules

函数.js

export function hello() {
  echo 'foo'
}

export function foo() {
  echo 'bar'
}
Run Code Online (Sandbox Code Playgroud)

索引.js

import * as Funcs from './funcs.js' // import module, does tree-shaking work?
import { hello } from './funcs.js' // optimise imports, potentially clashes with other imports
import { hello } as Funcs from './funcs.js' // what should make sense, but isn't supported syntax

// this should be straight forward... 
Funcs.hello()

// vs having some random function on top level scope
hello()

// and this shouldn't work if I didn't import it
Funcs.foo()
Run Code Online (Sandbox Code Playgroud)

这就是我的问题。如果我使用形式 1 与形式 2,这对树摇动有什么影响吗?形式 2 更适合表达性,但形式 1 是我可以将所有内容放入模块/命名空间的唯一方法。表 3 是我的建议,但也许我不知道有人还没有反对过为什么不应该支持这一点。

我不知道该去哪里建议,甚至不知道构建一个 babel 插件来做到这一点。

编辑:对于上下文,我正在使用一些较新的库(rxjs),它们不公开默认导出,并依赖开发人员加载他们需要的所有功能。所以我无法控制这些出口。

编辑:建议的解决方法是简单地创建一个全局导入文件,该文件导入所有全局所需的导入,并将其全部导出为模块,这就是我现在正在做的事情。

编辑:找到es-discuss。希望能够去那里进行讨论。

https://esdiscuss.org/topic/syntax-to-pick-named-exports-from-a-module

https://esdiscuss.org/topic/proposal-importing-selected-chucks-of-a-module-into-an-object

编辑:在这里找到最有启发性的讨论。

https://esdiscuss.org/topic/import-foo-bar-as-obj-from-module

Dar*_*Teo 5

事实证明,我并不是唯一一个有这种想法的人。该线程更详细地介绍了与此语法相关的一些潜在问题......我不一定同意,但事实就是如此。

https://esdiscuss.org/topic/import-foo-bar-as-obj-from-module