funcs.js
export function hello() {
echo 'foo'
}
export function foo() {
echo 'bar'
}index.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()这就是我的问题。如果我使用Form1和Form2,这对树摇动有什么不同吗?Form2在表现力方面更可取,但Form1是我将所有东西都放入模块/命名空间的唯一方法。形式3将是我的建议,但也许我不知道一些人没有争论过的东西,为什么不应该支持它。
我不知道到哪里去建议这个,甚至不知道构建一个babel插件来做这件事。
编辑:对于上下文,我正在使用一些较新的库(rxjs),这些库不公开默认导出,并且依赖开发人员加载他们需要的所有函数。所以我无法控制这些出口。
编辑:建议的一个解决方法是创建一个全局导入文件,该文件导入所有全局需要的导入,并将其作为一个模块导出,所以这就是我现在要做的。
编辑:找到es-讨论。将会去那里,希望能继续讨论。
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
发布于 2019-05-10 11:24:56
事实证明,我不是唯一有这种想法的人。这个帖子更详细地介绍了与此语法相关的一些潜在问题...我不一定同意,但事实就是这样。
https://esdiscuss.org/topic/import-foo-bar-as-obj-from-module
https://stackoverflow.com/questions/56014550
复制相似问题