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.

builder: how to write custom builder within nx / provide schematics

See original GitHub issue

Current Behaviour

So I was trying to build custom builder by following angular-devkit docs.

In the end, I was able to run it, but it didn’t work.

Error image

The process required “a lot” of overhead from developer perspective:

  • need to manually create all the boilerplate
  • need to compile builder on your own
  • need to create npm package and use yarn link and friends

Expected Behaviour/Feature Request

  1. provide docs how to author custom builder within nx workspace

  2. provide tooling

  • provide have schematic for booting custom builders (we have nx g @nrwl/workspace:workspace-schematic, so something like nx g @nrwl/workspace:workspace-builder)
  • allow custom workspace builder to be used as targets without all that manual churn that is needed at the moment

thx a lot !

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
georgieecommented, Nov 11, 2020

Hello, this is probably more an issue on the Angular side, as they just don’t have official support for TS schematics (and builders) to run directly yet. See https://github.com/angular/angular-cli/issues/13858.

I had the same pain and I want to share how we approach it currently for our project where we have various builders and schematics which we use on a regular basis locally and on the CI. In the beginning we searched for a “elegant” solution, then quickly decided for the pragmatic way to get things done. We use that solution every day since then.

  1. Create the builder as a library package as I don’t want to use the nx tools/ folder for our shared packages.
  2. Create an entry for the builder in your angular.json/workspace.json so you can build it anytime (yarn nx build @builders/builder-a)
  3. Ensure that you build it whenever you install your workspace. That way you always have a working artifact sitting in your workspace you can point to. This works locally for developers but also on the CI/CD. In your package.json we have a part to ensure the builders are compiled. We use npm-run-all for the wildcard support.
"postinstall": "run-s postinstall:*",
"postinstall:builder-a": "yarn nx build @builders/builder-a",
"postinstall:builder-b": "yarn nx build @builders/builder-b",
"postinstall:builder-c": "yarn nx build @builders/builder-c",

The builder artifacts will be written to build/libs/builders/builder-a and so on.

  1. You can now start using it. Refer to the artifact via "builder": "./build/libs/builders/builder-a in your target configuration in angular.json, wherever you want to use the builder

  2. You are currently developing the builder? We have a yarn nx develop @builders/builder-a target which will output the latest artifacts into build/libs/builders/builder-a whenever we change code (through a custom nodemon watch task). Works flawless for schematics & builders for us, especially if you use jest for a test driven approach, as you can run the schematics within jest without even building it as jest compiles the typescript.

As said, this is a limitation by Angular. Maybe we can extend the Nx Documentation to guide people properly or craft a schematic to support the creation of the various parts. I’m sure there is some smart person who can provide a solution to run the builders directly with ts-node some day, but until then the outlined solution is a good start that works without any problems for us. I hope this helps a few folks 🙏

2reactions
mtuzinskiycommented, Jan 25, 2022

Any updates on this? It’s a needed thing; I’d like to be able to develop Angular Builders in my Nrwl monorepo seamlessly

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nx Tutorial: Building Custom Plugins for Nx - YouTube
Get details about Nx plugins! Now you can create your own custom plugins. Nx core team member, Isaac Mann, tells shows you how,...
Read more >
Create Your First Custom Angular CLI Schematic with Nx - Auth0
Learn how to create applications, libraries, and custom workspace CLI Angular schematics with enterprise Angular tool Nx.
Read more >
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 >
Nx custom schematic in repository not able to load
I setup a repo for a custom Nx builder and schematics to build an application / init. Initially I developed it in the...
Read more >
How to Forge a Powerful Custom Builder in Angular
Our goal is to write a builder that publishes our libraries for us. ... a custom builder, if you're using the tools provided...
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