Is it possible to import modules from all files in a directory, using a wildcard?

前端 未结 13 2192
陌清茗
陌清茗 2020-11-22 04:25

With ES6, I can import several exports from a file like this:

import {ThingA, ThingB, ThingC} from \'lib/things\';

However, I like the orga

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 04:34

    You can use async import():

    import fs = require('fs');
    

    and then:

    fs.readdir('./someDir', (err, files) => {
     files.forEach(file => {
      const module = import('./' + file).then(m =>
        m.callSomeMethod();
      );
      // or const module = await import('file')
      });
    });
    

提交回复
热议问题