Skip to content

Commit 7de7c52

Browse files
alan-agius4alxhub
authored andcommitted
build: remove usages of useDefineForClassFields: false (angular#58297)
When setting `"useDefineForClassFields": false`, static fields are compiled within a block that relies on the `this` context. This output makes it more difficult for bundlers to treeshake and eliminate unused code. PR Close angular#58297
1 parent 0f2f7ec commit 7de7c52

File tree

38 files changed

+219
-367
lines changed

38 files changed

+219
-367
lines changed

adev/src/content/api-examples/upgrade/static/ts/full/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class Ng1HeroComponentWrapper extends UpgradeComponent {
105105
// The names of the input and output properties here must match the names of the
106106
// `<` and `&` bindings in the AngularJS component that is being wrapped
107107
@Input() hero!: Hero;
108-
@Output() onRemove!: EventEmitter<void>;
108+
@Output() onRemove: EventEmitter<void> = new EventEmitter();
109109

110110
constructor(elementRef: ElementRef, injector: Injector) {
111111
// We must pass the name of the directive as used by AngularJS to the super

adev/src/content/api-examples/upgrade/static/ts/lite/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Ng1HeroComponentWrapper extends UpgradeComponent {
116116
// The names of the input and output properties here must match the names of the
117117
// `<` and `&` bindings in the AngularJS component that is being wrapped.
118118
@Input() hero!: Hero;
119-
@Output() onRemove!: EventEmitter<void>;
119+
@Output() onRemove: EventEmitter<void> = new EventEmitter();
120120

121121
constructor(elementRef: ElementRef, injector: Injector) {
122122
// We must pass the name of the directive as used by AngularJS to the super.

devtools/projects/ng-devtools-backend/src/lib/highlighter.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,19 @@ export function highlightHydrationElement(el: Node, status: HydrationStatus) {
103103
}
104104

105105
export function unHighlight(): void {
106-
if (selectedElementOverlay) {
107-
document.body.removeChild(selectedElementOverlay);
108-
selectedElementOverlay = null;
106+
if (!selectedElementOverlay) {
107+
return;
109108
}
109+
110+
for (const node of document.body.childNodes) {
111+
if (node === selectedElementOverlay) {
112+
document.body.removeChild(selectedElementOverlay);
113+
114+
break;
115+
}
116+
}
117+
118+
selectedElementOverlay = null;
110119
}
111120

112121
export function removeHydrationHighlights(): void {

goldens/size-tracking/integration-payloads.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"defer": {
4949
"uncompressed": {
50-
"main": 11497,
50+
"main": 12094,
5151
"polyfills": 33807,
5252
"defer.component": 345
5353
}

packages/bazel/src/ng_module/ng_module.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,6 @@ def _ngc_tsconfig(ctx, files, srcs, **kwargs):
208208
# https://github.com/bazelbuild/rules_nodejs/blob/901df3868e3ceda177d3ed181205e8456a5592ea/third_party/github.com/bazelbuild/rules_typescript/internal/common/tsconfig.bzl#L195
209209
# TODO(devversion): In the future, combine prodmode and devmode so we can get rid of the
210210
# ambiguous terminology and concept that can result in slow-down for development workflows.
211-
# TODO(alanagius): The below causes a drastic size increase when enabled (4Kb in the //integration/forms:test). This is mainly due to Babel transforms for Safari 15.
212-
# https://github.com/angular/angular-cli/blob/3e8bdf72d6b7e2925d2822da807b726f88a77e1a/packages/angular_devkit/build_angular/src/babel/presets/application.ts#L199-L204
213-
# https://www.diffchecker.com/9Ge3pexk
214-
tsconfig["compilerOptions"]["useDefineForClassFields"] = False
215211
tsconfig["compilerOptions"]["target"] = "es2022"
216212
tsconfig["compilerOptions"]["module"] = "esnext"
217213

packages/bazel/test/ng_package/example_package.golden

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ import * as i0 from '@angular/core';
128128
import { NgModule } from '@angular/core';
129129

130130
class A11yModule {
131-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: A11yModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
132-
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "0.0.0", ngImport: i0, type: A11yModule }); }
133-
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: A11yModule }); }
131+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: A11yModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
132+
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "0.0.0", ngImport: i0, type: A11yModule });
133+
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: A11yModule });
134134
}
135135
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: A11yModule, decorators: [{
136136
type: NgModule,
@@ -157,20 +157,21 @@ import * as i0 from '@angular/core';
157157
import { Injectable } from '@angular/core';
158158

159159
class MySecondService {
160-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MySecondService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
161-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MySecondService, providedIn: 'root' }); }
160+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MySecondService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
161+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MySecondService, providedIn: 'root' });
162162
}
163163
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MySecondService, decorators: [{
164164
type: Injectable,
165165
args: [{ providedIn: 'root' }]
166166
}] });
167167

168168
class MyService {
169+
secondService;
169170
constructor(secondService) {
170171
this.secondService = secondService;
171172
}
172-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyService, deps: [{ token: MySecondService }], target: i0.ɵɵFactoryTarget.Injectable }); }
173-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyService, providedIn: 'root' }); }
173+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyService, deps: [{ token: MySecondService }], target: i0.ɵɵFactoryTarget.Injectable });
174+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyService, providedIn: 'root' });
174175
}
175176
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyService, decorators: [{
176177
type: Injectable,
@@ -197,9 +198,9 @@ import * as i0 from '@angular/core';
197198
import { NgModule } from '@angular/core';
198199

199200
class SecondaryModule {
200-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: SecondaryModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
201-
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "0.0.0", ngImport: i0, type: SecondaryModule }); }
202-
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: SecondaryModule }); }
201+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: SecondaryModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
202+
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "0.0.0", ngImport: i0, type: SecondaryModule });
203+
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: SecondaryModule });
203204
}
204205
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: SecondaryModule, decorators: [{
205206
type: NgModule,
@@ -227,9 +228,9 @@ import * as i0 from '@angular/core';
227228
import { NgModule } from '@angular/core';
228229

229230
class MyModule {
230-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
231-
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "0.0.0", ngImport: i0, type: MyModule }); }
232-
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyModule }); }
231+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
232+
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "0.0.0", ngImport: i0, type: MyModule });
233+
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyModule });
233234
}
234235
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: MyModule, decorators: [{
235236
type: NgModule,

packages/bazel/test/ng_package/example_with_ts_library_package.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ import * as i0 from '@angular/core';
7676
import { NgModule } from '@angular/core';
7777

7878
class PortalModule {
79-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: PortalModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
80-
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "0.0.0", ngImport: i0, type: PortalModule }); }
81-
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: PortalModule }); }
79+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: PortalModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
80+
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "0.0.0", ngImport: i0, type: PortalModule });
81+
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: PortalModule });
8282
}
8383
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0", ngImport: i0, type: PortalModule, decorators: [{
8484
type: NgModule,

packages/bazel/test/ngc-wrapped/ivy_enabled/ng_module_ivy_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ describe('ng_module with ivy enabled', () => {
1515
'packages/bazel/test/ngc-wrapped/ivy_enabled/test_module_default_compilation.mjs',
1616
);
1717
const fileContent = readFileSync(outputFile, 'utf8');
18-
expect(fileContent).toContain(`static { this.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent`);
18+
expect(fileContent).toContain(`static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent`);
1919
});
2020
});

packages/compiler-cli/linker/src/file_linker/linker_environment.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ import {DEFAULT_LINKER_OPTIONS, LinkerOptions} from './linker_options';
1515
import {Translator} from './translator';
1616

1717
export class LinkerEnvironment<TStatement, TExpression> {
18-
readonly translator = new Translator<TStatement, TExpression>(this.factory);
19-
readonly sourceFileLoader = this.options.sourceMapping
20-
? new SourceFileLoader(this.fileSystem, this.logger, {})
21-
: null;
18+
readonly translator: Translator<TStatement, TExpression>;
19+
readonly sourceFileLoader: SourceFileLoader | null;
2220

2321
private constructor(
2422
readonly fileSystem: ReadonlyFileSystem,
2523
readonly logger: Logger,
2624
readonly host: AstHost<TExpression>,
2725
readonly factory: AstFactory<TStatement, TExpression>,
2826
readonly options: LinkerOptions,
29-
) {}
27+
) {
28+
this.translator = new Translator<TStatement, TExpression>(this.factory);
29+
this.sourceFileLoader = this.options.sourceMapping
30+
? new SourceFileLoader(this.fileSystem, this.logger, {})
31+
: null;
32+
}
3033

3134
static create<TStatement, TExpression>(
3235
fileSystem: ReadonlyFileSystem,

packages/compiler-cli/test/ngtsc/env.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ export class NgtscTestEnvironment {
4848
private multiCompileHostExt: MultiCompileHostExt | null = null;
4949
private oldProgram: Program | null = null;
5050
private changedResources: Set<string> | null = null;
51-
private commandLineArgs = ['-p', this.basePath];
51+
private commandLineArgs: string[];
5252

5353
private constructor(
5454
private fs: FileSystem,
5555
readonly outDir: AbsoluteFsPath,
5656
readonly basePath: AbsoluteFsPath,
57-
) {}
57+
) {
58+
this.commandLineArgs = ['-p', this.basePath];
59+
}
5860

5961
/**
6062
* Set up a new testing environment.

0 commit comments

Comments
 (0)