Skip to content

Commit 8ad1539

Browse files
daiscogfilipesilva
authored andcommitted
feat(@schematics/angular): add 'none' value for the 'style' option of the component schematic
Allow setting `--style=none` for the component schematic to prevent generation of any style file. Previously this was possible only with `--inlineStyle=true`, which had the side-effect of adding an inline style block to the component decorator. Useful for components or projects which have entirely externalised stylesheets and never want to use component-specific styles.
1 parent cef94e1 commit 8ad1539

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.ts.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }
1414
display: block;
1515
}
1616
`<% } %>
17-
]<% } else { %>
17+
]<% } else if (style !== 'none') { %>
1818
styleUrls: ['./<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
1919
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
2020
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>

packages/schematics/angular/component/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { applyLintFix } from '../utility/lint-fix';
3030
import { parseName } from '../utility/parse-name';
3131
import { validateHtmlSelector, validateName } from '../utility/validation';
3232
import { buildDefaultPath, getWorkspace } from '../utility/workspace';
33-
import { Schema as ComponentOptions } from './schema';
33+
import { Schema as ComponentOptions, Style } from './schema';
3434

3535
function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile {
3636
const text = host.read(modulePath);
@@ -135,9 +135,10 @@ export default function (options: ComponentOptions): Rule {
135135
validateName(options.name);
136136
validateHtmlSelector(options.selector);
137137

138+
const skipStyleFile = options.inlineStyle || options.style === Style.None;
138139
const templateSource = apply(url('./files'), [
139140
options.skipTests ? filter((path) => !path.endsWith('.spec.ts.template')) : noop(),
140-
options.inlineStyle ? filter((path) => !path.endsWith('.__style__.template')) : noop(),
141+
skipStyleFile ? filter((path) => !path.endsWith('.__style__.template')) : noop(),
141142
options.inlineTemplate ? filter((path) => !path.endsWith('.html.template')) : noop(),
142143
applyTemplates({
143144
...strings,

packages/schematics/angular/component/index_spec.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
10-
import { Schema as ApplicationOptions } from '../application/schema';
10+
import { Style as AppStyle, Schema as ApplicationOptions } from '../application/schema';
1111
import { createAppModule } from '../utility/test';
1212
import { Schema as WorkspaceOptions } from '../workspace/schema';
1313
import { ChangeDetection, Schema as ComponentOptions, Style } from './schema';
@@ -43,7 +43,7 @@ describe('Component Schematic', () => {
4343
inlineStyle: false,
4444
inlineTemplate: false,
4545
routing: false,
46-
style: Style.Css,
46+
style: AppStyle.Css,
4747
skipTests: false,
4848
skipPackageJson: false,
4949
};
@@ -278,6 +278,15 @@ describe('Component Schematic', () => {
278278
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
279279
});
280280

281+
it('should respect the style=none option', async () => {
282+
const options = { ...defaultOptions, style: Style.None };
283+
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
284+
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
285+
expect(content).not.toMatch(/styleUrls: /);
286+
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
287+
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.none');
288+
});
289+
281290
it('should respect the type option', async () => {
282291
const options = { ...defaultOptions, type: 'Route' };
283292
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();

packages/schematics/angular/component/schema.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@
7777
]
7878
},
7979
"style": {
80-
"description": "The file extension or preprocessor to use for style files.",
80+
"description": "The file extension or preprocessor to use for style files, or 'none' to skip generating the style file.",
8181
"type": "string",
8282
"default": "css",
83-
"enum": ["css", "scss", "sass", "less"],
83+
"enum": ["css", "scss", "sass", "less", "none"],
8484
"x-user-analytics": 5
8585
},
8686
"type": {

0 commit comments

Comments
 (0)