Say I have a module (./my-module.js
) that has an object which should be its return value:
let values = { a: 1, b: 2, c: 3 }
// \"export values\
Every answer requires changing of the import statements.
If you want to be able to use:
import {a} from './my-module' // a === 1
import * as myModule from './my-module' // myModule.a === 1
as in the question, and in your my-module
you have everything that you need to export in one object (which can be useful e.g. if you want to validate the exported values with Joi or JSON Schema) then your my-module
would have to be either:
let values = { a: 1, b: 2, c: 3 }
let {a, b, c} = values;
export {a, b, c};
Or:
let values = { a: 1, b: 2, c: 3 }
export let {a, b, c} = values;
Not pretty, but it compiles to what you need.
See: Babel example
export const a = 1;
export const b = 2;
export const c = 3;
This will work w/ Babel transforms today and should take advantage of all the benefits of ES2016 modules whenever that feature actually lands in a browser.
You can also add export default {a, b, c};
which will allow you to import all the values as an object w/o the * as
, i.e. import myModule from 'my-module';
Sources:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
http://www.2ality.com/2014/09/es6-modules-final.html
Exporting each variable from your variables file. Then importing them with * as in your other file and exporting the as a constant from that file will give you a dynamic object with the named exports from the first file being attributes on the object exported from the second.
Variables.js
export const var1 = 'first';
export const var2 = 'second':
...
export const varN = 'nth';
Other.js
import * as vars from './Variables';
export const Variables = vars;
Third.js
import { Variables } from './Other';
Variables.var2 === 'second'