ES6: import many files

I have a script that imports many AMD modules and calls the initialization method for each of them:

define(['underscore', './mod0', ..., './modN'], function (_) { _.each(_.toArray(arguments).slice(1), function (m) { init(m); }); }); 

I need to switch to the ES6 import syntax, and I'm trying to figure out if it is possible to import modules from a list, similar to my AMD code. I want to avoid madness, for example:

 import mod0 from './mod0'; ... import modN from './modN'; init(mod0); ... init(modN); 

Any tips on how to do this? thanks!

+5
source share
1 answer

Can I import modules from a list?

No, without explicitly calling your module loader (depending on what it is). There is no way to do this using import declarations.

Any tips on how to do this?

eval could do this :-)

I would recommend using two modules:

 // index.js export mod0 from './mod0'; … export modN from './modN'; 

 // init-all.js import * as modules from './index'; // enumerable namespace for (var moduleIdentifier in modules) init(modules[moduleIdentifier]); 

You could do the same with just one module (which imports itself as an object of the module namespace), but that will certainly be real madness.

+5
source

Source: https://habr.com/ru/post/1243156/


All Articles