forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
365 lines (318 loc) · 11.3 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* @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 {
BuilderContext,
BuilderInfo,
BuilderOutput,
createBuilder,
} from '@angular-devkit/architect';
import { WebpackLoggingCallback, runWebpack } from '@angular-devkit/build-webpack';
import {
Path,
analytics,
experimental,
getSystemPath,
join,
json,
logging,
normalize,
resolve,
schema, virtualFs,
} from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import * as fs from 'fs';
import { Observable, combineLatest, from, of } from 'rxjs';
import { concatMap, map, switchMap } from 'rxjs/operators';
import * as ts from 'typescript';
import * as webpack from 'webpack';
import { NgBuildAnalyticsPlugin } from '../../plugins/webpack/analytics';
import { WebpackConfigOptions } from '../angular-cli-files/models/build-options';
import {
getAotConfig,
getBrowserConfig,
getCommonConfig,
getEsVersionForFileName,
getNonAotConfig,
getStatsConfig,
getStylesConfig,
getWorkerConfig,
} from '../angular-cli-files/models/webpack-configs';
import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig';
import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker';
import {
statsErrorsToString,
statsToString,
statsWarningsToString,
} from '../angular-cli-files/utilities/stats';
import {
NormalizedBrowserBuilderSchema,
defaultProgress,
deleteOutputDir,
normalizeBrowserSchema,
} from '../utils';
import { Schema as BrowserBuilderSchema } from './schema';
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const webpackMerge = require('webpack-merge');
export type BrowserBuilderOutput = json.JsonObject & BuilderOutput & {
outputPath: string;
};
export function createBrowserLoggingCallback(
verbose: boolean,
logger: logging.LoggerApi,
): WebpackLoggingCallback {
return (stats, config) => {
// config.stats contains our own stats settings, added during buildWebpackConfig().
const json = stats.toJson(config.stats);
if (verbose) {
logger.info(stats.toString(config.stats));
} else {
logger.info(statsToString(json, config.stats));
}
if (stats.hasWarnings()) {
logger.warn(statsWarningsToString(json, config.stats));
}
if (stats.hasErrors()) {
logger.error(statsErrorsToString(json, config.stats));
}
};
}
export function buildWebpackConfig(
root: Path,
projectRoot: Path,
options: NormalizedBrowserBuilderSchema,
additionalOptions: {
logger?: logging.LoggerApi,
analytics?: analytics.Analytics,
builderInfo?: BuilderInfo,
} = {},
): webpack.Configuration[] {
// Ensure Build Optimizer is only used with AOT.
if (options.buildOptimizer && !options.aot) {
throw new Error(`The 'buildOptimizer' option cannot be used without 'aot'.`);
}
let wco: WebpackConfigOptions<NormalizedBrowserBuilderSchema>;
const tsConfigPath = getSystemPath(normalize(resolve(root, normalize(options.tsConfig))));
const tsConfig = readTsconfig(tsConfigPath);
const logger = additionalOptions.logger
? additionalOptions.logger.createChild('webpackConfigOptions')
: new logging.NullLogger();
const scriptTarget = tsConfig.options.target;
// todo enabe when differential loading is complete
// const differentialLoading = isDifferentialLoadingNeeded(projectRoot, scriptTarget);
const differentialLoading = false;
const scriptTargets = differentialLoading ? [ts.ScriptTarget.ES5, scriptTarget] : [scriptTarget];
// For differential loading, we can have several targets
return scriptTargets.map(scriptTarget => {
let buildOptions: NormalizedBrowserBuilderSchema = { ...options };
if (differentialLoading) {
// For differential loading, the builder needs to created the index.html by itself
// without using a webpack plugin.
buildOptions = {
...options,
es5BrowserSupport: undefined,
index: '',
esVersionInFileName: true,
scriptTargetOverride: scriptTarget,
};
}
const supportES2015
= scriptTarget !== ts.ScriptTarget.ES3 && scriptTarget !== ts.ScriptTarget.ES5;
wco = {
root: getSystemPath(root),
logger,
projectRoot: getSystemPath(projectRoot),
buildOptions: buildOptions,
tsConfig,
tsConfigPath,
supportES2015,
};
wco.buildOptions.progress = defaultProgress(wco.buildOptions.progress);
const webpackConfigs: {}[] = [
getCommonConfig(wco),
getBrowserConfig(wco),
getStylesConfig(wco),
getStatsConfig(wco),
getWorkerConfig(wco),
];
if (wco.buildOptions.main || wco.buildOptions.polyfills) {
const typescriptConfigPartial = wco.buildOptions.aot
? getAotConfig(wco)
: getNonAotConfig(wco);
webpackConfigs.push(typescriptConfigPartial);
}
if (additionalOptions.analytics) {
// If there's analytics, add our plugin. Otherwise no need to slow down the build.
let category = 'build';
if (additionalOptions.builderInfo) {
// We already vetted that this is a "safe" package, otherwise the analytics would be noop.
category = additionalOptions.builderInfo.builderName.split(':')[1];
}
// The category is the builder name if it's an angular builder.
webpackConfigs.push({
plugins: [
new NgBuildAnalyticsPlugin(wco.projectRoot, additionalOptions.analytics, category),
],
});
}
const webpackConfig = webpackMerge(webpackConfigs);
if (options.profile || process.env['NG_BUILD_PROFILING']) {
const esVersionInFileName = getEsVersionForFileName(
wco.buildOptions.scriptTargetOverride,
wco.buildOptions.esVersionInFileName,
);
const smp = new SpeedMeasurePlugin({
outputFormat: 'json',
outputTarget: getSystemPath(join(root,
`speed-measure-plugin${esVersionInFileName}.json`)),
});
return smp.wrap(webpackConfig);
}
return webpackConfig;
});
}
export async function buildBrowserWebpackConfigFromWorkspace(
options: BrowserBuilderSchema,
projectName: string,
workspace: experimental.workspace.Workspace,
host: virtualFs.Host<fs.Stats>,
additionalOptions: {
logger?: logging.LoggerApi,
analytics?: analytics.Analytics,
builderInfo?: BuilderInfo,
} = {},
): Promise<webpack.Configuration[]> {
// TODO: Use a better interface for workspace access.
const projectRoot = resolve(workspace.root, normalize(workspace.getProject(projectName).root));
const sourceRoot = workspace.getProject(projectName).sourceRoot;
const normalizedOptions = normalizeBrowserSchema(
host,
workspace.root,
projectRoot,
sourceRoot ? resolve(workspace.root, normalize(sourceRoot)) : undefined,
options,
);
return buildWebpackConfig(workspace.root, projectRoot, normalizedOptions, additionalOptions);
}
export async function buildBrowserWebpackConfigFromContext(
options: BrowserBuilderSchema,
context: BuilderContext,
host: virtualFs.Host<fs.Stats>,
): Promise<{ workspace: experimental.workspace.Workspace, config: webpack.Configuration[] }> {
const registry = new schema.CoreSchemaRegistry();
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
const workspace = await experimental.workspace.Workspace.fromPath(
host,
normalize(context.workspaceRoot),
registry,
);
const projectName = context.target ? context.target.project : workspace.getDefaultProjectName();
if (!projectName) {
throw new Error('Must either have a target from the context or a default project.');
}
const config = await buildBrowserWebpackConfigFromWorkspace(
options,
projectName,
workspace,
host,
{
logger: context.logger,
analytics: context.analytics,
builderInfo: context.builder,
},
);
return { workspace, config };
}
export type BrowserConfigTransformFn = (
workspace: experimental.workspace.Workspace,
config: webpack.Configuration,
) => Observable<webpack.Configuration>;
export function buildWebpackBrowser(
options: BrowserBuilderSchema,
context: BuilderContext,
transforms: {
config?: BrowserConfigTransformFn,
output?: (output: BrowserBuilderOutput) => Observable<BuilderOutput>,
logging?: WebpackLoggingCallback,
} = {},
) {
const host = new NodeJsSyncHost();
const root = normalize(context.workspaceRoot);
const configFn = transforms.config;
const outputFn = transforms.output;
const loggingFn = transforms.logging
|| createBrowserLoggingCallback(!!options.verbose, context.logger);
// This makes a host observable into a cold one. This is because we want to wait until
// subscription before calling buildBrowserWebpackConfigFromContext, which can throw.
return of(null).pipe(
switchMap(() => from(buildBrowserWebpackConfigFromContext(options, context, host))),
switchMap(({ workspace, config }) => {
if (configFn) {
return combineLatest(config.map(config => configFn(workspace, config))).pipe(
map(config => ({ workspace, config })),
);
} else {
return of({ workspace, config });
}
}),
switchMap(({ workspace, config }) => {
if (options.deleteOutputPath) {
return deleteOutputDir(
normalize(context.workspaceRoot),
normalize(options.outputPath),
host,
).pipe(map(() => ({ workspace, config })));
} else {
return of({ workspace, config });
}
}),
switchMap(({ workspace, config: configs }) => {
const projectName = context.target
? context.target.project : workspace.getDefaultProjectName();
if (!projectName) {
throw new Error('Must either have a target from the context or a default project.');
}
const projectRoot = resolve(
workspace.root,
normalize(workspace.getProject(projectName).root),
);
return combineLatest(
configs.map(config => runWebpack(config, context, { logging: loggingFn })),
)
.pipe(
switchMap(buildEvents => {
if (buildEvents.length === 2) {
// todo implement writing index.html for differential loading in another PR
}
return of(buildEvents);
}),
map(buildEvents => ({ success: buildEvents.every(r => r.success) })),
concatMap(buildEvent => {
if (buildEvent.success && !options.watch && options.serviceWorker) {
return from(augmentAppWithServiceWorker(
host,
root,
projectRoot,
resolve(root, normalize(options.outputPath)),
options.baseHref || '/',
options.ngswConfigPath,
).then(() => ({ success: true }), () => ({ success: false })));
} else {
return of(buildEvent);
}
}),
map(event => ({
...event,
// If we use differential loading, both configs have the same outputs
outputPath: getSystemPath(join(normalize(context.workspaceRoot), options.outputPath)),
} as BrowserBuilderOutput)),
concatMap(output => outputFn ? outputFn(output) : of(output)),
);
}),
);
}
export default createBuilder<json.JsonObject & BrowserBuilderSchema>(buildWebpackBrowser);