Skip to content

Commit e7422e4

Browse files
devversionjelbourn
authored andcommittedFeb 20, 2019
fix(schematics): do not generate invalid stylesheet files (#15235)
Currently whenever someone specified Stylus or Sass as the default style extension for their Angular CLI project, the CDK/ Material schematics incorrectly generate files with that given extension. This is problematic because the schematic style templates are written in CSS and therefore are not compatible with Stylus or Sass (which are not supersets of CSS unlike less, scss) Fixes #15164
1 parent eae0efa commit e7422e4

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
 

‎src/cdk/schematics/ng-generate/drag-drop/index.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ describe('CDK drag-drop schematic', () => {
4646
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
4747
});
4848

49+
it('should not generate invalid stylesheets', () => {
50+
const tree = runner.runSchematic(
51+
'drag-drop', {styleext: 'styl', ...baseOptions}, createTestApp(runner));
52+
53+
// In this case we expect the schematic to generate a plain "css" file because
54+
// the component schematics are using CSS style templates which are not compatible
55+
// with all CLI supported styles (e.g. Stylus or Sass)
56+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.css',
57+
'Expected the schematic to generate a plain "css" file.');
58+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.styl',
59+
'Expected the schematic to not generate a "stylus" file');
60+
});
61+
4962
it('should fall back to the @schematics/angular:component option value', () => {
5063
const tree = runner.runSchematic(
5164
'drag-drop', baseOptions, createTestApp(runner, {style: 'less'}));

‎src/cdk/schematics/utils/build-component.ts

+14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ import {getProjectFromWorkspace} from './get-project';
4040
import {getDefaultComponentOptions} from './schematic-options';
4141
import {ts} from './version-agnostic-typescript';
4242

43+
/**
44+
* List of style extensions which are CSS compatible. All supported CLI style extensions can be
45+
* found here: angular/angular-cli/master/packages/schematics/angular/ng-new/schema.json#L118-L122
46+
*/
47+
const supportedCssExtensions = ['css', 'scss', 'less'];
48+
4349
function readIntoSourceFile(host: Tree, modulePath: string) {
4450
const text = host.read(modulePath);
4551
if (text === null) {
@@ -195,6 +201,14 @@ export function buildComponent(options: ComponentOptions,
195201
validateName(options.name);
196202
validateHtmlSelector(options.selector!);
197203

204+
// In case the specified style extension is not part of the supported CSS supersets,
205+
// we generate the stylesheets with the "css" extension. This ensures that we don't
206+
// accidentally generate invalid stylesheets (e.g. drag-drop-comp.styl) which will
207+
// break the Angular CLI project. See: https://github.com/angular/material2/issues/15164
208+
if (!supportedCssExtensions.includes(options.styleext!)) {
209+
options.styleext = 'css';
210+
}
211+
198212
// Object that will be used as context for the EJS templates.
199213
const baseTemplateContext = {
200214
...strings,

0 commit comments

Comments
 (0)
Please sign in to comment.