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

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

    Great gugly muglys! This was harder than it needed to be.

    Export one flat default

    This is a great opportunity to use spread (... in { ...Matters, ...Contacts } below:

    // imports/collections/Matters.js
    export default {           // default export
      hello: 'World',
      something: 'important',
    };
    
    // imports/collections/Contacts.js
    export default {           // default export
      hello: 'Moon',
      email: 'hello@example.com',
    };
    
    // imports/collections/index.js
    import Matters from './Matters';      // import default export as var 'Matters'
    import Contacts from './Contacts';
    
    export default {  // default export
      ...Matters,     // spread Matters, overwriting previous properties
      ...Contacts,    // spread Contacts, overwriting previosu properties
    };
    
    
    // imports/test.js
    import collections from './collections';  // import default export as 'collections'
    
    console.log(collections);
    

    Then, to run babel compiled code from the command line (from project root /):

    $ npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node 
    (trimmed)
    
    $ npx babel-node --presets @babel/preset-env imports/test.js 
    { hello: 'Moon',
      something: 'important',
      email: 'hello@example.com' }
    

    Export one tree-like default

    If you'd prefer to not overwrite properties, change:

    // imports/collections/index.js
    import Matters from './Matters';     // import default as 'Matters'
    import Contacts from './Contacts';
    
    export default {   // export default
      Matters,
      Contacts,
    };
    

    And the output will be:

    $ npx babel-node --presets @babel/preset-env imports/test.js
    { Matters: { hello: 'World', something: 'important' },
      Contacts: { hello: 'Moon', email: 'hello@example.com' } }
    

    Export multiple named exports w/ no default

    If you're dedicated to DRY, the syntax on the imports changes as well:

    // imports/collections/index.js
    
    // export default as named export 'Matters'
    export { default as Matters } from './Matters';  
    export { default as Contacts } from './Contacts'; 
    

    This creates 2 named exports w/ no default export. Then change:

    // imports/test.js
    import { Matters, Contacts } from './collections';
    
    console.log(Matters, Contacts);
    

    And the output:

    $ npx babel-node --presets @babel/preset-env imports/test.js
    { hello: 'World', something: 'important' } { hello: 'Moon', email: 'hello@example.com' }
    

    Import all named exports

    // imports/collections/index.js
    
    // export default as named export 'Matters'
    export { default as Matters } from './Matters';
    export { default as Contacts } from './Contacts';
    
    // imports/test.js
    
    // Import all named exports as 'collections'
    import * as collections from './collections';
    
    console.log(collections);  // interesting output
    console.log(collections.Matters, collections.Contacts);
    

    Notice the destructuring import { Matters, Contacts } from './collections'; in the previous example.

    $ npx babel-node --presets @babel/preset-env imports/test.js
    { Matters: [Getter], Contacts: [Getter] }
    { hello: 'World', something: 'important' } { hello: 'Moon', email: 'hello@example.com' }
    

    In practice

    Given these source files:

    /myLib/thingA.js
    /myLib/thingB.js
    /myLib/thingC.js
    

    Creating a /myLib/index.js to bundle up all the files defeats the purpose of import/export. It would be easier to make everything global in the first place, than to make everything global via import/export via index.js "wrapper files".

    If you want a particular file, import thingA from './myLib/thingA'; in your own projects.

    Creating a "wrapper file" with exports for the module only makes sense if you're packaging for npm or on a multi-year multi-team project.

    Made it this far? See the docs for more details.

    Also, yay for Stackoverflow finally supporting three `s as code fence markup.

提交回复
热议问题