The historical claim, that default exports we're introduced for compatibility with CommonJS/Node, is not accurate. Default exports were introduced because often a module wants to export a single value or piece of functionality. In such cases, in module systems where all exports are named, the author of the module and its consumer have to coordinate on some convention to indicate "this is the thing". For example, some…
Why we have banned default exports in JavaScript
71–76 of 76 posts
Re: Why we have banned default exports in JavaScript
#72Two of the three reasons listed involve refactoring and autocomplete quirks of Visual Studio Code, specifically. For instance, Webstorm has no such issues with default exports. It is smart enough to find all uses of a module and to track them down. The third reason is not all that common in my experience. Tree shaking is an optimization and often a premature one. It's also a technique primarily used for third party l…
In webstorm/intellij say you have a react component, one export per module as you say. The import statements are a tie imo to write by hand: import MyThing from "../../components/editors/MyThing"; import {MyThing} from "../../components/editors/MyThing"; But if it's named, you don't need to stop what you're doing, scroll to the top of the file, type out the import statement by hand, and look over at your code tree an…
Re: Why we have banned default exports in JavaScript
#73export default { foo: 2 };
export const foo = 3;
// other file
import { foo } from './file'
console.log(foo);
Presumably last one wins or, if the other way around, a reassignment of const error?
Re: Why we have banned default exports in JavaScript
#74While reading this thread away from my machine, I'm curious now what happens when export default { foo: 2 }; export const foo = 3; // other file import { foo } from './file' console.log(foo); Presumably last one wins or, if the other way around, a reassignment of const error?
Re: Why we have banned default exports in JavaScript
#75While reading this thread away from my machine, I'm curious now what happens when export default { foo: 2 }; export const foo = 3; // other file import { foo } from './file' console.log(foo); Presumably last one wins or, if the other way around, a reassignment of const error?
3 is logged into the console without any warning / error.
Re: Why we have banned default exports in JavaScript
#76While reading this thread away from my machine, I'm curious now what happens when export default { foo: 2 }; export const foo = 3; // other file import { foo } from './file' console.log(foo); Presumably last one wins or, if the other way around, a reassignment of const error?
is not the same as:
module.exports = { foo: 2 };
The way your example would translate to commonjs is:
module.exports = { default: { foo: 2 }, foo: 3 }