ES6: import many files

前端 未结 1 1285
予麋鹿
予麋鹿 2020-12-21 02:33

I have a script that imports a lot of AMD modules and calls an initialization method on each one:

define([\'underscore\', \'./mod0\', ..., \'./modN\'], funct         


        
相关标签:
1条回答
  • 2020-12-21 03:02

    Is it possible to import modules from a list?

    No, not without explicitly invoking your module loader (whichever that is). There is no way to do this using import declarations.

    Any advice on how this can be accomplished?

    eval could probably do it :-)

    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 potentially do the same with only a single module (that imports itself as a module namespace object), but that surely would be real insanity.

    0 讨论(0)
提交回复
热议问题