"import * as" 的结果是否可以在 ES2015 中导出?
Is it possible to export the result of "import * as" in ES2015?
在 ES2015 中,可以将整个模块作为对象导入,其属性是模块的导出:
import * as name from 'module';
我发现这对命名空间非常有用,并且一直在使用它。
也可以重新导出其他模块的导出:
export { name } from 'module'; // selectively
export * from 'other-module'; // indiscriminately
现在我正在尝试用这种风格编写一个带有命名空间的库。在顶层模块中收集所有内容的直观方式是这样的:
export * as name from 'module';
但这似乎行不通; Babel 和 Rollup 都拒绝它。
我可以将模块作为对象导入,通过遍历它的键创建一个克隆,然后导出它,但那样它就只是一个普通的旧动态对象,所以我会失去 Rollup 提供的巨大优势。
那么,真的没有办法用声明性模块语法来做到这一点吗?在我看来,这没有任何借口。
不,这只是在 ES6 中遗漏了。还有is a stage 1 proposal to add these, though, and rollup will consider implementing it.
到那时,您将需要使用两个声明和一个本地绑定,尽管不需要克隆对象:
import * as name from 'module';
export { name };
在 ES2015 中,可以将整个模块作为对象导入,其属性是模块的导出:
import * as name from 'module';
我发现这对命名空间非常有用,并且一直在使用它。
也可以重新导出其他模块的导出:
export { name } from 'module'; // selectively
export * from 'other-module'; // indiscriminately
现在我正在尝试用这种风格编写一个带有命名空间的库。在顶层模块中收集所有内容的直观方式是这样的:
export * as name from 'module';
但这似乎行不通; Babel 和 Rollup 都拒绝它。
我可以将模块作为对象导入,通过遍历它的键创建一个克隆,然后导出它,但那样它就只是一个普通的旧动态对象,所以我会失去 Rollup 提供的巨大优势。
那么,真的没有办法用声明性模块语法来做到这一点吗?在我看来,这没有任何借口。
不,这只是在 ES6 中遗漏了。还有is a stage 1 proposal to add these, though, and rollup will consider implementing it.
到那时,您将需要使用两个声明和一个本地绑定,尽管不需要克隆对象:
import * as name from 'module';
export { name };