Socket
Book a DemoInstallSign in
Socket

eslint-plugin-small-import

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-small-import

ESLint rule for preventing the full import of big libraries when just a part of it can be imported.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

eslint-plugin-small-import

ESLint rule for preventing the full import of big libraries when just a part of it can be imported.

small-import.gif

Installation

npm i eslint-plugin-small-import

Add this to your .eslintrc or your config::

"plugins": ["small-import"],
"rules": {
    "small-import/no-full-import": "error"
}

Disallow full import (no-full-import)

Some JavaScript libraries are a vast collection of independent helpers functions which are combined in one package, such as lodash or date-fns. If you import one of their functions, you usually import the full suite of functions, causing higher memory consumption, startup time and bundle sizes.

The solution for this problem is to import each function directly from it's location in the package rather than the index file of the package. However, it is more tedious since more code has to be written.

Rule details

This rule will automatically detect these cases and convert the imports. It supports lodash, date-fns and rambda out of the box, but can be configured to support more libraries as well.

Options

A map of packages can be supplied, where the keys are the names of the libraries and the values are the location of the distinct functions within the package. More practically, the values determine what gets put inbetween the package name and the function name. The default configuration is:

["error", {
    "packages": {
        "lodash": "/",
        "date-fns": "/",
        "rambda": "/src/"
    }
}]

Examples

Examples of correct code for this rule:

import sortBy from 'lodash/sortBy'
import groupBy from 'lodash/groupBy'
import format from 'date-fns/format'
import append from 'rambda/src/append'

Examples of incorrect code for this rule:

import {sortBy, groupBy} from 'lodash'
import {format} from 'date-fns'
import {append} from 'rambda'

When Not To Use It

If your application is not sensitive to memory consumption, startup time or bundle size, e.g. in a script, then you can disable this rule.

If you are not using the import syntax yet, it's not supported by this rule.

If you don't use packages which would cause this problem, you can disable this rule.

License

MIT

Author

© Jonny Burger

Keywords

eslint

FAQs

Package last updated on 14 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts