'Order of chain rules in Angular schematics
I want to create a custom schematic that installs an Angular material theme. The problem is that I modify the style.scss file but my modifications are overridden by the externalSchematic which installs @angular/material.
How do I get my change to work?
This is my schematics
export function addMd(options: any): Rule {
return (_tree: Tree, _context: SchematicContext) => {
const appName = options.appName;
const chains: Rule[] = [];
chains.push(installMd(appName));
const templateSource = apply(url(`./files/theming`), [
template({...options, ...strings}),
]);
const merged = mergeWith(templateSource, MergeStrategy.Overwrite);
chains.push(merged);
chains.push(addTheme(appName));
const rule = chain(chains);
return rule(tree, _context) as Rule;
};
}
function installMd(appName: string): Rule {
return externalSchematic('@angular/material', 'ng-add', {
theme: 'custom',
typography: true,
animations: true,
project: appName
});
}
function updateStyle(appName: string) {
return (tree: Tree, _context: SchematicContext) => {
const styleContent = '.... MY UPDATE ...`;
const stylePath = `./projects/${appName}/src/styles.scss`;
tree.overwrite(stylePath, styleContent);
return tree;
};
}
And the result of my styles.scss
// Custom Theming for Angular Material
// For more information: https://material.angular.io/guide/theming
@use '~@angular/material' as mat;
// Plus imports for other components in your app.
// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat.core();
// Define the palettes for your theme using the Material Design palettes available in palette.scss
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
// hue. Available color palettes: https://material.io/design/color/
$admin-primary: mat.define-palette(mat.$indigo-palette);
$admin-accent: mat.define-palette(mat.$pink-palette, A200, A100, A400);
// The warn palette is optional (defaults to red).
$admin-warn: mat.define-palette(mat.$red-palette);
// Create the theme object. A theme consists of configurations for individual
// theming systems such as "color" or "typography".
$admin-theme: mat.define-light-theme((
color: (
primary: $admin-primary,
accent: $admin-accent,
warn: $admin-warn,
)
));
// Include theme styles for core and each component used in your app.
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
@include mat.all-component-themes($admin-theme);
.... MY UPDATE ...
html, body { height: 100%; }
body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }
Thnaks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
