生成 es6 模块导出 [英] Generating es6 module exports

查看:32
本文介绍了生成 es6 模块导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式为模块生成导出,这在 es6 中可行吗?

I'm wanting to programmatically generate the exports for a module, is this possible in es6?

大致如下:

const ids = ['foo', 'bar', 'baz'];

ids.forEach(id => {
    export const [id.toUpperCase()] = id;
});

推荐答案

不,不是.导出和导入需要在 ES6 模块中静态分析.

No, it's not. Exports and imports are required to be statically analysable in ES6 modules.

非顶级 export 声明不仅是一个语法错误,而且是您尝试使用动态名称声明变量的尝试.括号表示法仅用于计算属性.

Not only is that no-top-level export declaration a syntax error, but also your attempt at declaring variables with dynamic names. The bracket notation is reserved for computed properties only.

因此,如果您要以编程方式生成模块导出,则需要动态生成模块源文本(作为构建过程的一部分).

So if you are going to programmatically generate module exports, you'll need to dynamically generate the module source text (as a part of your build process).

这篇关于生成 es6 模块导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆