Eslint-plugin-import: New rule for monorepos: no relative import between packages

Created on 3 Aug 2018  Β·  3Comments  Β·  Source: benmosher/eslint-plugin-import

I'm not sure if this is possible with any existing rule.

I have a monorepo and I would like to ensure that packages don't import each others files using relative imports. For example:

β”œβ”€β”€ packages
β”‚Β Β  β”œβ”€β”€ first
β”‚Β Β  β”‚   β”œβ”€β”€ index.js
β”‚Β Β  β”‚   └── package.json
β”‚Β Β  └── second
β”‚Β Β      β”œβ”€β”€ b.js
β”‚Β Β      └── package.json
β”œβ”€β”€ base.js
└── package.json
  • From packages/first/index.js it disallows require('../../base')
  • From packages/first/index.js it disallows require('../second/b') but allows require('second/b') (assuming second is the package name).
  • From packages/second/b.js it disallows require('../first') but allows require('first') (assuming first is the package name)
  • From base.js it disallows require('./packages/second/b') but allows require('second/b') (this is less important than the above cases).

I think the formulation of the rule would be that any folder that contains a package.json forms a boundary that no relative imports can cross.

Most helpful comment

I don’t think the rule should handle bare imports of other sub projects, but a rule that specifies boundaries - dirs from which relative imports can not escape - seems quite useful.

All 3 comments

I don’t think the rule should handle bare imports of other sub projects, but a rule that specifies boundaries - dirs from which relative imports can not escape - seems quite useful.

Totally agree with @ljharb, need this too.

It looks like this may cover this case pretty well.

https://github.com/azz/eslint-plugin-monorepo

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yutin1987 picture yutin1987  Β·  3Comments

flipchart picture flipchart  Β·  4Comments

daltonamitchell picture daltonamitchell  Β·  3Comments

silvenon picture silvenon  Β·  3Comments

sveyret picture sveyret  Β·  3Comments