Improve Local Nx Plugins Loading #26668
jogelin
started this conversation in
Feature Requests
Replies: 3 comments 6 replies
-
I have a secondary entry point in "src/plugin" and it works locally. https://github.com/RobbyRabbitman/nx-plus/tree/demo/local-plugin. I could not create a stackblitz, somehow nx is not working in there, but i created a branch to fork/clone/download. |
Beta Was this translation helpful? Give feedback.
4 replies
-
Related to that, this is the approach I recommend for the moment: Current:Improved: |
Beta Was this translation helpful? Give feedback.
1 reply
-
@jogelin do u have a working example of a local plugin with ts project references instead of paths? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description:
In Nx, you can generate a specialized library called an Nx Plugin, which includes powerful built-in features such as:
When it comes to loading plugins for inferred configurations, Nx compiles and loads these plugins at runtime. However, there’s a limitation: Nx will only load the entry point specified in the main property of the plugin’s Project Configuration.
Because of that limitation, some use cases cannot be covered!
Current
❌ Cannot Use a Secondary Entry Point for Local Nx Plugins
If you need to share some functions that are unrelated to the same use case, it is recommended to create separate entry points and use the one relevant to your use case. This way, you won’t import libraries that you don’t need. This is important to avoid unnecessary compilation, especially when loading multiple plugins locally.
However, it is not possible to expose the createNodes() API in a secondary entry point for a Local Nx Plugin.
For example, if you create a separate plugin.ts file to expose your
createNodes()
:and declare it in your
tsconfig.base.json
like:and try to access it in your
nx.json
like this:Nx will not load your
libs/my-lib/plugin.ts
. Instead, it will fall back to themain
entry point specified in your project configuration, typicallylibs/my-lib/src/index.ts
.❌ Cannot Use Inferred Configuration for Local Nx Plugins
When using Nx Plugins in a monorepo, you may end up with many plugins and similar configurations. In other projects, inferring configurations can be useful, and the same principle applies to Nx Plugins.
For example, you might have an additional plugin responsible for managing all other plugins, similar to how Nx does with
@nx/plugin
:However, Nx will fail in this case. To load a plugin, Nx requires reading the
main
property from your project configuration, but since this is part of your inferred configuration, it won’t be available in yourproject.json
.Expected:
✅ Load Nx Plugins from the Entry Point Specified in the tsconfig Path
I expect that if I declare a path pointing to a different entry point like this:
Nx should allow loading the plugin both locally and in distributed repositories using:
✅ Allow Inferred Configuration for Nx Plugin Projects
I expect that I can use inferred configurations for any type of project, including Nx Plugin projects.
Suggestions
I am uncertain which use case is being addressed by defaulting to the main entry point. If the path is found in
tsconfig.base.json
, shouldn’t Nx use it directly without loading the project configuration?When searching for the path matching the plugin import, could we support wildcards instead of requiring perfect matches? This would reduce the need for declaring numerous paths.
Hacky solution
It is possible to patch Nx to support these use cases by bypassing the main property and avoiding the fallback to
src/index.ts
.Here’s a diff that outlines the changes:
Last thought
I see that Nx follows this approach internally. For example, in the
@nx/angular
plugin, generators and executors are exposed through separate entry points.@AgentEnder, I remember you mentioned the potential need to review how plugins are loaded. In the meantime, do you think it makes sense to adapt Nx’s code as suggested in the patch above?
Beta Was this translation helpful? Give feedback.
All reactions