Skip to content

Commit 4718de4

Browse files
Alanmgechev
authored andcommitted
feat(@schematics/angular): only support known style extentions
Fixes #13149
1 parent b1b365e commit 4718de4

File tree

19 files changed

+77
-32
lines changed

19 files changed

+77
-32
lines changed

packages/angular/cli/lib/config/schema.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,16 @@
140140
"default": "css"
141141
},
142142
"style": {
143-
"description": "The file extension to use for style files.",
143+
"description": "The file extension or preprocessor to use for style files.",
144144
"type": "string",
145-
"default": "css"
145+
"default": "css",
146+
"enum": [
147+
"css",
148+
"scss",
149+
"sass",
150+
"less",
151+
"styl"
152+
]
146153
},
147154
"viewEncapsulation": {
148155
"description": "Specifies the view encapsulation strategy.",

packages/schematics/angular/app-shell/index_spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ describe('App Shell Schematic', () => {
3333
inlineStyle: false,
3434
inlineTemplate: false,
3535
routing: true,
36-
style: 'css',
3736
skipTests: false,
3837
skipPackageJson: false,
3938
};

packages/schematics/angular/application/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
url,
3232
} from '@angular-devkit/schematics';
3333
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
34+
import { styleToFileExtention } from '../component/index';
3435
import { Schema as ComponentOptions } from '../component/schema';
3536
import { Schema as E2eOptions } from '../e2e/schema';
3637
import {
@@ -48,7 +49,7 @@ import {
4849
WorkspaceProject,
4950
WorkspaceSchema,
5051
} from '../utility/workspace-models';
51-
import { Schema as ApplicationOptions } from './schema';
52+
import { Schema as ApplicationOptions, Style } from './schema';
5253

5354

5455
// TODO: use JsonAST
@@ -162,15 +163,15 @@ function addAppToWorkspaceFile(options: ApplicationOptions, workspace: Workspace
162163

163164
if (options.inlineTemplate === true
164165
|| options.inlineStyle === true
165-
|| options.style !== 'css') {
166+
|| options.style !== Style.Css) {
166167
schematics['@schematics/angular:component'] = {};
167168
if (options.inlineTemplate === true) {
168169
(schematics['@schematics/angular:component'] as JsonObject).inlineTemplate = true;
169170
}
170171
if (options.inlineStyle === true) {
171172
(schematics['@schematics/angular:component'] as JsonObject).inlineStyle = true;
172173
}
173-
if (options.style && options.style !== 'css') {
174+
if (options.style && options.style !== Style.Css) {
174175
(schematics['@schematics/angular:component'] as JsonObject).styleext = options.style;
175176
}
176177
}
@@ -346,6 +347,8 @@ export default function (options: ApplicationOptions): Rule {
346347
projectRoot: newProjectRoot ? `${newProjectRoot}/${options.name}-e2e` : 'e2e',
347348
};
348349

350+
const styleExt = styleToFileExtention(options.style);
351+
349352
return chain([
350353
addAppToWorkspaceFile(options, workspace),
351354
mergeWith(
@@ -356,6 +359,7 @@ export default function (options: ApplicationOptions): Rule {
356359
...options,
357360
'dot': '.',
358361
relativePathToWorkspaceRoot,
362+
styleExt,
359363
}),
360364
move(sourceRoot),
361365
])),
@@ -417,6 +421,7 @@ export default function (options: ApplicationOptions): Rule {
417421
...options as any, // tslint:disable-line:no-any
418422
selector: appRootSelector,
419423
...componentOptions,
424+
styleExt,
420425
}),
421426
move(sourceDir),
422427
]), MergeStrategy.Overwrite),

packages/schematics/angular/application/other-files/app.component.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { Component } from '@angular/core';
2828
`,<% } else { %>
2929
templateUrl: './app.component.html',<% } if(inlineStyle) { %>
3030
styles: []<% } else { %>
31-
styleUrls: ['./app.component.<%= style %>']<% } %>
31+
styleUrls: ['./app.component.<%= styleExt %>']<% } %>
3232
})
3333
export class AppComponent {
3434
title = '<%= name %>';

packages/schematics/angular/application/schema.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@
5454
"alias": "p"
5555
},
5656
"style": {
57-
"description": "The file extension to use for style files.",
57+
"description": "The file extension or preprocessor to use for style files.",
5858
"type": "string",
59-
"default": "css"
59+
"default": "css",
60+
"enum": [
61+
"css",
62+
"scss",
63+
"sass",
64+
"less",
65+
"styl"
66+
]
6067
},
6168
"skipTests": {
6269
"description": "When true, does not create \"spec.ts\" test files for the app.",

packages/schematics/angular/class/index_spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ describe('Class Schematic', () => {
3535
inlineStyle: false,
3636
inlineTemplate: false,
3737
routing: false,
38-
style: 'css',
3938
skipTests: false,
4039
skipPackageJson: false,
4140
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }
99
`,<% } else { %>
1010
templateUrl: './<%= dasherize(name) %>.component.html',<% } if(inlineStyle) { %>
1111
styles: []<% } else { %>
12-
styleUrls: ['./<%= dasherize(name) %>.component.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
12+
styleUrls: ['./<%= dasherize(name) %>.component.<%= styleExt %>']<% } %><% if(!!viewEncapsulation) { %>,
1313
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1414
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
1515
})

packages/schematics/angular/component/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { applyLintFix } from '../utility/lint-fix';
3232
import { parseName } from '../utility/parse-name';
3333
import { buildDefaultPath, getProject } from '../utility/project';
3434
import { validateHtmlSelector, validateName } from '../utility/validation';
35-
import { Schema as ComponentOptions } from './schema';
35+
import { Schema as ComponentOptions, Style } from './schema';
3636

3737
function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile {
3838
const text = host.read(modulePath);
@@ -145,22 +145,23 @@ export default function (options: ComponentOptions): Rule {
145145

146146
// todo remove these when we remove the deprecations
147147
options.style = (
148-
options.style && options.style !== 'css'
149-
? options.style : options.styleext
150-
) || 'css';
148+
options.style && options.style !== Style.Css
149+
? options.style : options.styleext as Style
150+
) || Style.Css;
151151
options.skipTests = options.skipTests || !options.spec;
152152

153153
validateName(options.name);
154154
validateHtmlSelector(options.selector);
155155

156156
const templateSource = apply(url('./files'), [
157157
options.skipTests ? filter(path => !path.endsWith('.spec.ts.template')) : noop(),
158-
options.inlineStyle ? filter(path => !path.endsWith('.__style__.template')) : noop(),
158+
options.inlineStyle ? filter(path => !path.endsWith('.__styleExt__.template')) : noop(),
159159
options.inlineTemplate ? filter(path => !path.endsWith('.html.template')) : noop(),
160160
applyTemplates({
161161
...strings,
162162
'if-flat': (s: string) => options.flat ? '' : s,
163163
...options,
164+
styleExt: styleToFileExtention(options.style),
164165
}),
165166
move(parsedPath.path),
166167
]);
@@ -174,3 +175,12 @@ export default function (options: ComponentOptions): Rule {
174175
]);
175176
};
176177
}
178+
179+
export function styleToFileExtention(style: Style | undefined): string {
180+
switch (style) {
181+
case Style.Sass:
182+
return 'scss';
183+
default:
184+
return style || 'css';
185+
}
186+
}

0 commit comments

Comments
 (0)