Skip to content

Commit 290929c

Browse files
committed
fix(@angular-devkit/build-angular): only add ngsw after universal has rendered
1 parent d2984e0 commit 290929c

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

packages/angular_devkit/build_angular/src/app-shell/index.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import {
1111
BuilderConfiguration,
1212
BuilderContext,
1313
} from '@angular-devkit/architect';
14-
import { Path, getSystemPath, join, normalize, virtualFs } from '@angular-devkit/core';
14+
import { Path, getSystemPath, join, normalize, resolve, virtualFs } from '@angular-devkit/core';
1515
import { Observable, forkJoin, from, merge, of, throwError } from 'rxjs';
1616
import { concatMap, map, switchMap } from 'rxjs/operators';
1717
import { requireProjectModule } from '../angular-cli-files/utilities/require-project-module';
18+
import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker';
19+
import { BrowserBuilderSchema } from '../browser/schema';
1820
import { BuildWebpackServerSchema } from '../server/schema';
1921
import { BuildWebpackAppShellSchema } from './schema';
2022

@@ -30,7 +32,9 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
3032
let success = true;
3133
const subscription = merge(
3234
this.build(options.serverTarget, {}),
33-
this.build(options.browserTarget, { watch: false }),
35+
// Never run the browser target in watch mode.
36+
// If service worker is needed, it will be added in this.renderUniversal();
37+
this.build(options.browserTarget, { watch: false, serviceWorker: false }),
3438
).subscribe((event: BuildEvent) => {
3539
// TODO: once we support a better build event, add support for merging two event streams
3640
// together.
@@ -109,25 +113,30 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
109113
});
110114
}
111115

112-
getBrowserIndexOutputPath(options: BuildWebpackAppShellSchema) {
116+
getBrowserBuilderConfig(options: BuildWebpackAppShellSchema) {
113117
const architect = this.context.architect;
114118
const [project, target, configuration] = options.browserTarget.split(':');
115-
const builderConfig = architect.getBuilderConfiguration<BuildWebpackServerSchema>({
119+
const builderConfig = architect.getBuilderConfiguration<BrowserBuilderSchema>({
116120
project,
117121
target,
118122
configuration,
119123
});
120124

121125
return architect.getBuilderDescription(builderConfig).pipe(
122126
concatMap(description => architect.validateBuilderOptions(builderConfig, description)),
123-
map(config => join(normalize(config.options.outputPath), 'index.html')),
124127
);
125128
}
126129

127130
renderUniversal(options: BuildWebpackAppShellSchema): Observable<BuildEvent> {
131+
let browserOptions: BrowserBuilderSchema;
132+
let projectRoot: Path;
133+
128134
return forkJoin(
129-
this.getBrowserIndexOutputPath(options).pipe(
130-
switchMap(browserIndexOutputPath => {
135+
this.getBrowserBuilderConfig(options).pipe(
136+
switchMap(config => {
137+
browserOptions = config.options;
138+
projectRoot = resolve(this.context.workspace.root, config.root);
139+
const browserIndexOutputPath = join(normalize(browserOptions.outputPath), 'index.html');
131140
const path = join(this.context.workspace.root, browserIndexOutputPath);
132141

133142
return this.context.host.read(path).pipe(
@@ -151,7 +160,7 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
151160
getSystemPath(serverBundlePath),
152161
).AppServerModuleNgFactory;
153162
const indexHtml = virtualFs.fileBufferToString(indexContent);
154-
const outputPath = join(root, options.outputIndexPath || browserIndexOutputPath);
163+
const outputIndexPath = join(root, options.outputIndexPath || browserIndexOutputPath);
155164

156165
// Render to HTML and overwrite the client index file.
157166
return from(
@@ -161,9 +170,21 @@ export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
161170
})
162171
.then((html: string) => {
163172
return this.context.host
164-
.write(outputPath, virtualFs.stringToFileBuffer(html))
173+
.write(outputIndexPath, virtualFs.stringToFileBuffer(html))
165174
.toPromise();
166175
})
176+
.then(() => {
177+
if (browserOptions.serviceWorker) {
178+
return augmentAppWithServiceWorker(
179+
this.context.host,
180+
root,
181+
projectRoot,
182+
join(root, browserOptions.outputPath),
183+
browserOptions.baseHref || '/',
184+
browserOptions.ngswConfigPath,
185+
);
186+
}
187+
})
167188
.then(() => ({ success: true })),
168189
);
169190
}),

packages/angular_devkit/build_angular/test/app-shell/app-shell_spec_large.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
// tslint:disable:no-big-function
9+
810
import { DefaultTimeout, runTargetSpec } from '@angular-devkit/architect/testing';
911
import { getSystemPath, join, normalize, virtualFs } from '@angular-devkit/core';
1012
import * as express from 'express'; // tslint:disable-line:no-implicit-dependencies

0 commit comments

Comments
 (0)