2

Does anyone know of a better way to do this?

The goal: import, use, and export createLogger from the same file (application entry point).

WebStorm gives me a duplicate declaration warning.

import createLogger from './logger';

const logger = createLogger('namespace');

export { default as createLogger };
export { * as plugins } from './plugins'; 

export setup = () => {
  // ...
  logger.log('');
}

export start = async () => {
  // ... 
  logger.log('');
}
michaelitoh
  • 2,317
  • 14
  • 26
  • 1
    why do you want to import and export? you're not even modifying it – Abdul Ahmad Sep 07 '18 at 18:31
  • @Shane Exactly what are you trying to achieve? maybe there is another option for doing it because this seems weird... – c-chavez Sep 07 '18 at 18:32
  • you still don't need to export it, other places that need the `createLogger` can import it from the original logger file – Abdul Ahmad Sep 07 '18 at 18:38
  • 1
    Technically, yes, but this is for a library so we want to have everything importable from one statement. import { setup, start, createLogger } from 'library'; –  Sep 07 '18 at 18:41
  • I see, so you can just do `export createLogger` I'm guessing? why the default? if it's default, the import would be `import createLogger, { setup, etc.. } from library` Also, the syntax might be wrong, it may have to be `export { createLogger as default }` instead of the other way around - scratch that, the syntax is right – Abdul Ahmad Sep 07 '18 at 18:44
  • have a look at this: https://stackoverflow.com/questions/35665759/es6-how-can-you-export-an-imported-module-in-a-single-line looks like it's `export { default as createLogger } from '...';` or you might be able to just do `export default createLogger;` – Abdul Ahmad Sep 07 '18 at 18:45
  • @ShaneDaugherty Did any of the responses help you? if you need more information we are here to help! – c-chavez Sep 21 '18 at 20:43

1 Answers1

3

To export multiple functions from the same file just do this:

import createLogger from './logger';
const logger = createLogger('namespace');
import plugins from './plugins';
import anotherLib from './anotherLib';

const setup = () => {
    // ...
    logger.log('');
}

const start = async () => {
    // ...
    logger.log('');
}


// export everything without default
export { plugins, 
    createLogger, 
    anotherLib,
    setup,
    start}

You can import them in another file after this is done.

Here's a sandbox to see how it works.

Have a look at this documentation about the export statement.

c-chavez
  • 7,237
  • 5
  • 35
  • 49