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

前端 未结 13 2065
陌清茗
陌清茗 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:48

    I've used them a few times (in particular for building massive objects splitting the data over many files (e.g. AST nodes)), in order to build them I made a tiny script (which I've just added to npm so everyone else can use it).

    Usage (currently you'll need to use babel to use the export file):

    $ npm install -g folder-module
    $ folder-module my-cool-module/
    

    Generates a file containing:

    export {default as foo} from "./module/foo.js"
    export {default as default} from "./module/default.js"
    export {default as bar} from "./module/bar.js"
    ...etc
    

    Then you can just consume the file:

    import * as myCoolModule from "my-cool-module.js"
    myCoolModule.foo()
    

提交回复
热议问题