Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@schematics/angular): add 'none' value for the 'style' option of the component schematic #21141

Merged
merged 1 commit into from Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,7 +14,7 @@ import { Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }
display: block;
}
`<% } %>
]<% } else { %>
]<% } else if (style !== 'none') { %>
styleUrls: ['./<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
Expand Down
5 changes: 3 additions & 2 deletions packages/schematics/angular/component/index.ts
Expand Up @@ -30,7 +30,7 @@ import { applyLintFix } from '../utility/lint-fix';
import { parseName } from '../utility/parse-name';
import { validateHtmlSelector, validateName } from '../utility/validation';
import { buildDefaultPath, getWorkspace } from '../utility/workspace';
import { Schema as ComponentOptions } from './schema';
import { Schema as ComponentOptions, Style } from './schema';

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

const skipStyleFile = options.inlineStyle || options.style === Style.None;
const templateSource = apply(url('./files'), [
options.skipTests ? filter((path) => !path.endsWith('.spec.ts.template')) : noop(),
options.inlineStyle ? filter((path) => !path.endsWith('.__style__.template')) : noop(),
skipStyleFile ? filter((path) => !path.endsWith('.__style__.template')) : noop(),
options.inlineTemplate ? filter((path) => !path.endsWith('.html.template')) : noop(),
applyTemplates({
...strings,
Expand Down
13 changes: 11 additions & 2 deletions packages/schematics/angular/component/index_spec.ts
Expand Up @@ -7,7 +7,7 @@
*/

import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as ApplicationOptions } from '../application/schema';
import { Style as AppStyle, Schema as ApplicationOptions } from '../application/schema';
import { createAppModule } from '../utility/test';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { ChangeDetection, Schema as ComponentOptions, Style } from './schema';
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('Component Schematic', () => {
inlineStyle: false,
inlineTemplate: false,
routing: false,
style: Style.Css,
style: AppStyle.Css,
skipTests: false,
skipPackageJson: false,
};
Expand Down Expand Up @@ -278,6 +278,15 @@ describe('Component Schematic', () => {
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
});

it('should respect the style=none option', async () => {
const options = { ...defaultOptions, style: Style.None };
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).not.toMatch(/styleUrls: /);
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.none');
});

it('should respect the type option', async () => {
const options = { ...defaultOptions, type: 'Route' };
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
Expand Down
4 changes: 2 additions & 2 deletions packages/schematics/angular/component/schema.json
Expand Up @@ -77,10 +77,10 @@
]
},
"style": {
"description": "The file extension or preprocessor to use for style files.",
"description": "The file extension or preprocessor to use for style files, or 'none' to skip generating the style file.",
"type": "string",
"default": "css",
"enum": ["css", "scss", "sass", "less"],
"enum": ["css", "scss", "sass", "less", "none"],
"x-user-analytics": 5
},
"type": {
Expand Down