@@ -11,50 +11,60 @@ import type { TransformFailure, TransformResult } from 'esbuild';
11
11
import { minify } from 'terser' ;
12
12
import { EsbuildExecutor } from './esbuild-executor' ;
13
13
14
+ /**
15
+ * The options to use when optimizing.
16
+ */
17
+ export interface OptimizeRequestOptions {
18
+ /**
19
+ * Controls advanced optimizations.
20
+ * Currently these are only terser related:
21
+ * * terser compress passes are set to 2
22
+ * * terser pure_getters option is enabled
23
+ */
24
+ advanced ?: boolean ;
25
+ /**
26
+ * Specifies the string tokens that should be replaced with a defined value.
27
+ */
28
+ define ?: Record < string , string > ;
29
+ /**
30
+ * Controls whether class, function, and variable names should be left intact
31
+ * throughout the output code.
32
+ */
33
+ keepIdentifierNames : boolean ;
34
+
35
+ /**
36
+ * Controls whether to retain the original name of classes and functions.
37
+ */
38
+ keepNames : boolean ;
39
+ /**
40
+ * Controls whether license text is removed from the output code.
41
+ * Within the CLI, this option is linked to the license extraction functionality.
42
+ */
43
+ removeLicenses ?: boolean ;
44
+ /**
45
+ * Controls whether source maps should be generated.
46
+ */
47
+ sourcemap ?: boolean ;
48
+ /**
49
+ * Specifies the target ECMAScript version for the output code.
50
+ */
51
+ target : 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 ;
52
+ /**
53
+ * Controls whether esbuild should only use the WASM-variant instead of trying to
54
+ * use the native variant. Some platforms may not support the native-variant and
55
+ * this option allows one support test to be conducted prior to all the workers starting.
56
+ */
57
+ alwaysUseWasm : boolean ;
58
+ }
59
+
14
60
/**
15
61
* A request to optimize JavaScript using the supplied options.
16
62
*/
17
63
interface OptimizeRequest {
18
64
/**
19
65
* The options to use when optimizing.
20
66
*/
21
- options : {
22
- /**
23
- * Controls advanced optimizations.
24
- * Currently these are only terser related:
25
- * * terser compress passes are set to 2
26
- * * terser pure_getters option is enabled
27
- */
28
- advanced : boolean ;
29
- /**
30
- * Specifies the string tokens that should be replaced with a defined value.
31
- */
32
- define ?: Record < string , string > ;
33
- /**
34
- * Controls whether class, function, and variable names should be left intact
35
- * throughout the output code.
36
- */
37
- keepNames : boolean ;
38
- /**
39
- * Controls whether license text is removed from the output code.
40
- * Within the CLI, this option is linked to the license extraction functionality.
41
- */
42
- removeLicenses : boolean ;
43
- /**
44
- * Controls whether source maps should be generated.
45
- */
46
- sourcemap : boolean ;
47
- /**
48
- * Specifies the target ECMAScript version for the output code.
49
- */
50
- target : 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 ;
51
- /**
52
- * Controls whether esbuild should only use the WASM-variant instead of trying to
53
- * use the native variant. Some platforms may not support the native-variant and
54
- * this option allows one support test to be conducted prior to all the workers starting.
55
- */
56
- alwaysUseWasm : boolean ;
57
- } ;
67
+ options : OptimizeRequestOptions ;
58
68
59
69
/**
60
70
* The JavaScript asset to optimize.
@@ -142,7 +152,7 @@ async function optimizeWithEsbuild(
142
152
let result : TransformResult ;
143
153
try {
144
154
result = await esbuild . transform ( content , {
145
- minifyIdentifiers : ! options . keepNames ,
155
+ minifyIdentifiers : ! options . keepIdentifierNames ,
146
156
minifySyntax : true ,
147
157
// NOTE: Disabling whitespace ensures unused pure annotations are kept
148
158
minifyWhitespace : false ,
@@ -151,6 +161,10 @@ async function optimizeWithEsbuild(
151
161
sourcefile : name ,
152
162
sourcemap : options . sourcemap && 'external' ,
153
163
define : options . define ,
164
+ // This option should always be disabled for browser builds as we don't rely on `.name`
165
+ // and causes deadcode to be retained which makes `NG_BUILD_MANGLE` unusable to investigate tree-shaking issues.
166
+ // We enable `keepNames` only for server builds as Domino relies on `.name`.
167
+ // Once we no longer rely on Domino for SSR we should be able to remove this.
154
168
keepNames : options . keepNames ,
155
169
target : `es${ options . target } ` ,
156
170
} ) ;
@@ -193,9 +207,9 @@ async function optimizeWithEsbuild(
193
207
async function optimizeWithTerser (
194
208
name : string ,
195
209
code : string ,
196
- sourcemaps : boolean ,
210
+ sourcemaps : boolean | undefined ,
197
211
target : OptimizeRequest [ 'options' ] [ 'target' ] ,
198
- advanced : boolean ,
212
+ advanced : boolean | undefined ,
199
213
) : Promise < { code : string ; map ?: object } > {
200
214
const result = await minify (
201
215
{ [ name ] : code } ,
@@ -207,6 +221,8 @@ async function optimizeWithTerser(
207
221
ecma : target ,
208
222
// esbuild in the first pass is used to minify identifiers instead of mangle here
209
223
mangle : false ,
224
+ // esbuild in the first pass is used to minify function names
225
+ keep_fnames : true ,
210
226
format : {
211
227
// ASCII output is enabled here as well to prevent terser from converting back to UTF-8
212
228
ascii_only : true ,
0 commit comments