Last active
June 9, 2017 21:00
-
-
Save Restuta/73d54ead9c27239351c2 to your computer and use it in GitHub Desktop.
Babel Interop Defaults might lead to confusion with ES6 Modules and imports/exports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//define module-a.js | |
export default { x: 8 } | |
//then in module-b.js | |
import {x} from './module-a'; | |
console.log(x); | |
// ^^^ outputs 8 with default Babel settings while according to ES6 spec should output "undefined" | |
//this is explained by the following quote: | |
/* In order to encourage the use of CommonJS and ES6 modules, when exporting a default export with no other exports | |
module.exports will be set in addition to exports["default"]. | |
Source: http://babeljs.io/docs/usage/modules/ (see Interop section) | |
*/ | |
//to achieve the same with vanilla ES6 modules you would have to do | |
export const x = 8; | |
export default { x }; // not required, but if you want defaults to be supported you would have to export twice | |
//this might not be obvious and lead to some WTF's when you will be switching to strict mode after Interop freindly. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment