Skip to content

Commit 2b2d356

Browse files
alan-agius4Keen Yee Liau
authored and
Keen Yee Liau
committed
fix(@schematics/angular): fixes issue that ViewEncapsulation is not being configured when provided
Fixes #13689
1 parent df241dc commit 2b2d356

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { enableProdMode } from '@angular/core';
1+
import { enableProdMode<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';
22
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
33

44
import { AppModule } from './app/app.module';
@@ -7,6 +7,12 @@ import { environment } from './environments/environment';
77
if (environment.production) {
88
enableProdMode();
99
}
10-
10+
<% if(!!viewEncapsulation) { %>
11+
platformBrowserDynamic().bootstrapModule(AppModule, {
12+
defaultEncapsulation: ViewEncapsulation.<%= viewEncapsulation %>
13+
})
14+
.catch(err => console.error(err));
15+
<% } else { %>
1116
platformBrowserDynamic().bootstrapModule(AppModule)
1217
.catch(err => console.error(err));
18+
<% } %>

packages/schematics/angular/application/index.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,18 @@ function addAppToWorkspaceFile(options: ApplicationOptions, workspace: Workspace
164164
if (options.inlineTemplate === true
165165
|| options.inlineStyle === true
166166
|| options.style !== Style.Css) {
167-
schematics['@schematics/angular:component'] = {};
167+
const componentSchematicsOptions: JsonObject = {};
168168
if (options.inlineTemplate === true) {
169-
(schematics['@schematics/angular:component'] as JsonObject).inlineTemplate = true;
169+
componentSchematicsOptions.inlineTemplate = true;
170170
}
171171
if (options.inlineStyle === true) {
172-
(schematics['@schematics/angular:component'] as JsonObject).inlineStyle = true;
172+
componentSchematicsOptions.inlineStyle = true;
173173
}
174174
if (options.style && options.style !== Style.Css) {
175-
(schematics['@schematics/angular:component'] as JsonObject).style = options.style;
175+
componentSchematicsOptions.style = options.style;
176176
}
177+
178+
schematics['@schematics/angular:component'] = componentSchematicsOptions;
177179
}
178180

179181
if (options.skipTests === true) {

packages/schematics/angular/application/index_spec.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/te
1010
import { latestVersions } from '../utility/latest-versions';
1111
import { getFileContent } from '../utility/test';
1212
import { Schema as WorkspaceOptions } from '../workspace/schema';
13-
import { Schema as ApplicationOptions, Style } from './schema';
13+
import { Schema as ApplicationOptions, Style, ViewEncapsulation } from './schema';
1414

1515
// tslint:disable:max-line-length
1616
describe('Application Schematic', () => {
@@ -116,6 +116,17 @@ describe('Application Schematic', () => {
116116
expect(content).toMatch(/import { AppComponent } from \'\.\/app\.component\';/);
117117
});
118118

119+
it(`should set 'defaultEncapsulation' in main.ts when 'ViewEncapsulation' is provided`, () => {
120+
const tree = schematicRunner.runSchematic('application', {
121+
...defaultOptions,
122+
viewEncapsulation: ViewEncapsulation.ShadowDom,
123+
}, workspaceTree);
124+
const path = '/projects/foo/src/main.ts';
125+
const content = tree.readContent(path);
126+
expect(content).toContain('defaultEncapsulation: ViewEncapsulation.ShadowDom');
127+
expect(content).toContain(`import { enableProdMode, ViewEncapsulation } from '@angular/core'`);
128+
});
129+
119130
it('should set the right paths in the tsconfig files', () => {
120131
const tree = schematicRunner.runSchematic('application', defaultOptions, workspaceTree);
121132
let path = '/projects/foo/tsconfig.app.json';
@@ -293,7 +304,6 @@ describe('Application Schematic', () => {
293304

294305
it('should set the relative tsconfig paths', () => {
295306
const options = { ...defaultOptions, projectRoot: '' };
296-
297307
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
298308
const appTsConfig = JSON.parse(tree.readContent('/src/tsconfig.app.json'));
299309
expect(appTsConfig.extends).toEqual('../tsconfig.json');

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';
22

33
@Component({
44
selector: '<%= selector %>',<% if(inlineTemplate) { %>
@@ -28,7 +28,8 @@ import { Component } from '@angular/core';
2828
`,<% } else { %>
2929
templateUrl: './app.component.html',<% } if(inlineStyle) { %>
3030
styles: []<% } else { %>
31-
styleUrls: ['./app.component.<%= styleExt %>']<% } %>
31+
styleUrls: ['./app.component.<%= styleExt %>']<% } %>,<% if(!!viewEncapsulation) { %>,
32+
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } %>
3233
})
3334
export class AppComponent {
3435
title = '<%= name %>';

0 commit comments

Comments
 (0)