question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add external imports whitelist logic to nx-enforce-module-boundaries (i.e. allowedExternalImports option)

See original GitHub issue

Description

nx-enforce-module-boundaries should allow a deny-all + whitelist logic in addition to the blacklist logic.

Motivation

There are use cases where users might need more restrictive rules and only whitelist some external imports instead of explicitly banning them.

Here is one, when implementing something similar hexagonal, onion, or clean architecture, it is safer to use whitelists. For instance, a library tagged as “type:core” will not be able to import anything using something like this allowedExternalImports: [], while a “platform:backend” & “type:data-access” will be allowed to use libraries that can communicate with databases or remote services: allowedExternalImports: ['mongo', '@google-cloud/*', ...] or something like this.

Suggested Implementation

We could implement this using an allowedExternalImports or whitelistedExternalImports option.

  • allowedExternalImports is exclusive with bannedExternalImports. We can’t use both on the same rule.
  • Nice to have: It would be nice to figure out a way of combining rules. If the project matches multiple rules, then the allowed imports should be merged together

Alternate Implementations

Meanwhile, here is the workaround:

"bannedExternalImports": [
    "((?!(@lib-a/*|@lib-b/something|lib-c)).)+"
 ]

Special thanks @meeroslav for the hint 😉

cf. https://github.com/nrwl/nx/issues/6727#issuecomment-948856631 cc. @mlebarron

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
yjaaidicommented, Dec 17, 2022

Hey @meeroslav! I finally managed to take some time and send a PR https://github.com/nrwl/nx/pull/13891 🎉

I was about to send a distinct PR for the docs as I thought that this would require a major rewrite of the “Ban External Imports” recipe but I managed to just add a chapter at the end with some examples.

Let me know what you think of this and if you want me to squash everything.

Good job for the allSourceTags btw 😉

1reaction
meeroslavcommented, Oct 28, 2022

I don’t really get why we would go through nested projects as we are just whitelisting direct external imports. I think that I am missing something.

True, it doesn’t matter in the opposite direction. Sorry for the confusion

Read more comments on GitHub >

github_iconTop Results From Across the Web

nx-enforce-module-boundaries restrict importing react in libs
I would like to restrict importing of React in libraries that are just utils. ... Add external imports whitelist logic to nx-enforce-module-boundaries (i.e....
Read more >
Ban External Imports - Nx
Whitelisting external imports with allowedExternalImports. If you need a more restrictive approach, you can use the allowedExternalImports option to ensure that ...
Read more >
Pulse · nrwl/nx · GitHub
Add external imports whitelist logic to nx-enforce-module-boundaries (i.e. allowedExternalImports option). #12870 closed Dec 21, 2022.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found