What is the benefit of prefer-default-export?
The docs don't have a Why section for prefer-default-export, and I'm not seeing the benefit of it on my own. I would think that not using default is preferred. With default exports, you lose refactoring power (if you rename the source const/function/class, it won't rename default imports).
As more of an edge case: it makes code less future-proof. i.e. if you create a file that will be a collection of errors, but it only starts with one error, to follow the linting rules, you'll have to have it export default, but then when you add the 2nd error at a later time, you'll have to do a bunch of refactoring that could have been prevented if the recommendation was to avoid default export.
Absolutely! This one :-) it uses eslint-plugin-import which verifies imports and exports across files. Thus, typos are a non-issue with either flavor of exports, when using this linter config.
Cool, that sounds like what I'm looking for, but I think you're missing a link haha
No link is required; use this repo's config and you get that checking for free, that's part of the whole point of it :-p
Limiting yourself to only named exports would be just as limiting as limiting yourself to only named exports.
I presume one of these occurrences were meant to be "... to only default exports"? (So as to be less tautological, unless I'm missing something here)
Although this rule encourages single default exports, it does not discourage multiple named exports.
- If you are exporting one thing and forget to make it the default, the rule just reminds you to make it the default. This is is the primary use case.
- If you have one thing and you know you are going to add more stuff later, add a linter directive to the file temporarily to silence the rule. This is an exception to the rule.
- If this rule conflicts with your project's existing style, just turn the rule off.
The strongest argument against the rule I have read in this thread is that, in the case of an exception, the developer might think they need to export a dictionary as the default. If that is the case and it is an undesirable style, then there should be a rule that discourages default dictionary exports.
@FireyFly yes thanks, i've corrected the typo.
As the one that complained that I've seen too many people exporting a dictionary as the default I'd love a lint rule that forbid people from doing this.
@aboyton there's https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-anonymous-default-export.md which only allows it if it's stored in a named var (which is weird), but otherwise I don't see anything. Feel free to file a proposal issue, or a PR, on eslint-plugin-import, and we can discuss it there.
For cases like this,
/ACollectionOfTinyComponent/index.js
import Container from './Container';
import Container2 from './Container2';
export {
Container,
Container2,
};
So the index.js becomes a directory to include other small parts without having to write each Component Name out all the time. So in this case, we can import a component in another component like following:
import {Container} from './ACollectionOfTinyComponent'
//then use Container in the code here
Is this a bad practice?
Whoever interested can answer here as well ...https://stackoverflow.com/questions/47874818/how-to-get-around-airbnb-eslint-import-prefer-default-export-rule-when-using-ind
^^^ I answered question myself, I think its just that by looking at the lint rule name itself is not that obvious for what the error was.
@adamchenwei if you're going to have a "manifest" file like that; named exports are the best way to do it - do NOT default-export an object.
However, I'd discourage such index manifests - all they do is make treeshaking necessary by importing too much into the dependency graph. People should deep-import the things they need from a file that default-exports just that thing.
My IDE can't and I think no IDE can resolve and automatically import the package I need as export default cut the relationship.
If your IDE works with named exports but breaks on default exports, I’d find a better IDE. There is zero reason default or named exports should differ in functionality or restrictability in any way, full stop.
Or maybe file a bug report with the IDE/plugin that is missing this feature. If it really supports named imports (which it might not), then it should support default imports. Naive autocomplete implementations using variable name matching could make it seem like it understands imports when it doesn’t.
@tom10271 It doesn't even make sense why an IDE would do better with named exports and not default ones. In order to find named exports it needs to index and parse every single file in the entire project. In the most common case for default exports you're going to name the import the same as end of the full path without extension.
So for an IDE when you type import map it could easily just suggest these: [import map from "./operators/sync/map.js", import map from "./operators/async/map.js"] or whatever files ending in map.js you might have.
Now obviously it's a bit more complicated if you wanted to import both the sync and async versions for example, but I don't see how named exports would make this much better, instead your IDE could just have some fancy syntax that is replaced (e.g. import async/map could suggest an expansion like import asyncMap from "./operators/async/map.js" or something like that).
Unrelated to the first part I'm personally not entirely opposed to named exports, but I think it would be better if tree-shaking could be opt-ed into by the language itself instead of as a tooling step that has to try to detect if exports are pure or not (which is hard!). I suggested a scheme for tree-shaken library exports decided by the library author here as static export ... from ..., but it didn't garner much interest.
I don't get the "single thing per file in your entire project" thing. Then why on earth is a JS file equal to a JS Module? A module with only one thing in it breaks the concept of being a module. A module is meant to group things that belong together, so named exports are more logical towards the fact that a JS file is a module.
I must say though that for libraries, I do tend to split everything up, just for easier development and better understand all the pieces of the library, but they become sub-modules then.
@dietergeerts Try not to mix up the concept of modules with the concept of packages. Normally when people talk about using modules to break their code up they mean it in the literal english definition of small parts that can be easily replaced and re-used.
The idea of a module is definitely not to group things that belong together, modules should be minimal pieces that be put together to build larger things from them. This is why it's recommended to split at the most granular level of only using export default as it forces you to make all parts relatively re-usable and replaceable.
Packages on the other hand are a group of things that belong together. In fact typically a package will be a group of one or more modules. You might even call a package itself a "module" as it's a replaceable/re-usable singular unit that be put into larger systems.
As an example consider a package like Angular, this package consists of many many modules (the small parts which make up angular itself). You might however treat the Angular package as a "module" because it's sort've it's own unit that you don't need to look into and can replace (normally just with newer versions) and re-use as you need.
Also don't confuse the authoring format with the concept, there's many authoring formats for modules (ES Modules, CommonJS, UMD, ...) but the concept of modules being small replaceable/re-usable chunks of the application always holds regardless of what format it's in.
@Jamesernator, I get your point, but is 20 files of all one line of code is easier to read through than one file of 20 lines of code? and where all these 20 functions all belong together very tightly.
This rule only applies to files that export one thing. If a file exports multiple things, then it will not complain about not exporting a default.
https://github.com/airbnb/javascript/issues/1365#issuecomment-346976818
@deckar01 , true, but that's the thing not wanted, as later on, we can add extra exports etc... An application is in constant development...
@dietergeerts https://github.com/airbnb/javascript/issues/1365#issuecomment-394702944 is spot on; a module is not necessarily meant to group things that belong together; that's what directories are for. A module is meant to encapsulate an explicit API - whether that's one thing or multiple things.