Allow overriding generator template files
See original GitHub issue- I’d potentially be willing to implement this feature (contributing guide)
Description
Users should have the ability to replace the template files for generators. In some cases, the output files need small modifications in order to adhere to style guide requirements. The ability to copy the template file and make those slight modifications, while still utilizing the generator pipeline, would make this process trivial.
Motivation
I was excited to use the @nx/react:component
and @nx/react:component-story
generators to quickly scaffold new components in my project. I even created a small workspace generator to compose the two, so developers only have to call yarn generate:component
and both the component and component-story generators run in one command.
However, I was frustrated that my team members would need to immediately modify the output files and bring them up-to-par with linting rules. To fix this, I ended up copying the NX implementation of those two generators, and made very slight modifications to the template files. It worked, but took much longer than it needed to.
Suggested Implementation
I apologize, I don’t know the internals well enough to know exactly how this would work. But I have an idea of how a consumer could use this.
Extend the generator framework. Every generator takes a templateOverrideDirectory
argument. Any consumer can provide an override directory filled with templates they want to use. The exact same variables will be injected. Every generator also takes a skipTemplate
argument - an array of template file names that the consumer would like to skip, if they exist in the target generator.
The generateFiles
function will still take the files directory as an argument. However, if a file with the name exists within the override directory provided by the consumer, it is used instead of the template within the directory provided by the source generator. If there is no duplicated file, but the file name matches an entry in the skipTemplate
list, it is skipped. This allows the consumer to effectively rename or even completely reorganize the file structure. If there is no override file and no match in the skipTemplate
list, then the file is generated as usual.
It would be really nice to expand generator documentation as a part of this. I would like for consumers to know which files are created from the generator (including the ability to copy the template, so they can easily copy and modify), and which variables are available for injection into templates. I’m not sure if these could be grabbed dynamically.
Alternate Implementations
This could be implemented on a per-generator basis. This seems like a lot more work. It could take longer to release and/or result in fewer generators providing this functionality. But if there are technical limitations with my proposal, then this could work.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7
@FrozenPandaz can this ticket be re-opened please? I don’t feel the request has been understood or addressed.
Having the same issue. I even proposed a simpler solution for that https://github.com/nrwl/nx/issues/9579 and also nothing. I’m using Nx a while and every new day leads me to believe that this team is a bunch of noobs or has very incompetent boss. Sad that such a great project is so underdeveloped.
I will definitely not use it anymore and do not recommend to other’s as well. I’d spare much more time configuring the project by myself once instead of constantly fighting with some Nx’s stuff.