Skip to content

Commit 7c06886

Browse files
crisbetothePunderWoman
authored andcommitted
refactor(compiler): output arrow functions for deferred dependencies functions (angular#51650)
Reworks the compiler to generate arrow functions for the deferred dependencies which reduces the number of bytes we generate. PR Close angular#51650
1 parent 4b54947 commit 7c06886

4 files changed

Lines changed: 14 additions & 25 deletions

File tree

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_deferred/deferred_with_external_deps_template.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import {EagerDep} from './deferred_with_external_deps_eager';
22
import {LoadingDep} from './deferred_with_external_deps_loading';
33
import * as $r3$ from "@angular/core";
44

5-
function MyApp_Defer_4_DepsFn() {
6-
return [import("./deferred_with_external_deps_lazy").then(function (m) {
7-
return m.LazyDep;
8-
})];
9-
}
5+
const MyApp_Defer_4_DepsFn = () => [import("./deferred_with_external_deps_lazy").then(m => m.LazyDep)];
106

117
function MyApp_Defer_2_Template(rf, ctx) {
128
if (rf & 1) {

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_deferred/deferred_with_local_deps_template.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
function MyApp_Defer_4_DepsFn() {
2-
return [LazyDep];
3-
}
1+
const MyApp_Defer_4_DepsFn = () => [LazyDep];
42
53
MyApp.ɵcmp = /*@__PURE__*/ $r3$.ɵɵdefineComponent({
64

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8793,9 +8793,7 @@ function allTests(os: string) {
87938793
const jsContents = env.getContents('test.js');
87948794

87958795
expect(jsContents).toContain('ɵɵdefer(1, 0, TestCmp_Defer_1_DepsFn)');
8796-
expect(jsContents)
8797-
.toContain(
8798-
'function TestCmp_Defer_1_DepsFn() { return [import("./cmp-a").then(function (m) { return m.CmpA; }), LocalDep]; }');
8796+
expect(jsContents).toContain('() => [import("./cmp-a").then(m => m.CmpA), LocalDep]');
87998797

88008798
// The `CmpA` symbol wasn't referenced elsewhere, so it can be defer-loaded
88018799
// via dynamic imports and an original import can be removed.
@@ -8847,7 +8845,7 @@ function allTests(os: string) {
88478845

88488846
// The dependency function doesn't have a dynamic import, because `CmpA`
88498847
// was eagerly referenced in component's code, thus regular import can not be removed.
8850-
expect(jsContents).toContain('function TestCmp_Defer_1_DepsFn() { return [CmpA]; }');
8848+
expect(jsContents).toContain('() => [CmpA]');
88518849
expect(jsContents).toContain('import { CmpA }');
88528850
});
88538851

@@ -8904,8 +8902,7 @@ function allTests(os: string) {
89048902
// The dependency function doesn't have a dynamic import, because `CmpA`
89058903
// was eagerly referenced in component's code, thus regular import can not be removed.
89068904
// This also affects `CmpB`, since it was extracted from the same import.
8907-
expect(jsContents)
8908-
.toContain('function TestCmp_Defer_1_DepsFn() { return [CmpA, CmpB]; }');
8905+
expect(jsContents).toContain('() => [CmpA, CmpB]');
89098906
expect(jsContents).toContain('import { CmpA, CmpB }');
89108907
});
89118908

@@ -8957,9 +8954,9 @@ function allTests(os: string) {
89578954
// referenced elsewhere, so we generate dynamic imports and drop a regular one.
89588955
expect(jsContents)
89598956
.toContain(
8960-
'function TestCmp_Defer_1_DepsFn() { return [' +
8961-
'import("./cmp-a").then(function (m) { return m.CmpA; }), ' +
8962-
'import("./cmp-a").then(function (m) { return m.CmpB; })]; }');
8957+
'() => [' +
8958+
'import("./cmp-a").then(m => m.CmpA), ' +
8959+
'import("./cmp-a").then(m => m.CmpB)]');
89638960
expect(jsContents).not.toContain('import { CmpA, CmpB }');
89648961
});
89658962
});
@@ -9062,7 +9059,7 @@ function allTests(os: string) {
90629059
const jsContents = env.getContents('test.js');
90639060

90649061
// Dependency function eagerly references `CmpA`.
9065-
expect(jsContents).toContain('function TestCmp_Defer_1_DepsFn() { return [CmpA]; }');
9062+
expect(jsContents).toContain('() => [CmpA]');
90669063

90679064
// The `setClassMetadataAsync` wasn't generated, since there are no deferrable
90689065
// symbols.

packages/compiler/src/render3/view/template.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,10 +1339,9 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
13391339

13401340
for (const deferredDep of deferredDeps) {
13411341
if (deferredDep.isDeferrable) {
1342-
// Callback function, e.g. `function(m) { return m.MyCmp; }`.
1343-
const innerFn = o.fn(
1344-
[new o.FnParam('m', o.DYNAMIC_TYPE)],
1345-
[new o.ReturnStatement(o.variable('m').prop(deferredDep.symbolName))]);
1342+
// Callback function, e.g. `m () => m.MyCmp;`.
1343+
const innerFn = o.arrowFn(
1344+
[new o.FnParam('m', o.DYNAMIC_TYPE)], o.variable('m').prop(deferredDep.symbolName));
13461345

13471346
// Dynamic import, e.g. `import('./a').then(...)`.
13481347
const importExpr =
@@ -1354,10 +1353,9 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
13541353
}
13551354
}
13561355

1357-
const depsFnExpr =
1358-
o.fn([], [new o.ReturnStatement(o.literalArr(dependencyExp))], o.INFERRED_TYPE, null, name);
1356+
const depsFnExpr = o.arrowFn([], o.literalArr(dependencyExp));
13591357

1360-
this.constantPool.statements.push(depsFnExpr.toDeclStmt(name));
1358+
this.constantPool.statements.push(depsFnExpr.toDeclStmt(name, o.StmtModifier.Final));
13611359

13621360
return o.variable(name);
13631361
}

0 commit comments

Comments
 (0)