Skip to content

Commit 2bed07d

Browse files
clydinmgechev
authored andcommitted
refactor(@angular-devkit/build-angular): improve debug optimize environment variables
`NG_BUILD_DEBUG_OPTIMIZE` when enabled will disable minify and mangle as well as enable beautify.
1 parent 0a7a49c commit 2bed07d

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ 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 { beautifyEnabled, cachingDisabled, manglingDisabled, minifyDisabled } from '../../../utils/environment-options';
33+
import {
34+
allowMangle,
35+
allowMinify,
36+
cachingDisabled,
37+
shouldBeautify,
38+
} from '../../../utils/environment-options';
3439
import { BundleBudgetPlugin } from '../../plugins/bundle-budget';
3540
import { NamedLazyChunksPlugin } from '../../plugins/named-chunks-plugin';
3641
import { OptimizeCssWebpackPlugin } from '../../plugins/optimize-css-webpack-plugin';
@@ -410,12 +415,12 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
410415
// default behavior (undefined value) is to keep only important comments (licenses, etc.)
411416
comments: !buildOptions.extractLicenses && undefined,
412417
webkit: true,
413-
beautify: beautifyEnabled,
418+
beautify: shouldBeautify,
414419
},
415420
// On server, we don't want to compress anything. We still set the ngDevMode = false for it
416421
// to remove dev code, and ngI18nClosureMode to remove Closure compiler i18n code
417422
compress:
418-
!minifyDisabled &&
423+
allowMinify &&
419424
(buildOptions.platform == 'server'
420425
? {
421426
ecma: terserEcma,
@@ -432,7 +437,7 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
432437
}),
433438
// We also want to avoid mangling on server.
434439
// Name mangling is handled within the browser builder
435-
mangle: !manglingDisabled && buildOptions.platform !== 'server' && !differentialLoadingMode,
440+
mangle: allowMangle && buildOptions.platform !== 'server' && !differentialLoadingMode,
436441
};
437442

438443
extraMinimizers.push(
@@ -456,15 +461,15 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
456461
globalScriptsByBundleName.some(s => s.bundleName === chunk.name),
457462
terserOptions: {
458463
...terserOptions,
459-
compress: !minifyDisabled && {
464+
compress: allowMinify && {
460465
...terserOptions.compress,
461466
ecma: 5,
462467
},
463468
output: {
464469
...terserOptions.output,
465470
ecma: 5,
466471
},
467-
mangle: !manglingDisabled && buildOptions.platform !== 'server',
472+
mangle: allowMangle && buildOptions.platform !== 'server',
468473
},
469474
}),
470475
);

packages/angular_devkit/build_angular/src/utils/action-cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { createHash } from 'crypto';
99
import * as fs from 'fs';
1010
import { copyFile } from './copy-file';
11-
import { manglingDisabled } from './environment-options';
11+
import { allowMangle } from './environment-options';
1212
import { CacheKey, ProcessBundleOptions, ProcessBundleResult } from './process-bundle';
1313

1414
const cacache = require('cacache');
@@ -42,7 +42,7 @@ export class BundleActionCache {
4242
.update(content)
4343
.digest('base64');
4444
let baseCacheKey = `${packageVersion}|${content.length}|${algorithm}-${codeHash}`;
45-
if (manglingDisabled) {
45+
if (!allowMangle) {
4646
baseCacheKey += '|MD';
4747
}
4848

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,50 @@ function isPresent(variable: string | undefined): variable is string {
1919
return typeof variable === 'string' && variable !== '';
2020
}
2121

22-
const mangleVariable = process.env['NG_BUILD_MANGLE'];
23-
export const manglingDisabled = isPresent(mangleVariable) && isDisabled(mangleVariable);
22+
const debugOptimizeVariable = process.env['NG_BUILD_DEBUG_OPTIMIZE'];
23+
const debugOptimize = (() => {
24+
if (!isPresent(debugOptimizeVariable) || isDisabled(debugOptimizeVariable)) {
25+
return {
26+
mangle: true,
27+
minify: true,
28+
beautify: false,
29+
};
30+
}
31+
32+
const debugValue = {
33+
mangle: false,
34+
minify: false,
35+
beautify: true,
36+
};
37+
38+
if (isEnabled(debugOptimizeVariable)) {
39+
return debugValue;
40+
}
2441

25-
const beautifyVariable = process.env['NG_BUILD_BEAUTIFY'];
26-
export const beautifyEnabled = isPresent(beautifyVariable) && !isDisabled(beautifyVariable);
42+
for (const part of debugOptimizeVariable.split(',')) {
43+
switch (part.trim().toLowerCase()) {
44+
case 'mangle':
45+
debugValue.mangle = true;
46+
break;
47+
case 'minify':
48+
debugValue.minify = true;
49+
break;
50+
case 'beautify':
51+
debugValue.beautify = true;
52+
break;
53+
}
54+
}
55+
56+
return debugValue;
57+
})();
58+
59+
const mangleVariable = process.env['NG_BUILD_MANGLE'];
60+
export const allowMangle = isPresent(mangleVariable)
61+
? !isDisabled(mangleVariable)
62+
: debugOptimize.mangle;
2763

28-
const minifyVariable = process.env['NG_BUILD_MINIFY'];
29-
export const minifyDisabled = isPresent(minifyVariable) && isDisabled(minifyVariable);
64+
export const shouldBeautify = debugOptimize.beautify;
65+
export const allowMinify = debugOptimize.minify;
3066

3167
const cacheVariable = process.env['NG_BUILD_CACHE'];
3268
export const cachingDisabled = isPresent(cacheVariable) && isDisabled(cacheVariable);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map'
2121
import { minify } from 'terser';
2222
import * as v8 from 'v8';
2323
import { SourceMapSource } from 'webpack-sources';
24-
import { beautifyEnabled, manglingDisabled, minifyDisabled } from './environment-options';
24+
import { allowMangle, allowMinify, shouldBeautify } from './environment-options';
2525
import { I18nOptions } from './i18n-options';
2626

2727
const cacache = require('cacache');
@@ -136,8 +136,8 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
136136
},
137137
]],
138138
plugins: options.replacements ? [createReplacePlugin(options.replacements)] : [],
139-
minified: !minifyDisabled && !!options.optimize,
140-
compact: !beautifyEnabled && !!options.optimize,
139+
minified: allowMinify && !!options.optimize,
140+
compact: !shouldBeautify && !!options.optimize,
141141
sourceMaps: !!sourceMap,
142142
});
143143

@@ -341,14 +341,14 @@ async function terserMangle(
341341

342342
// Mangle downlevel code
343343
const minifyOutput = minify(options.filename ? { [options.filename]: code } : code, {
344-
compress: !minifyDisabled && !!options.compress,
344+
compress: allowMinify && !!options.compress,
345345
ecma: options.ecma || 5,
346-
mangle: !manglingDisabled,
346+
mangle: allowMangle,
347347
safari10: true,
348348
output: {
349349
ascii_only: true,
350350
webkit: true,
351-
beautify: beautifyEnabled,
351+
beautify: shouldBeautify,
352352
},
353353
sourceMap:
354354
!!options.map &&

0 commit comments

Comments
 (0)