Issue
Angular version 16.1.
Is there any .ts template file or something like that for the "ng generate component" command? I would like to modify what it generates, if it is possible.
Solution
One of the suggested answers is to create a totally new schematic, which in my case is a total overkill and a waste of time.
I just wanted to add one import and extend all generated components with an inheritance for using a base component across my app. So just adding a few words to the existing template of ng generate component
.
There actually is a lot easier solution using a patch-package
With this package you can modify another package in your node_modules and keep the changes, without losing them when updating packages.
So, install it according to their guide and then install @schematics/angular
to register it in your package.json:
npm i @schematics/angular --save-dev
Apply your changes to the desired schematics template, in my case I modify the typescript component file in the directory:
node_modules\@schematics\angular\component\files
After adding my custom import and extends CustomComponent
to the class I save the file.
Then only thing left is to create a patch:
npx patch-package @schematics/angular
With that the change I made to the @schematics/angular will not be deleted when using npm install
.
In the future you can also use this command to apply all your patches:
npx patch-package
That's it. No need to create a whole new schematics, you can edit existing one in few minutes.
Answered By - Ap0st0l
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.