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

How are non-ES6 node_modules imported? #16

Closed
ruswerner opened this issue Dec 17, 2014 · 6 comments
Closed

How are non-ES6 node_modules imported? #16

ruswerner opened this issue Dec 17, 2014 · 6 comments
Assignees
Labels

Comments

@ruswerner
Copy link

I would like to do this:

import React from 'react';

However it gets compiled down to:

var React = require('react')["default"];

...which of course is undefined since that node module doesn't export "default".

Is there a way to use the ES6 syntax and have the compiled output drop the ["default"]?

@sebmck
Copy link

sebmck commented Dec 17, 2014

6to5 has a commonInterop module formatter which in 2.0.0 is going to be the default. You can pass this option to 6to5 like so:

browserify().transform(to5ify.configure({ modules: "commonInterop" }));

or

$ browserify -t [ 6to5ify --modules commonInterop ]

@ruswerner
Copy link
Author

For those of you who find this later and want to know more... this is what I ended up with using Gulp:

...
var browserify = require('browserify');
var envify = require('envify');
var reactify = require('reactify');
var to5ify = require('6to5ify');
...

var bundler = browserify({
    basedir: __dirname,
    debug: (process.env.NODE_ENV === 'development'),
    entries: './src/client/client.js',
    cache: {},
    packageCache: {},
    fullPaths: watch
})
    .add(require.resolve("6to5/polyfill"))
    .transform(envify)
    .transform(to5ify.configure({
        modules: 'commonInterop'
    }))
    .transform(reactify);

@sebmck
Copy link

sebmck commented Dec 18, 2014

@ruswerner modules should be a string, not an array. I'm not even sure how that works if it somehow does.

@ruswerner
Copy link
Author

@sebmck It certainly does work as an array:

when using

    .transform(to5ify.configure({
    }))

this

import React from 'react';

becomes this

var React = require('react')["default"];

Whereas when using this:

    .transform(to5ify.configure({
        modules: ['commonInterop']
    }))

it becomes this

var _interopRequire = function (obj) {
  return obj && (obj["default"] || obj);
};

var React = _interopRequire(require('react'));

Did I find a bug?

@ruswerner
Copy link
Author

@sebmck OK. So I traced through the 6to5 code and you are correct in thinking how this could possibly work. It doesn't make sense, however in nodejs, this happens:

var hash = { commonInterop: 'yesplease' };
console.log(hash['commonInterop']); // prints yesplease
console.log(hash[['commonInterop']]); // also prints yesplease

I have been writing JavaScript for a long time and had no idea this works; it's either a typecasting bug, or something else about JS that makes no sense.

@sebmck
Copy link

sebmck commented Dec 18, 2014

Ah yeah, that'e make sense. It's type casting toString because only all
keys are strings.

On Thursday, 18 December 2014, ruswerner notifications@github.com wrote:

@sebmck https://github.com/sebmck OK. So I traced through the 6to5 code
and you are correct in thinking how this could possibly work. It doesn't
make sense, however in nodejs, this happens:

var hash = { commonInterop: 'yesplease' };
console.log(hash['commonInterop']); // prints yesplease
console.log(hash[['commonInterop']]); // also prints yesplease

I have been writing JavaScript for a long time and had no idea this works;
it's either a typecasting bug, or something else about JS that makes no
sense.

Reply to this email directly or view it on GitHub
#16 (comment).

Sebastian McKenzie

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

No branches or pull requests

2 participants