javascript - How to deconstruct an ES6 module joint export -
i've run little hiccup export scenario , not sure why. may need babel plugin address not sure which.
// a.js export function froma() {} // b.js export function fromb() {} // index.js import * './a' import * b './b' export default { ...a, ...b} // test.js import './index' const { froma } = // works import { froma } './index' // not work. why?
i running through babel. here's rc:
{ "plugins": [ "transform-object-rest-spread", "transform-class-properties", "transform-export-extensions", "transform-decorators-legacy" ], "presets": ["latest", "react"] }
it seems should able destucture in test.js within import statement usual no. if, in index.js, export individual functions woks. in:
import { froma } './a' import { fromb } './b' export default { froma, fromb }
however i'd avoid that.
though import syntax looks deconstruction, it's not.
when you're exporting named variable can import named variable. , when you're exporting default variable, can import default one.
for example:
// a.js export const foo = 1 export const bar = 2 export default { bar: 42, baz: 33 }
import { foo } './a' // foo = 1
import { bar } './a' // bar = 2
import './a' // = { bar: 42, baz: 33 }
the exception when you're importing non-es6 module. since commonjs modules can export 1 variable per module, babel falls deconstruction importing them.
so, since you're exporting single object index.js
, can import whole object.
probably you're looking export * from
statement:
export * './a' export * './b'
it re-export named exports both modules.
Comments
Post a Comment