Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating es6 module exports

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

Something along these lines:

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

ids.forEach(id => {
    export const [id.toUpperCase()] = id;
});
like image 275
Tom Avatar asked Apr 22 '26 10:04

Tom


1 Answers

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

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).

like image 137
Bergi Avatar answered Apr 25 '26 00:04

Bergi