-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Rule preventing pulling in a whole package #642
Comments
I'm up for that. @jfmengels, any thoughts? I'd rename to |
I personally don't like importing that way. To me, everything the lib exports should be available in the I would be interested in having the opposite rule (or rather, an option that does the opposite) for the reasons I mentioned above. 👍 for PS: For Lodash, you have babel-plugin-lodash that does the work for you. |
@jfmengels: isn't https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-internal-modules.md the complement of this proposal? |
Ah yes, my bad. |
Perfect, I'll put up a PR! |
I'm not sure how this rule works - how do you verify that the file-path-based import is |
@ljharb This rule is useful for people that don't have tree shaking. Because in that case, importing a named import still pulls the whole package which is not good for your bundle size when you only need 1 file. We currently don't do verification that the path is good. It's just a simple verification not to pull the whole bundle. If people manually require the main index, then :/ |
@dozoisch tree-shaking barely works half the time, so I'm strongly in favor of this rule - I'm asking how it's implemented, not why it's desired. I think that in order for this rule to be added, it would need to, for every named import of the "main" package value, traverse the graph and locate the identical import at its original entry point - and then explicitly recommend the user use that entry point instead of the "main" entry point. There's too high a danger of confusion if it's simply a rule that bans named imports - some packages named-export values that don't exist in other entry points, and sometimes the named export is a different value than the naming of another entry point might suggest. |
Currently what it does is very simple. It warns you if you include the main package. It doesn't do any traversing or anything like it. This is something that could be done as a step 2 to improve the rule though! I would see the step one being just the rule migration as it is. |
I don't think it's a useful rule if it just warns on named imports - that's like a rule that warns on calling a function with > 2 arguments - overly broad and more harmful than useful. |
It's not just on named imports. It warns any time the main module is imported or required, for a list of modules you specify. // with "lean-imports/import": [1, [ "react-bootstrap "] ]
import RB from 'react-bootstrap'; // warns
const RB = require('react-bootstrap'); // warns
const Button = require('react-bootstrap/lib/button'); // passes The goal is really to the prevent I don't see how remembering people not to pull a whole dep is harmful! |
|
In other words, there's no benefit in adding it here unless it's doing something new, which would be "follows the dep graph". |
Actually you don't need the pattern at all - you can use both of the no-restricted rules to forbid "react" and it will automatically allow all of the sub-entry points. |
Oh, I didn't see that rule was there, I can just deprecate for that rule instead! Completely missed it in eslint-core 👍. It had actually never came to my mind you could use it for that... I'll try to put up a PR on the eslint rule to add this use case in the doc. |
The error might be a bit more obscure though, since it tells you not to import it at all instead of telling you to pull the files directly. |
Very true - however there's precedent for modifying rules like that to allow for custom error messages. I think that'd be a good issue/PR for eslint core. |
👍 I'll open an issue in eslint directly, during the day, so it can be discussed in which format it should be done! My only concern is duplication of module names between both rules, but it's a "one time" deal normally, and can be negated by using JS config. |
Closing this since it seems you've decided it's better handled with the core ESLint rule 😎 |
Some repos have defined structure to include just a few files instead of the whole bundle (eg: react-bootstrap, lodash).
We had developed an eslint plugin a while ago to have a rule to prevent this. I don't see the point of having a standalone package just for that rule, and I'd like to add it here instead. (And I haven't seen that rule here)
This is the current repo:
https://github.com/eslint-plugins/eslint-plugin-lean-imports
Let me know if you are interested!
Here is an example:
The text was updated successfully, but these errors were encountered: