Skip to content

Instantly share code, notes, and snippets.

@sterlingwes
Created August 24, 2023 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sterlingwes/a6d95e637e9d6fdd0d44b05315df5a9d to your computer and use it in GitHub Desktop.
Save sterlingwes/a6d95e637e9d6fdd0d44b05315df5a9d to your computer and use it in GitHub Desktop.
Prevent imports from barrel files (index js / ts)

In my case I wanted to avoid importing unused modules into a built JS bundle for a lambda environment using a shared codebase. Using a nested eslint file I could prevent this kind of import: import {ModelA, ModelB} from 'src/models' and instead encourage this kind of import: import {ModelA} from 'src/models/model-a'

{
  "rules": {
    "no-restricted-imports": [
      "error",
      {
        // prevent barrel file imports to have more control over what ends up in the js bundle
        "patterns": ["src/models", "!src/models/"]
      }
    ]
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment