How do I use Javascript ES6 ES2015 modules to export / import constants directly to the import module namespace?

Viewed 11068

OK, I can't quite find the answer to this. I am using webpack and babel es2015 preset to handle ES2015 modules.

Module 1 to export, filename "foobar.js"

export const FOO = 'foo'
export const BAR = 'bar'

Is there a way to import this constant into my global namespace in my import module?

I want to do this in my module that will use the constants:

import 'foobar'

const doSomething = () => { console.log(FOO + BAR) }

I know that this would work:

import * as CONSTANTS from 'foobar'

const doSomething = () => { console.log(CONSTANTS.FOO + CONSTANTS.BAR) }

...and that there are other ways to achieve the same result of the import being in a particular namespace. But I want to use the constants without a prefix.

Is there a way to directly import ALL the exports from a different module into the root namespace of the importing module?

*NOTE: I know that I can do this:

import {FOO, BAR} from 'foobar'

But then I have to explicitly reference each of the constants in the import, which leads to more headaches rather than less.

2 Answers
Related