Skip to content

Commit ef552da

Browse files
filipesilvahansl
authored andcommitted
refactor(@angular-devkit/build-angular): use devkit/build-webpack
1 parent f8b5a99 commit ef552da

File tree

13 files changed

+182
-293
lines changed

13 files changed

+182
-293
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
"license-webpack-plugin": "^1.3.1",
114114
"loader-utils": "^1.1.0",
115115
"material-design-icons": "^3.0.1",
116-
"memory-fs": "^0.4.1",
117116
"mini-css-extract-plugin": "~0.4.0",
118117
"minimatch": "^3.0.4",
119118
"minimist": "^1.2.0",

packages/angular_devkit/build_angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@angular-devkit/architect": "0.0.0",
1313
"@angular-devkit/build-optimizer": "0.0.0",
14+
"@angular-devkit/build-webpack": "0.0.0",
1415
"@angular-devkit/core": "0.0.0",
1516
"@ngtools/webpack": "0.0.0",
1617
"ajv": "~6.4.0",
@@ -27,7 +28,6 @@
2728
"less": "^3.0.4",
2829
"less-loader": "^4.1.0",
2930
"license-webpack-plugin": "^1.3.1",
30-
"memory-fs": "^0.4.1",
3131
"mini-css-extract-plugin": "~0.4.0",
3232
"minimatch": "^3.0.4",
3333
"parse5": "^4.0.0",

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
253253
publicPath: buildOptions.deployUrl,
254254
filename: `[name]${hashFormat.chunk}.js`,
255255
},
256+
watch: buildOptions.watch,
257+
watchOptions: {
258+
poll: buildOptions.poll
259+
},
256260
performance: {
257261
hints: false,
258262
},

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export * from './styles';
1515
export * from './test';
1616
export * from './typescript';
1717
export * from './utils';
18+
export * from './stats';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { WebpackConfigOptions } from '../build-options';
10+
11+
const webpackOutputOptions = {
12+
colors: true,
13+
hash: true, // required by custom stat output
14+
timings: true, // required by custom stat output
15+
chunks: true, // required by custom stat output
16+
chunkModules: false,
17+
children: false, // listing all children is very noisy in AOT and hides warnings/errors
18+
modules: false,
19+
reasons: false,
20+
warnings: true,
21+
errors: true,
22+
assets: true, // required by custom stat output
23+
version: false,
24+
errorDetails: false,
25+
moduleTrace: false,
26+
};
27+
28+
const verboseWebpackOutputOptions = {
29+
children: true,
30+
assets: true,
31+
version: true,
32+
reasons: true,
33+
chunkModules: false, // TODO: set to true when console to file output is fixed
34+
errorDetails: true,
35+
moduleTrace: true,
36+
};
37+
38+
export function getWebpackStatsConfig(verbose = false) {
39+
return verbose
40+
? Object.assign(webpackOutputOptions, verboseWebpackOutputOptions)
41+
: webpackOutputOptions;
42+
}
43+
44+
export function getStatsConfig(wco: WebpackConfigOptions) {
45+
const verbose = !!wco.buildOptions.verbose;
46+
47+
return { stats: getWebpackStatsConfig(verbose) };
48+
}

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/utils.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,6 @@ export const ngAppResolve = (resolvePath: string): string => {
1616
return path.resolve(process.cwd(), resolvePath);
1717
};
1818

19-
const webpackOutputOptions = {
20-
colors: true,
21-
hash: true, // required by custom stat output
22-
timings: true, // required by custom stat output
23-
chunks: true, // required by custom stat output
24-
chunkModules: false,
25-
children: false, // listing all children is very noisy in AOT and hides warnings/errors
26-
modules: false,
27-
reasons: false,
28-
warnings: true,
29-
errors: true,
30-
assets: true, // required by custom stat output
31-
version: false,
32-
errorDetails: false,
33-
moduleTrace: false,
34-
};
35-
36-
const verboseWebpackOutputOptions = {
37-
children: true,
38-
assets: true,
39-
version: true,
40-
reasons: true,
41-
chunkModules: false, // TODO: set to true when console to file output is fixed
42-
errorDetails: true,
43-
moduleTrace: true,
44-
};
45-
46-
export function getWebpackStatsConfig(verbose = false) {
47-
return verbose
48-
? Object.assign(webpackOutputOptions, verboseWebpackOutputOptions)
49-
: webpackOutputOptions;
50-
}
51-
5219
export interface HashFormat {
5320
chunk: string;
5421
extract: string;

packages/angular_devkit/build_angular/src/browser/index.ts

Lines changed: 50 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ import {
1111
BuilderConfiguration,
1212
BuilderContext,
1313
} from '@angular-devkit/architect';
14+
import { LoggingCallback, WebpackBuilder } from '@angular-devkit/build-webpack';
1415
import { Path, getSystemPath, normalize, resolve, virtualFs } from '@angular-devkit/core';
1516
import * as fs from 'fs';
16-
import { Observable, concat, of } from 'rxjs';
17+
import { Observable, concat, of, throwError } from 'rxjs';
1718
import { concatMap, last, tap } from 'rxjs/operators';
1819
import * as ts from 'typescript'; // tslint:disable-line:no-implicit-dependencies
19-
import * as webpack from 'webpack';
2020
import { WebpackConfigOptions } from '../angular-cli-files/models/build-options';
2121
import {
2222
getAotConfig,
2323
getBrowserConfig,
2424
getCommonConfig,
2525
getNonAotConfig,
26+
getStatsConfig,
2627
getStylesConfig,
2728
} from '../angular-cli-files/models/webpack-configs';
28-
import { getWebpackStatsConfig } from '../angular-cli-files/models/webpack-configs/utils';
2929
import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig';
3030
import { requireProjectModule } from '../angular-cli-files/utilities/require-project-module';
3131
import { augmentAppWithServiceWorker } from '../angular-cli-files/utilities/service-worker';
@@ -58,6 +58,7 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
5858
const root = this.context.workspace.root;
5959
const projectRoot = resolve(root, builderConfig.root);
6060
const host = new virtualFs.AliasHost(this.context.host as virtualFs.Host<fs.Stats>);
61+
const webpackBuilder = new WebpackBuilder({ ...this.context, host });
6162

6263
return of(null).pipe(
6364
concatMap(() => options.deleteOutputPath
@@ -68,7 +69,7 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
6869
options.assets, host, root, projectRoot, builderConfig.sourceRoot)),
6970
// Replace the assets in options with the normalized version.
7071
tap((assetPatternObjects => options.assets = assetPatternObjects)),
71-
concatMap(() => new Observable(obs => {
72+
concatMap(() => {
7273
// Ensure Build Optimizer is only used with AOT.
7374
if (options.buildOptimizer && !options.aot) {
7475
throw new Error('The `--build-optimizer` option cannot be used without `--aot`.');
@@ -79,80 +80,35 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
7980
webpackConfig = this.buildWebpackConfig(root, projectRoot, host,
8081
options as NormalizedBrowserBuilderSchema);
8182
} catch (e) {
82-
obs.error(e);
83-
84-
return;
83+
return throwError(e);
8584
}
86-
const webpackCompiler = webpack(webpackConfig);
87-
const statsConfig = getWebpackStatsConfig(options.verbose);
88-
89-
const callback: webpack.compiler.CompilerCallback = (err, stats) => {
90-
if (err) {
91-
return obs.error(err);
92-
}
93-
94-
const json = stats.toJson(statsConfig);
95-
if (options.verbose) {
96-
this.context.logger.info(stats.toString(statsConfig));
97-
} else {
98-
this.context.logger.info(statsToString(json, statsConfig));
99-
}
100-
101-
if (stats.hasWarnings()) {
102-
this.context.logger.warn(statsWarningsToString(json, statsConfig));
103-
}
104-
if (stats.hasErrors()) {
105-
this.context.logger.error(statsErrorsToString(json, statsConfig));
106-
}
107-
108-
if (options.watch) {
109-
obs.next({ success: !stats.hasErrors() });
110-
111-
// Never complete on watch mode.
112-
return;
113-
} else {
114-
if (builderConfig.options.serviceWorker) {
115-
augmentAppWithServiceWorker(
116-
this.context.host,
117-
root,
118-
projectRoot,
119-
resolve(root, normalize(options.outputPath)),
120-
options.baseHref || '/',
121-
options.ngswConfigPath,
122-
).then(
123-
() => {
124-
obs.next({ success: !stats.hasErrors() });
125-
obs.complete();
126-
},
127-
(err: Error) => {
128-
// We error out here because we're not in watch mode anyway (see above).
129-
obs.error(err);
130-
},
131-
);
132-
} else {
133-
obs.next({ success: !stats.hasErrors() });
134-
obs.complete();
135-
}
136-
}
137-
};
13885

139-
try {
140-
if (options.watch) {
141-
const watching = webpackCompiler.watch({ poll: options.poll }, callback);
142-
143-
// Teardown logic. Close the watcher when unsubscribed from.
144-
return () => watching.close(() => { });
145-
} else {
146-
webpackCompiler.run(callback);
147-
}
148-
} catch (err) {
149-
if (err) {
150-
this.context.logger.error(
151-
'\nAn error occured during the build:\n' + ((err && err.stack) || err));
152-
}
153-
throw err;
86+
return webpackBuilder.runWebpack(webpackConfig, getBrowserLoggingCb(options.verbose));
87+
}),
88+
concatMap(buildEvent => {
89+
if (buildEvent.success && !options.watch && options.serviceWorker) {
90+
return new Observable(obs => {
91+
augmentAppWithServiceWorker(
92+
this.context.host,
93+
root,
94+
projectRoot,
95+
resolve(root, normalize(options.outputPath)),
96+
options.baseHref || '/',
97+
options.ngswConfigPath,
98+
).then(
99+
() => {
100+
obs.next({ success: true });
101+
obs.complete();
102+
},
103+
(err: Error) => {
104+
obs.error(err);
105+
},
106+
);
107+
});
108+
} else {
109+
return of(buildEvent);
154110
}
155-
})),
111+
}),
156112
);
157113
}
158114

@@ -185,6 +141,7 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
185141
getCommonConfig(wco),
186142
getBrowserConfig(wco),
187143
getStylesConfig(wco),
144+
getStatsConfig(wco),
188145
];
189146

190147
if (wco.buildOptions.main || wco.buildOptions.polyfills) {
@@ -213,4 +170,22 @@ export class BrowserBuilder implements Builder<BrowserBuilderSchema> {
213170
}
214171
}
215172

173+
export const getBrowserLoggingCb = (verbose: boolean): LoggingCallback =>
174+
(stats, config, logger) => {
175+
// config.stats contains our own stats settings, added during buildWebpackConfig().
176+
const json = stats.toJson(config.stats);
177+
if (verbose) {
178+
logger.info(stats.toString(config.stats));
179+
} else {
180+
logger.info(statsToString(json, config.stats));
181+
}
182+
183+
if (stats.hasWarnings()) {
184+
logger.warn(statsWarningsToString(json, config.stats));
185+
}
186+
if (stats.hasErrors()) {
187+
logger.error(statsErrorsToString(json, config.stats));
188+
}
189+
};
190+
216191
export default BrowserBuilder;

0 commit comments

Comments
 (0)