Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deconstruct an ES6 module joint export

I've run into a little hiccup with an export scenario and I am not sure why. I may need a Babel plugin to address it but not sure which.

// a.js
export function fromA() {}

// b.js
export function fromB() {}

// index.js
import * as a from './a'
import * as b from './b'

export default { ...a, ...b}

// test.js
import all from './index'  
const { fromA } = all // Works

import { fromA } from './index'  // Does not work. Why?

I am running through Babel. Here's my rc:

{
  "plugins":  [
    "transform-object-rest-spread", 
    "transform-class-properties", 
    "transform-export-extensions", 
    "transform-decorators-legacy"
   ], 
  "presets":  ["latest", "react"]
}

It seems that I should be able to destucture in test.js within the import statement as usual but no. If, in index.js, I export individual functions then it woks. As in:

import { fromA } from './a'
import { fromB } from './b'
export default { fromA, fromB }

However I'd like to avoid that.

like image 442
cyberwombat Avatar asked Nov 13 '16 02:11

cyberwombat


People also ask

Is module exports ES6?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

What is the equivalent of module exports in ES6?

All CommonJS and AMD modules are presented to ES6 as having a default export, which is the same thing that you would get if you asked require() for that module—that is, the exports object. ES6 modules were designed to let you export multiple things, but for existing CommonJS modules, the default export is all you get.

What is CommonJS vs ES6?

ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse.

Can a module have more than one export?

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export. Each type corresponds to one of the above syntax.


1 Answers

Though import syntax looks like deconstruction, it's not.

When you're exporting a named variable you can only import it as a named variable. And when you're exporting a default variable, you can only import it as a default one.

For example:

// a.js
export const foo = 1
export const bar = 2
export default { bar: 42, baz: 33 }
import { foo } from './a'
// foo = 1
import { bar } from './a'
// bar = 2
import a from './a'
// a = { bar: 42, baz: 33 }

The only exception is when you're importing a non-es6 module. Since commonjs modules can only export one variable per module, babel falls back to deconstruction importing them.

So, since you're exporting a single object from your index.js, so you can only import it as the whole object.


Probably what you're looking for is an export * from statement:

export * from './a'
export * from './b'

It will re-export all named exports from both modules.

like image 114
Leonid Beschastny Avatar answered Oct 07 '22 06:10

Leonid Beschastny