From 4d8de73421fb779138d3accf267fc61b41054c8b Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 27 Apr 2023 14:02:29 +0000 Subject: [PATCH] refactor(@angular-devkit/build-angular): exclude `@angular/platform-server/init` from unsafe optimizations in esbuild While currently esbuild is not use to bundle server bundles, it will in the future. This commit adds a check for the `@angular/platform-server/init` entry-point to be excluded from advanced optimizations. (cherry picked from commit 4a38e8ab35994f12413b93e71e8ae707abcbcae0) --- .../plugins/adjust-typescript-enums_spec.ts | 59 +++++++++---------- .../javascript-transformer-worker.ts | 7 ++- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/babel/plugins/adjust-typescript-enums_spec.ts b/packages/angular_devkit/build_angular/src/babel/plugins/adjust-typescript-enums_spec.ts index 1d5845700d6c..8efa5cf4cd9b 100644 --- a/packages/angular_devkit/build_angular/src/babel/plugins/adjust-typescript-enums_spec.ts +++ b/packages/angular_devkit/build_angular/src/babel/plugins/adjust-typescript-enums_spec.ts @@ -43,10 +43,9 @@ describe('adjust-typescript-enums Babel plugin', () => { `, expected: ` var ChangeDetectionStrategy = /*#__PURE__*/ (() => { - (function (ChangeDetectionStrategy) { - ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush"; - ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default"; - })(ChangeDetectionStrategy || (ChangeDetectionStrategy = {})); + ChangeDetectionStrategy = ChangeDetectionStrategy || {}; + ChangeDetectionStrategy[(ChangeDetectionStrategy["OnPush"] = 0)] = "OnPush"; + ChangeDetectionStrategy[(ChangeDetectionStrategy["Default"] = 1)] = "Default"; return ChangeDetectionStrategy; })(); `, @@ -64,10 +63,9 @@ describe('adjust-typescript-enums Babel plugin', () => { `, expected: ` export var ChangeDetectionStrategy = /*#__PURE__*/ (() => { - (function (ChangeDetectionStrategy) { - ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush"; - ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default"; - })(ChangeDetectionStrategy || (ChangeDetectionStrategy = {})); + ChangeDetectionStrategy = ChangeDetectionStrategy || {}; + ChangeDetectionStrategy[(ChangeDetectionStrategy["OnPush"] = 0)] = "OnPush"; + ChangeDetectionStrategy[(ChangeDetectionStrategy["Default"] = 1)] = "Default"; return ChangeDetectionStrategy; })(); `, @@ -85,10 +83,9 @@ describe('adjust-typescript-enums Babel plugin', () => { `, expected: ` export var ChangeDetectionStrategy = /*#__PURE__*/ (() => { - (function (ChangeDetectionStrategy) { - ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 5] = "OnPush"; - ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 8] = "Default"; - })(ChangeDetectionStrategy || (ChangeDetectionStrategy = {})); + ChangeDetectionStrategy = ChangeDetectionStrategy || {}; + ChangeDetectionStrategy[(ChangeDetectionStrategy["OnPush"] = 5)] = "OnPush"; + ChangeDetectionStrategy[(ChangeDetectionStrategy["Default"] = 8)] = "Default"; return ChangeDetectionStrategy; })(); `, @@ -98,20 +95,19 @@ describe('adjust-typescript-enums Babel plugin', () => { it('wraps string-based TypeScript enums', () => { testCase({ input: ` - var NotificationKind; - (function (NotificationKind) { - NotificationKind["NEXT"] = "N"; - NotificationKind["ERROR"] = "E"; - NotificationKind["COMPLETE"] = "C"; - })(NotificationKind || (NotificationKind = {})); + var NotificationKind; + (function (NotificationKind) { + NotificationKind["NEXT"] = "N"; + NotificationKind["ERROR"] = "E"; + NotificationKind["COMPLETE"] = "C"; + })(NotificationKind || (NotificationKind = {})); `, expected: ` var NotificationKind = /*#__PURE__*/ (() => { - (function (NotificationKind) { - NotificationKind["NEXT"] = "N"; - NotificationKind["ERROR"] = "E"; - NotificationKind["COMPLETE"] = "C"; - })(NotificationKind || (NotificationKind = {})); + NotificationKind = NotificationKind || {}; + NotificationKind["NEXT"] = "N"; + NotificationKind["ERROR"] = "E"; + NotificationKind["COMPLETE"] = "C"; return NotificationKind; })(); `, @@ -165,15 +161,14 @@ describe('adjust-typescript-enums Babel plugin', () => { * @deprecated use @angular/common/http instead */ var RequestMethod = /*#__PURE__*/ (() => { - (function (RequestMethod) { - RequestMethod[RequestMethod["Get"] = 0] = "Get"; - RequestMethod[RequestMethod["Post"] = 1] = "Post"; - RequestMethod[RequestMethod["Put"] = 2] = "Put"; - RequestMethod[RequestMethod["Delete"] = 3] = "Delete"; - RequestMethod[RequestMethod["Options"] = 4] = "Options"; - RequestMethod[RequestMethod["Head"] = 5] = "Head"; - RequestMethod[RequestMethod["Patch"] = 6] = "Patch"; - })(RequestMethod || (RequestMethod = {})); + RequestMethod = RequestMethod || {}; + RequestMethod[(RequestMethod["Get"] = 0)] = "Get"; + RequestMethod[(RequestMethod["Post"] = 1)] = "Post"; + RequestMethod[(RequestMethod["Put"] = 2)] = "Put"; + RequestMethod[(RequestMethod["Delete"] = 3)] = "Delete"; + RequestMethod[(RequestMethod["Options"] = 4)] = "Options"; + RequestMethod[(RequestMethod["Head"] = 5)] = "Head"; + RequestMethod[(RequestMethod["Patch"] = 6)] = "Patch"; return RequestMethod; })(); `, diff --git a/packages/angular_devkit/build_angular/src/builders/browser-esbuild/javascript-transformer-worker.ts b/packages/angular_devkit/build_angular/src/builders/browser-esbuild/javascript-transformer-worker.ts index 3a7cde1adbc3..1a4bc1106f60 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser-esbuild/javascript-transformer-worker.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser-esbuild/javascript-transformer-worker.ts @@ -55,7 +55,10 @@ async function transformWithBabel({ return useInputSourcemap ? data : data.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''); } - const angularPackage = /[\\/]node_modules[\\/]@angular[\\/]/.test(filename); + // @angular/platform-server/init entry-point has side-effects. + const safeAngularPackage = + /[\\/]node_modules[\\/]@angular[\\/]/.test(filename) && + !/@angular[\\/]platform-server[\\/]f?esm2022[\\/]init/.test(filename); // Lazy load the linker plugin only when linking is required if (shouldLink) { @@ -86,7 +89,7 @@ async function transformWithBabel({ }, forceAsyncTransformation, optimize: options.advancedOptimizations && { - pureTopLevel: angularPackage, + pureTopLevel: safeAngularPackage, }, }, ],