Skip to content

Commit 5a1b190

Browse files
clydinKeen Yee Liau
authored and
Keen Yee Liau
committed
refactor(@angular-devkit/build-angular): add additional debug build environment variables
`NG_BUILD_MINIFY` can be used to separately disable minification (terser's compress) `NG_BUILD_BEAUTIFY` can be used to format the output code even when otherwise optimized
1 parent 9a7d8e3 commit 5a1b190

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { RawSource } from 'webpack-sources';
3030
import { AssetPatternClass, ExtraEntryPoint } from '../../../browser/schema';
3131
import { BuildBrowserFeatures } from '../../../utils';
3232
import { findCachePath } from '../../../utils/cache-path';
33-
import { cachingDisabled, manglingDisabled } from '../../../utils/environment-options';
33+
import { beautifyEnabled, cachingDisabled, manglingDisabled, minifyDisabled } from '../../../utils/environment-options';
3434
import { BundleBudgetPlugin } from '../../plugins/bundle-budget';
3535
import { NamedLazyChunksPlugin } from '../../plugins/named-chunks-plugin';
3636
import { OptimizeCssWebpackPlugin } from '../../plugins/optimize-css-webpack-plugin';
@@ -410,11 +410,13 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
410410
// default behavior (undefined value) is to keep only important comments (licenses, etc.)
411411
comments: !buildOptions.extractLicenses && undefined,
412412
webkit: true,
413+
beautify: beautifyEnabled,
413414
},
414415
// On server, we don't want to compress anything. We still set the ngDevMode = false for it
415416
// to remove dev code, and ngI18nClosureMode to remove Closure compiler i18n code
416417
compress:
417-
buildOptions.platform == 'server'
418+
!minifyDisabled &&
419+
(buildOptions.platform == 'server'
418420
? {
419421
ecma: terserEcma,
420422
global_defs: angularGlobalDefinitions,
@@ -427,13 +429,10 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
427429
// See https://github.com/webpack/webpack/issues/2899#issuecomment-317425926.
428430
passes: buildOptions.buildOptimizer ? 3 : 1,
429431
global_defs: angularGlobalDefinitions,
430-
},
432+
}),
431433
// We also want to avoid mangling on server.
432434
// Name mangling is handled within the browser builder
433-
mangle:
434-
!manglingDisabled &&
435-
buildOptions.platform !== 'server' &&
436-
!differentialLoadingMode,
435+
mangle: !manglingDisabled && buildOptions.platform !== 'server' && !differentialLoadingMode,
437436
};
438437

439438
extraMinimizers.push(
@@ -457,7 +456,7 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
457456
globalScriptsByBundleName.some(s => s.bundleName === chunk.name),
458457
terserOptions: {
459458
...terserOptions,
460-
compress: {
459+
compress: !minifyDisabled && {
461460
...terserOptions.compress,
462461
ecma: 5,
463462
},

packages/angular_devkit/build_angular/src/utils/environment-options.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,31 @@
77
*/
88
import * as path from 'path';
99

10+
function isDisabled(variable: string): boolean {
11+
return variable === '0' || variable.toLowerCase() === 'false';
12+
}
13+
14+
function isEnabled(variable: string): boolean {
15+
return variable === '1' || variable.toLowerCase() === 'true';
16+
}
17+
18+
function isPresent(variable: string | undefined): variable is string {
19+
return typeof variable === 'string' && variable !== '';
20+
}
21+
1022
const mangleVariable = process.env['NG_BUILD_MANGLE'];
11-
export const manglingDisabled =
12-
!!mangleVariable && (mangleVariable === '0' || mangleVariable.toLowerCase() === 'false');
23+
export const manglingDisabled = isPresent(mangleVariable) && isDisabled(mangleVariable);
24+
25+
const beautifyVariable = process.env['NG_BUILD_BEAUTIFY'];
26+
export const beautifyEnabled = isPresent(beautifyVariable) && !isDisabled(beautifyVariable);
27+
28+
const minifyVariable = process.env['NG_BUILD_MINIFY'];
29+
export const minifyDisabled = isPresent(minifyVariable) && isDisabled(minifyVariable);
1330

1431
const cacheVariable = process.env['NG_BUILD_CACHE'];
15-
export const cachingDisabled =
16-
!!cacheVariable && (cacheVariable === '0' || cacheVariable.toLowerCase() === 'false');
32+
export const cachingDisabled = isPresent(cacheVariable) && isDisabled(cacheVariable);
1733
export const cachingBasePath = (() => {
18-
if (
19-
cachingDisabled ||
20-
!cacheVariable ||
21-
(cacheVariable === '1' || cacheVariable.toLowerCase() === 'true')
22-
) {
34+
if (cachingDisabled || !isPresent(cacheVariable) || isEnabled(cacheVariable)) {
2335
return null;
2436
}
2537
if (!path.isAbsolute(cacheVariable)) {

packages/angular_devkit/build_angular/src/utils/process-bundle.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { RawSourceMap } from 'source-map';
2121
import { minify } from 'terser';
2222
import * as v8 from 'v8';
2323
import { SourceMapSource } from 'webpack-sources';
24-
import { manglingDisabled } from './environment-options';
24+
import { beautifyEnabled, manglingDisabled, minifyDisabled } from './environment-options';
2525
import { I18nOptions } from './i18n-options';
2626

2727
const cacache = require('cacache');
@@ -132,9 +132,8 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
132132
},
133133
]],
134134
plugins: options.replacements ? [createReplacePlugin(options.replacements)] : [],
135-
minified: options.optimize,
136-
// `false` ensures it is disabled and prevents large file warnings
137-
compact: options.optimize || false,
135+
minified: !minifyDisabled && !!options.optimize,
136+
compact: !beautifyEnabled && !!options.optimize,
138137
sourceMaps: !!sourceMap,
139138
});
140139

@@ -275,13 +274,14 @@ function terserMangle(
275274

276275
// Mangle downlevel code
277276
const minifyOutput = minify(options.filename ? { [options.filename]: code } : code, {
278-
compress: options.compress || false,
277+
compress: !minifyDisabled && !!options.compress,
279278
ecma: options.ecma || 5,
280279
mangle: !manglingDisabled,
281280
safari10: true,
282281
output: {
283282
ascii_only: true,
284283
webkit: true,
284+
beautify: beautifyEnabled,
285285
},
286286
sourceMap:
287287
!!options.map &&

0 commit comments

Comments
 (0)