|
| 1 | +# Should not import all from library (`@ecocode/no-import-all-from-library`) |
| 2 | + |
| 3 | +⚠️ This rule _warns_ in the ✅ `recommended` config. |
| 4 | + |
| 5 | +<!-- end auto-generated rule header --> |
| 6 | + |
| 7 | +## Rule details |
| 8 | + |
| 9 | +This rule aims to reduce weight of programs by using only needed modules. Many libraries export only one module by |
| 10 | +default, but some of them are exporting ES modules or submodules. We should use them to select more precisly needed |
| 11 | +modules and avoid unnecessarily overloading files weight. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +*Example with the well-known [lodash](https://lodash.com/) library, if you only need "isEmpty" method.* |
| 16 | + |
| 17 | +## Options |
| 18 | + |
| 19 | +You can externally add your own libraries to be checked. |
| 20 | +To add your own libraries you need to modify your .eslintrc.js by adding the following rule configuration: |
| 21 | + |
| 22 | +```js |
| 23 | +module.exports = { |
| 24 | + ...yourConf, |
| 25 | + rules: { |
| 26 | + "no-import-all-from-library": [ |
| 27 | + "warn", |
| 28 | + { |
| 29 | + notAllowedLibraries: ["some-lib"], // will check for -> import someLib from "some-lib" |
| 30 | + importByNamespaceNotAllowedLibraries: ["some-other-lib"], // will check for -> import * as someOtherLib from "some-other-lib" |
| 31 | + }, |
| 32 | + ], |
| 33 | + }, |
| 34 | +}; |
| 35 | +``` |
| 36 | + |
| 37 | +## Examples |
| 38 | + |
| 39 | +Examples of **non-compliant** code for this rule: |
| 40 | + |
| 41 | +```js |
| 42 | +// Example with lodash |
| 43 | +import lodash from "lodash"; |
| 44 | +import { isEmpty } from "lodash"; |
| 45 | +import * as lodash from "lodash"; |
| 46 | + |
| 47 | +// Example with underscore |
| 48 | +import _ from "underscore"; |
| 49 | +``` |
| 50 | + |
| 51 | +Examples of **compliant** code for this rule: |
| 52 | + |
| 53 | +```js |
| 54 | +// Example with lodash (uses submodules) |
| 55 | +import isEmpty from "lodash/isEmpty"; |
| 56 | +import intersect from "lodash/intersect"; |
| 57 | + |
| 58 | +// Example with underscore (uses esm modules) |
| 59 | +import map from "underscore/modules/map.js"; |
| 60 | +``` |
0 commit comments