Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to ES6 exports #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

markotom
Copy link

I would need to load module with:

export default function () {}

@slavafomin
Copy link

Hello @markotom, thank you for your effort!

However, what will happen if I have the below code in my module?

module.exports = {
  foo: 'foo',
  default: {
    foo: 'FOO'
  }
};

So, I'm exporting an object, which has default property. It looks like your implementation will break it and will return only a subset of the exported object. I think to handle it more safely, we will need to distinguish module definition objects and exported plain objects.

I'm not an expert at this matter, but according to this answer on SO babel will define the __esModule property on the exported module.

And here's how it checks for module definition:

obj && obj.__esModule

I think the more safe approach would be to use this code:

obj = obj && obj.__esModule && obj.default ? obj.default : obj;

It's based solely on the answer on SO. You will need to check this against various versions of Babel.

Also, are there other transpilers that we should be aware of? How TypeScript handles this?

@markotom
Copy link
Author

markotom commented Jul 2, 2017

I totally agree, __esModule would not be enough. We can use systemjs to accept not only babel, but other transpilers that use es6, commonjs, amd, etc.

@generalov
Copy link

A custom loader can be passed to the require-directory in the first argument.
For example ES6 modules loading using @std/esm:

cli.js

const loader = require('@std/esm')(module)
const requireDirectory = require('require-directory')
const mod = { filename: module.filename, require: loader }

requireDirectory(mod, './data', { extensions: ['mjs'] } )

data/hello.mjs

export const foo = 'bar'

Run the node cli.js:

{ hello: { foo: 'bar' } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants