Add support to run schematics that are part of custom plugins within an existing workspace.
See original GitHub issueDescription
We can currently create custom schematics using the nx generate @nrwl/workspace-schematic
command and run them locally within an existing workspace (consisting of apps and libs) via nx workspace-schematic <schematic-name>
However, when creating a custom plugin via nx generate @nrwl/nx-plugin:plugin
and creating a schematic under libs/<plugin-name>/src/schematics
, there is currently no way to run this plugin locally to use/test our schematics (unless we publish)
- Here is a sample project to illustrate the issue: https://github.com/niki4810/myorg
- You can run the schematic in
tools
: https://github.com/niki4810/myorg/tree/master/tools/schematics/custom-react - But currently, there is no way to run the same schematic that is part of the plugin: https://github.com/niki4810/myorg/tree/master/libs/custom-react/src/schematics/custom-react
Please refer to this slack thread for more details: https://nrwlcommunity.slack.com/archives/CMFKWPU6Q/p1592940422065400
Motivation
Having the ability to run plugins (with custom schematics) helps in:
- following the same syntax external plugins:
nx generate <plugin-name>:<schematic>
instead of new commandnx workspace-schematic <schematic>
- Have the ability to list the schematics via:
nx list <plugin-name>
Suggested Implementaion
- I would expect it to work the same way as any published community plugins work, i.e.
nx generate <plugin-name>:<schematic>
Alternate Implementations
- N/A
Issue Analytics
- State:
- Created 3 years ago
- Reactions:16
- Comments:27 (18 by maintainers)
Top Results From Across the Web
Creating an Angular Schematic and Turn It Into an Nx Plugin
In this blog post I would like to explain how you can write a schematic and turn it into an nx plugin or...
Read more >Hi Folks I am new to … - @Nrwl/Community
I am new to Nx and I am in the process of creating some custom schematics for my workspace. Specifically, I want help...
Read more >Creating an ng add Schematic for an Nx Plugin
Learn how to build an Nx Plugin that supports `ng add`. ... on Angular schematics, this functionality is fully supported in Nx Plugins....
Read more >Better Code Generation in Angular CLI workspaces with Nx ...
Let's update the schema for the schematic to accept some options, such as the service name, the path to create the service, and...
Read more >nrwl/nx Workspace-Specific Schematics - Stack Overflow
Then you could run that schematic with: npm run workspace-schematic <name-schematic> . The default schematic that you create with @nrwl/ ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is actually possible. You can build the plugin locally, so it ends up in your dist folder. Then you can run it directly from that build target location via nx. In your sample repo, it would work like that:
nx build custom-react
nx g ./dist/libs/custom-react:library
Ofc you can also pass options.
This is something we are re-evaluating post Nx 13.0, as we agree that it would help DX tremendously.
As a quick note, there are some workarounds or other things you can do to enhance DX with the current setup.
Workspace Generator -> Generators in a Plugin
If moving a workspace generator into a plugin, you can have the existing workspace-generator import the plugins generator and call it. This way people don’t have to switch the commands they are used to using already either.
Executors from a local nx plugin
For executors in a “workspace-plugin”, you can make sure projects that use the plugin have an implicit dependency on the plugin project. This ensures that the plugin is built (and in its latest state) before the executor runs. Since nx caches these builds, it shouldn’t extend your command run times.
Just adding the dependency like above, is all thats needed for build targets. This is because of the targetDependencies section of nx.json’s default contents. If your executor has a generic targetName, you would need to add a dependsOn block to the target, and may want to add a target on the plugin called “build-plugin” or similar, to avoid building non plugin projects before the executor runs.
For example:
If you have a plugin that contains a new serve executor, and a lint executor, you may have a
workspace.json
with projects such as below:If multiple projects use those custom executors in your workspace, it would make more sense to remove the dependsOn blocks, and use targetDependencies in
nx.json
. That would look something like: