Skip to content

Commit 662a2b8

Browse files
alan-agius4filipesilva
authored andcommitted
fix(@angular-devkit/build-angular): provided earlier build feedback in console
With this change we provide earlier feedback in the console during a build. The setup phase can take some times if the project has a large set of dependencies. We also move several warnings in the dev-server to an earlier phase so that we don't clutter the progress indicator. Closes #20957 (cherry picked from commit f9acdc7)
1 parent 6c26421 commit 662a2b8

File tree

2 files changed

+97
-98
lines changed

2 files changed

+97
-98
lines changed

packages/angular_devkit/build_angular/src/dev-server/index.ts

+61-61
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ const devServerBuildOverriddenKeys: (keyof DevServerBuilderOptions)[] = [
6565
'deployUrl',
6666
];
6767

68+
// Get dev-server only options.
69+
type DevServerOptions = Partial<
70+
Omit<
71+
Schema,
72+
| 'watch'
73+
| 'optimization'
74+
| 'aot'
75+
| 'sourceMap'
76+
| 'vendorChunk'
77+
| 'commonChunk'
78+
| 'baseHref'
79+
| 'progress'
80+
| 'poll'
81+
| 'verbose'
82+
| 'deployUrl'
83+
>
84+
>;
85+
6886
/**
6987
* @experimental Direct usage of this type is considered experimental.
7088
*/
@@ -119,23 +137,6 @@ export function serveWebpackBrowser(
119137
{},
120138
);
121139

122-
// Get dev-server only options.
123-
type DevServerOptions = Partial<
124-
Omit<
125-
Schema,
126-
| 'watch'
127-
| 'optimization'
128-
| 'aot'
129-
| 'sourceMap'
130-
| 'vendorChunk'
131-
| 'commonChunk'
132-
| 'baseHref'
133-
| 'progress'
134-
| 'poll'
135-
| 'verbose'
136-
| 'deployUrl'
137-
>
138-
>;
139140
const devServerOptions: DevServerOptions = (Object.keys(options) as (keyof Schema)[])
140141
.filter((key) => !devServerBuildOverriddenKeys.includes(key) && key !== 'browserTarget')
141142
.reduce<DevServerOptions>(
@@ -156,6 +157,36 @@ export function serveWebpackBrowser(
156157
logger.warn(`Warning: 'outputHashing' option is disabled when using the dev-server.`);
157158
}
158159

160+
if (options.hmr) {
161+
logger.warn(tags.stripIndents`NOTICE: Hot Module Replacement (HMR) is enabled for the dev server.
162+
See https://webpack.js.org/guides/hot-module-replacement for information on working with HMR for Webpack.`);
163+
}
164+
165+
if (
166+
!options.disableHostCheck &&
167+
options.host &&
168+
!/^127\.\d+\.\d+\.\d+/g.test(options.host) &&
169+
options.host !== 'localhost'
170+
) {
171+
logger.warn(tags.stripIndent`
172+
Warning: This is a simple server for use in testing or debugging Angular applications
173+
locally. It hasn't been reviewed for security issues.
174+
175+
Binding this server to an open connection can result in compromising your application or
176+
computer. Using a different host than the one passed to the "--host" flag might result in
177+
websocket connection issues. You might need to use "--disableHostCheck" if that's the
178+
case.
179+
`);
180+
}
181+
182+
if (options.disableHostCheck) {
183+
logger.warn(tags.oneLine`
184+
Warning: Running a server with --disable-host-check is a security risk.
185+
See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a
186+
for more information.
187+
`);
188+
}
189+
159190
// Webpack's live reload functionality adds the `strip-ansi` package which is commonJS
160191
rawBrowserOptions.allowedCommonJsDependencies ??= [];
161192
rawBrowserOptions.allowedCommonJsDependencies.push('strip-ansi');
@@ -166,6 +197,18 @@ export function serveWebpackBrowser(
166197
browserName,
167198
)) as json.JsonObject & BrowserBuilderSchema;
168199

200+
const { styles, scripts } = normalizeOptimization(browserOptions.optimization);
201+
if (scripts || styles.minify) {
202+
logger.error(tags.stripIndents`
203+
****************************************************************************************
204+
This is a simple server for use in testing or debugging Angular applications locally.
205+
It hasn't been reviewed for security issues.
206+
207+
DON'T USE IT FOR PRODUCTION!
208+
****************************************************************************************
209+
`);
210+
}
211+
169212
const { config, projectRoot, i18n } = await generateI18nBrowserWebpackConfigFromContext(
170213
browserOptions,
171214
context,
@@ -220,36 +263,6 @@ export function serveWebpackBrowser(
220263
}
221264
}
222265

223-
if (options.hmr) {
224-
logger.warn(tags.stripIndents`NOTICE: Hot Module Replacement (HMR) is enabled for the dev server.
225-
See https://webpack.js.org/guides/hot-module-replacement for information on working with HMR for Webpack.`);
226-
}
227-
228-
if (
229-
!options.disableHostCheck &&
230-
options.host &&
231-
!/^127\.\d+\.\d+\.\d+/g.test(options.host) &&
232-
options.host !== 'localhost'
233-
) {
234-
logger.warn(tags.stripIndent`
235-
Warning: This is a simple server for use in testing or debugging Angular applications
236-
locally. It hasn't been reviewed for security issues.
237-
238-
Binding this server to an open connection can result in compromising your application or
239-
computer. Using a different host than the one passed to the "--host" flag might result in
240-
websocket connection issues. You might need to use "--disableHostCheck" if that's the
241-
case.
242-
`);
243-
}
244-
245-
if (options.disableHostCheck) {
246-
logger.warn(tags.oneLine`
247-
Warning: Running a server with --disable-host-check is a security risk.
248-
See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a
249-
for more information.
250-
`);
251-
}
252-
253266
let locale: string | undefined;
254267
if (i18n.shouldInline) {
255268
// Dev-server only supports one locale
@@ -290,8 +303,6 @@ export function serveWebpackBrowser(
290303

291304
return from(setup()).pipe(
292305
switchMap(({ browserOptions, webpackConfig, projectRoot, locale }) => {
293-
const normalizedOptimization = normalizeOptimization(browserOptions.optimization);
294-
295306
if (browserOptions.index) {
296307
const { scripts = [], styles = [], baseHref, tsConfig } = browserOptions;
297308
const { options: compilerOptions } = readTsconfig(tsConfig, workspaceRoot);
@@ -315,25 +326,14 @@ export function serveWebpackBrowser(
315326
deployUrl: browserOptions.deployUrl,
316327
sri: browserOptions.subresourceIntegrity,
317328
postTransform: transforms.indexHtml,
318-
optimization: normalizedOptimization,
329+
optimization: normalizeOptimization(browserOptions.optimization),
319330
WOFFSupportNeeded: !buildBrowserFeatures.isFeatureSupported('woff2'),
320331
crossOrigin: browserOptions.crossOrigin,
321332
lang: locale,
322333
}),
323334
);
324335
}
325336

326-
if (normalizedOptimization.scripts || normalizedOptimization.styles.minify) {
327-
logger.error(tags.stripIndents`
328-
****************************************************************************************
329-
This is a simple server for use in testing or debugging Angular applications locally.
330-
It hasn't been reviewed for security issues.
331-
332-
DON'T USE IT FOR PRODUCTION!
333-
****************************************************************************************
334-
`);
335-
}
336-
337337
return runWebpackDevServer(webpackConfig, context, {
338338
logging: transforms.logging || createWebpackLoggingCallback(!!options.verbose, logger),
339339
webpackFactory: require('webpack') as typeof webpack,

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

+36-37
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,42 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
6868
buildOptions.differentialLoadingNeeded,
6969
);
7070

71+
if (buildOptions.progress) {
72+
const spinner = new Spinner();
73+
spinner.start(`Generating ${platform} application bundles (phase: setup)...`);
74+
75+
let previousPercentage: number | undefined;
76+
extraPlugins.push(
77+
new ProgressPlugin({
78+
handler: (percentage: number, message: string) => {
79+
if (previousPercentage === 1 && percentage !== 0) {
80+
// In some scenarios in Webpack 5 percentage goes from 1 back to 0.99.
81+
// Ex: 0.99 -> 1 -> 0.99 -> 1
82+
// This causes the "complete" message to be displayed multiple times.
83+
84+
return;
85+
}
86+
87+
switch (percentage) {
88+
case 1:
89+
spinner.succeed(
90+
`${platform.replace(/^\w/, (s) =>
91+
s.toUpperCase(),
92+
)} application bundle generation complete.`,
93+
);
94+
break;
95+
case 0:
96+
default:
97+
spinner.text = `Generating ${platform} application bundles (phase: ${message})...`;
98+
break;
99+
}
100+
101+
previousPercentage = percentage;
102+
},
103+
}),
104+
);
105+
}
106+
71107
if (buildOptions.main) {
72108
const mainPath = path.resolve(root, buildOptions.main);
73109
entryPoints['main'] = [mainPath];
@@ -216,43 +252,6 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
216252
);
217253
}
218254

219-
if (buildOptions.progress) {
220-
const spinner = new Spinner();
221-
222-
let previousPercentage: number | undefined;
223-
extraPlugins.push(
224-
new ProgressPlugin({
225-
handler: (percentage: number, message: string) => {
226-
if (previousPercentage === 1 && percentage !== 0) {
227-
// In some scenarios in Webpack 5 percentage goes from 1 back to 0.99.
228-
// Ex: 0.99 -> 1 -> 0.99 -> 1
229-
// This causes the "complete" message to be displayed multiple times.
230-
231-
return;
232-
}
233-
234-
switch (percentage) {
235-
case 0:
236-
spinner.start(`Generating ${platform} application bundles...`);
237-
break;
238-
case 1:
239-
spinner.succeed(
240-
`${platform.replace(/^\w/, (s) =>
241-
s.toUpperCase(),
242-
)} application bundle generation complete.`,
243-
);
244-
break;
245-
default:
246-
spinner.text = `Generating ${platform} application bundles (phase: ${message})...`;
247-
break;
248-
}
249-
250-
previousPercentage = percentage;
251-
},
252-
}),
253-
);
254-
}
255-
256255
if (buildOptions.showCircularDependencies) {
257256
const CircularDependencyPlugin = require('circular-dependency-plugin');
258257
extraPlugins.push(

0 commit comments

Comments
 (0)