Skip to content

Commit e690b7c

Browse files
committed
fix(@angular-devkit/build-angular): always enable looseEnums build optimizer rule
With this change TypeScript enums are always processing in "loose" mode. Previously, this was only done for `@angular/` packages. (cherry picked from commit bf4229d)
1 parent dbb78f4 commit e690b7c

File tree

5 files changed

+18
-47
lines changed

5 files changed

+18
-47
lines changed

packages/angular_devkit/build_angular/src/babel/plugins/adjust-typescript-enums.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { NodePath, PluginObj, PluginPass, types } from '@babel/core';
9+
import { NodePath, PluginObj, types } from '@babel/core';
1010
import annotateAsPure from '@babel/helper-annotate-as-pure';
1111

1212
/**
@@ -27,10 +27,8 @@ export function getKeywords(): Iterable<string> {
2727
export default function (): PluginObj {
2828
return {
2929
visitor: {
30-
VariableDeclaration(path: NodePath<types.VariableDeclaration>, state: PluginPass) {
30+
VariableDeclaration(path: NodePath<types.VariableDeclaration>) {
3131
const { parentPath, node } = path;
32-
const { loose } = state.opts as { loose: boolean };
33-
3432
if (node.kind !== 'var' || node.declarations.length !== 1) {
3533
return;
3634
}
@@ -80,13 +78,8 @@ export default function (): PluginObj {
8078
const isEnumCalleeMatching =
8179
types.isIdentifier(enumCalleeParam) && enumCalleeParam.name === declarationId.name;
8280

83-
// Loose mode rewrites the enum to a shorter but less TypeScript-like form
84-
// Note: We only can apply the `loose` mode transformation if the callee parameter matches
85-
// with the declaration identifier name. This is necessary in case the the declaration id has
86-
// been renamed to avoid collisions, as the loose transform would then break the enum assignments
87-
// which rely on the differently-named callee identifier name.
8881
let enumAssignments: types.ExpressionStatement[] | undefined;
89-
if (loose && isEnumCalleeMatching) {
82+
if (isEnumCalleeMatching) {
9083
enumAssignments = [];
9184
}
9285

packages/angular_devkit/build_angular/src/babel/plugins/adjust-typescript-enums_spec.ts

+9-23
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@ import { default as adjustTypeScriptEnums } from './adjust-typescript-enums';
1212
// eslint-disable-next-line import/no-extraneous-dependencies
1313
const prettier = require('prettier');
1414

15-
function testCase({
16-
input,
17-
expected,
18-
options,
19-
}: {
20-
input: string;
21-
expected: string;
22-
options?: { loose?: boolean };
23-
}): void {
15+
function testCase({ input, expected }: { input: string; expected: string }): void {
2416
const result = transform(input, {
2517
configFile: false,
2618
babelrc: false,
27-
plugins: [[adjustTypeScriptEnums, options]],
19+
plugins: [[adjustTypeScriptEnums]],
2820
});
2921
if (!result) {
3022
fail('Expected babel to return a transform result.');
@@ -211,7 +203,7 @@ describe('adjust-typescript-enums Babel plugin', () => {
211203
`);
212204
});
213205

214-
it('wraps TypeScript enums in loose mode', () => {
206+
it('wraps TypeScript enums', () => {
215207
testCase({
216208
input: `
217209
var ChangeDetectionStrategy;
@@ -228,23 +220,19 @@ describe('adjust-typescript-enums Babel plugin', () => {
228220
return ChangeDetectionStrategy;
229221
})();
230222
`,
231-
options: { loose: true },
232223
});
233224
});
234225

235-
it(
236-
'should not wrap TypeScript enums in loose mode if the declaration identifier has been ' +
237-
'renamed to avoid collisions',
238-
() => {
239-
testCase({
240-
input: `
226+
it('should not wrap TypeScript enums if the declaration identifier has been renamed to avoid collisions', () => {
227+
testCase({
228+
input: `
241229
var ChangeDetectionStrategy$1;
242230
(function (ChangeDetectionStrategy) {
243231
ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
244232
ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";
245233
})(ChangeDetectionStrategy$1 || (ChangeDetectionStrategy$1 = {}));
246234
`,
247-
expected: `
235+
expected: `
248236
var ChangeDetectionStrategy$1 = /*#__PURE__*/ (() => {
249237
(function (ChangeDetectionStrategy) {
250238
ChangeDetectionStrategy[(ChangeDetectionStrategy["OnPush"] = 0)] = "OnPush";
@@ -253,8 +241,6 @@ describe('adjust-typescript-enums Babel plugin', () => {
253241
return ChangeDetectionStrategy$1;
254242
})();
255243
`,
256-
options: { loose: true },
257-
});
258-
},
259-
);
244+
});
245+
});
260246
});

packages/angular_devkit/build_angular/src/babel/presets/application.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ export interface ApplicationPresetOptions {
6868
inputSourceMap: unknown;
6969
};
7070
optimize?: {
71-
looseEnums: boolean;
7271
pureTopLevel: boolean;
7372
wrapDecorators: boolean;
7473
};
@@ -245,10 +244,7 @@ export default function (api: unknown, options: ApplicationPresetOptions) {
245244

246245
plugins.push(
247246
require('../plugins/elide-angular-metadata').default,
248-
[
249-
require('../plugins/adjust-typescript-enums').default,
250-
{ loose: options.optimize.looseEnums },
251-
],
247+
[require('../plugins/adjust-typescript-enums').default, { loose: true }],
252248
[
253249
require('../plugins/adjust-static-class-members').default,
254250
{ wrapDecorators: options.optimize.wrapDecorators },

packages/angular_devkit/build_angular/src/babel/webpack-loader.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,15 @@ export default custom<ApplicationPresetOptions>(() => {
154154
}
155155

156156
if (optimize) {
157-
// @angular/platform-server/init entry-point has side-effects.
158-
const safeAngularPackage =
159-
/[\\/]node_modules[\\/]@angular[\\/]/.test(this.resourcePath) &&
160-
!/@angular[\\/]platform-server[\\/]f?esm2022[\\/]init/.test(this.resourcePath);
157+
const AngularPackage = /[\\/]node_modules[\\/]@angular[\\/]/.test(this.resourcePath);
158+
const sideEffectFree = !!this._module?.factoryMeta?.sideEffectFree;
161159
customOptions.optimize = {
162160
// Angular packages provide additional tested side effects guarantees and can use
163-
// otherwise unsafe optimizations.
164-
looseEnums: safeAngularPackage,
165-
pureTopLevel: safeAngularPackage,
161+
// otherwise unsafe optimizations. (@angular/platform-server/init) however has side-effects.
162+
pureTopLevel: AngularPackage && sideEffectFree,
166163
// JavaScript modules that are marked as side effect free are considered to have
167164
// no decorators that contain non-local effects.
168-
wrapDecorators: !!this._module?.factoryMeta?.sideEffectFree,
165+
wrapDecorators: sideEffectFree,
169166
};
170167

171168
shouldProcess = true;

packages/angular_devkit/build_angular/src/builders/browser-esbuild/javascript-transformer-worker.ts

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ async function transformWithBabel({
8686
},
8787
forceAsyncTransformation,
8888
optimize: options.advancedOptimizations && {
89-
looseEnums: angularPackage,
9089
pureTopLevel: angularPackage,
9190
},
9291
},

0 commit comments

Comments
 (0)