import-js / eslint-plugin-import

ESLint plugin with rules that help validate proper imports.
MIT License
5.57k stars 1.57k forks source link

Proposal: new rule for disable importing specific items from specific module #2218

Open meowtec opened 3 years ago

meowtec commented 3 years ago

Many packages support both "tree shacking friendly" exports style and "legacy" exports style, like this:

import { add } from 'lodash'
add(1, 2)
// 2KB

vs

import _ from 'lodash-es'
_.add(1, 2)
// 88KB

Need a rule to disable the second one.

Example

Options:

{
    "import/no-specific-imports": [
        "error",
        {
            "modules": [
                {
                    "name": "lodash-es",
                    "imports": [
                        "default",
                        "foo"
                    ]
                }
            ]
        }
    ]
}

valid:

import { bar } from 'lodash-es'
import type { foo } from 'lodash-es'

invalid:

import _ from 'lodash-es'
import { foo } from 'lodash-es'
import { foo, bar } from 'lodash-es'
import * as _ from 'lodash-es'

Draft implement: https://github.com/meowtec/eslint-plugin-import/tree/feat/no-specific-imports

ljharb commented 3 years ago

Neither of those are actually tree-shaking friendly, because they both necessitate tree-shaking. The best choice is to deep import exactly what you need, and never touch the default bag-o-things at all.

Separately, it seems like lodash-es shouldn’t be providing both named and default exports - doing that is definitely not something many packages do and is absolutely an antipattern. However, Babel import interop does allow doing both, this is true.