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 support to run schematics that are part of custom plugins within an existing workspace.

See original GitHub issue

Description

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)

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 command nx 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:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:27 (18 by maintainers)

github_iconTop GitHub Comments

3reactions
pascalbe-devcommented, Jul 16, 2020

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.

2reactions
AgentEndercommented, Sep 17, 2021

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:

{
  "projects": {
    "my-plugin": {
      "targets": {
        "build": {
          "executor": "@nrwl/node:package"
        },
        "build-plugin": {
          "executor": "@nrwl/run-commands",
          "options": {
            "command": "nx build my-plugin"
          }
        }
      }
    },
    "my-app": {
      "targets": {
        "serve": {
          "executor": "./dist/packages/my-plugin:serve",
          "dependsOn": [
            {
              "target": "build-plugin",
              "projects": "dependencies"
            }
          ]
        },
        "lint": {
          "executor": "./dist/packages/my-plugin:lint",
          "dependsOn": [
            {
              "target": "build-plugin",
              "projects": "dependencies"
            }
          ]
        }
      }
    }
  }
}

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:

{
  "targetDependencies": {
    "serve": [
      {
        "target": "build-plugin",
        "projects": "dependencies"
      }
    ],
    "lint": [
      {
        "target": "build-plugin",
        "projects": "dependencies"
      }
    ]
  }
}
Read more comments on GitHub >

github_iconTop 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 >

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