douglasduteil / isparta

:skull: A code coverage tool for ES6 (babel/6to5)
Do What The F*ck You Want To Public License
642 stars 47 forks source link

Breaking destructuring `import` from `export default` #98

Closed dtothefp closed 8 years ago

dtothefp commented 8 years ago

If I have the following structure

mocks
├── first-compiled.js
├── first.js
├── second-compiled.js
└── second.js

And I compile first.js and second.js with isparta instrumenter API

//isparta-config.js
const instrumenter = new isparta.Instrumenter({
  embedSource: true,
  noAutoWrap: true,
  babel: {
    presets: ['es2015', 'stage-0', 'react'],
    plugins: ['transform-decorators-legacy']
  }
});
//first.js
import all, {stuff, things} from './second-compiled';

console.log('**DEFAULT**', all);
console.log('***DESTRUCTURED***', stuff, things);

//second.js
export default {
  stuff: 'stuff',
  things: 'things'
}
$ babel-node mocks/first.js
**DEFAULT** { stuff: 'stuff', things: 'things' }
***DESTRUCTURED*** undefined undefined

Attempting to destructure from the export default statement will result in undefined values. All works as expected if I run the code without compiling with isparta (notice I'm importing second.js not second-compiled.js:

import all, {stuff, things} from './second';
import {reactor} from '../lib/modules/bootstrap.js';

console.log('**DEFAULT**', all);
console.log('***DESTRUCTURED***', stuff, things);
$ babel-node mocks/first.js
**DEFAULT** { stuff: 'stuff', things: 'things' }
***DESTRUCTURED*** stuff things

Curious if this is a bug or if Babel is letting me get away with stuff I shouldn't be able to? I noticed if I export something that is not default out of the file I will get similar output to isparta when I just run it with babel

export const whatevs = 'whatevs'

export default {
  stuff: 'stuff',
  things: 'things'
}
babel-node mocks/first.js
**DEFAULT** { stuff: 'stuff', things: 'things' }
***DESTRUCTURED*** undefined undefined

This is a problem because I have a big application that I am doing various destructuring off of export default and it chokes when I'm running it with isparta/karma/webpack

UPDATE

NVM, I believe my problem is that I'm still on Babel 5 for my project (because it is using babel plugins that have not yet been updated), but was using Isparta with Babel 6. It seems that Babel 6 is enforcing this syntax more strongly now http://stackoverflow.com/questions/33524696/es6-destructuring-and-module-imports.

Hopefully, to save others time in the future you can use this plugin to restore the old behavior https://www.npmjs.com/package/babel-plugin-add-module-exports http://stackoverflow.com/questions/33505992/babel-6-changes-how-it-exports-default