-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex.ts
195 lines (171 loc) · 6.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
BuildEvent,
Builder,
BuilderConfiguration,
BuilderContext,
} from '@angular-devkit/architect';
import { Path, getSystemPath, join, normalize, resolve, virtualFs } from '@angular-devkit/core';
import { Observable, forkJoin, from, merge, of, throwError } from 'rxjs';
import { concatMap, map, switchMap } from 'rxjs/operators';
import { requireProjectModule } from '../angular-cli-files/utilities/require-project-module';
import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker';
import { BrowserBuilderSchema } from '../browser/schema';
import { BuildWebpackServerSchema } from '../server/schema';
import { BuildWebpackAppShellSchema } from './schema';
export class AppShellBuilder implements Builder<BuildWebpackAppShellSchema> {
constructor(public context: BuilderContext) { }
run(builderConfig: BuilderConfiguration<BuildWebpackAppShellSchema>): Observable<BuildEvent> {
const options = builderConfig.options;
return new Observable<BuildEvent>(obs => {
let success = true;
const subscription = merge(
this.build(options.serverTarget, {}),
// Never run the browser target in watch mode.
// If service worker is needed, it will be added in this.renderUniversal();
this.build(options.browserTarget, { watch: false, serviceWorker: false }),
).subscribe((event: BuildEvent) => {
// TODO: once we support a better build event, add support for merging two event streams
// together.
success = success && event.success;
}, error => {
obs.error(error);
}, () => {
obs.next({ success });
obs.complete();
});
// Allow subscriptions to us to unsubscribe from each builds at the same time.
return () => subscription.unsubscribe();
}).pipe(
switchMap(event => {
if (!event.success) {
return of(event);
}
return this.renderUniversal(options);
}),
);
}
build(targetString: string, overrides: {}) {
const architect = this.context.architect;
const [project, target, configuration] = targetString.split(':');
// Override browser build watch setting.
const builderConfig = architect.getBuilderConfiguration<{}>({
project,
target,
configuration,
overrides,
});
return architect.run(builderConfig, this.context);
}
getServerModuleBundlePath(options: BuildWebpackAppShellSchema) {
const architect = this.context.architect;
return new Observable<Path>(obs => {
if (options.appModuleBundle) {
obs.next(join(this.context.workspace.root, options.appModuleBundle));
return obs.complete();
} else {
const [project, target, configuration] = options.serverTarget.split(':');
const builderConfig = architect.getBuilderConfiguration<BuildWebpackServerSchema>({
project,
target,
configuration,
});
return architect.getBuilderDescription(builderConfig).pipe(
concatMap(description => architect.validateBuilderOptions(builderConfig, description)),
switchMap(config => {
const outputPath = join(this.context.workspace.root, config.options.outputPath);
return this.context.host.list(outputPath).pipe(
switchMap(files => {
const re = /^main\.(?:[a-zA-Z0-9]{20}\.)?(?:bundle\.)?js$/;
const maybeMain = files.filter(x => re.test(x))[0];
if (!maybeMain) {
return throwError(new Error('Could not find the main bundle.'));
} else {
return of(join(outputPath, maybeMain));
}
}),
);
}),
).subscribe(obs);
}
});
}
getBrowserBuilderConfig(options: BuildWebpackAppShellSchema) {
const architect = this.context.architect;
const [project, target, configuration] = options.browserTarget.split(':');
const builderConfig = architect.getBuilderConfiguration<BrowserBuilderSchema>({
project,
target,
configuration,
});
return architect.getBuilderDescription(builderConfig).pipe(
concatMap(description => architect.validateBuilderOptions(builderConfig, description)),
);
}
renderUniversal(options: BuildWebpackAppShellSchema): Observable<BuildEvent> {
let browserOptions: BrowserBuilderSchema;
let projectRoot: Path;
return forkJoin(
this.getBrowserBuilderConfig(options).pipe(
switchMap(config => {
browserOptions = config.options;
projectRoot = resolve(this.context.workspace.root, config.root);
const browserIndexOutputPath = join(normalize(browserOptions.outputPath), 'index.html');
const path = join(this.context.workspace.root, browserIndexOutputPath);
return this.context.host.read(path).pipe(
map<virtualFs.FileBuffer, [Path, virtualFs.FileBuffer]>(x => {
return [browserIndexOutputPath, x];
}),
);
}),
),
this.getServerModuleBundlePath(options),
).pipe(
switchMap(([[browserIndexOutputPath, indexContent], serverBundlePath]) => {
const root = this.context.workspace.root;
requireProjectModule(getSystemPath(root), 'zone.js/dist/zone-node');
const renderModuleFactory = requireProjectModule(
getSystemPath(root),
'@angular/platform-server',
).renderModuleFactory;
const AppServerModuleNgFactory = require(
getSystemPath(serverBundlePath),
).AppServerModuleNgFactory;
const indexHtml = virtualFs.fileBufferToString(indexContent);
const outputIndexPath = join(root, options.outputIndexPath || browserIndexOutputPath);
// Render to HTML and overwrite the client index file.
return from(
renderModuleFactory(AppServerModuleNgFactory, {
document: indexHtml,
url: options.route,
})
.then((html: string) => {
return this.context.host
.write(outputIndexPath, virtualFs.stringToFileBuffer(html))
.toPromise();
})
.then(() => {
if (browserOptions.serviceWorker) {
return augmentAppWithServiceWorker(
this.context.host,
root,
projectRoot,
join(root, browserOptions.outputPath),
browserOptions.baseHref || '/',
browserOptions.ngswConfigPath,
);
}
})
.then(() => ({ success: true })),
);
}),
);
}
}
export default AppShellBuilder;