Skip to content

Commit d5cc9d5

Browse files
alan-agius4alxhub
authored andcommitted
feat(bazel): remove unused ESM2022 from APF (angular#57559)
We have removed the `esm2022` directory from the generated package, as it was unused and contributed 7.7 MB to `@angular/core`. The `ng_package` rule still passes `esm2022` to the packager to perform `analyzeFileAndEnsureNoCrossImports`. A moment of reflection: You've always been there, full of potential, yet never called upon. Now, we bid you farewell. PR Close angular#57559
1 parent 7f42e69 commit d5cc9d5

File tree

8 files changed

+10
-403
lines changed

8 files changed

+10
-403
lines changed

adev/src/content/tools/libraries/angular-package-format.md

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,16 @@ The following example shows a simplified version of the `@angular/core` package'
2626

2727
```markdown
2828
node_modules/@angular/core
29-
├── README.md
30-
├── package.json
31-
├── index.d.ts
32-
├── esm2022
33-
│ ├── core.mjs
34-
│ ├── index.mjs
35-
│ ├── public_api.mjs
36-
│ └── testing
37-
├── fesm2022
29+
├── README.md
30+
├── package.json
31+
├── index.d.ts
32+
├── fesm2022
3833
│ ├── core.mjs
3934
│ ├── core.mjs.map
4035
│ ├── testing.mjs
4136
│ └── testing.mjs.map
42-
└── testing
43-
└── index.d.ts
37+
└── testing
38+
└── index.d.ts
4439
```
4540

4641
This table describes the file layout under `node_modules/@angular/core` annotated to describe the purpose of files and directories:
@@ -50,8 +45,6 @@ This table describes the file layout under `node_modules/@angular/core` annotate
5045
| `README.md` | Package README, used by npmjs web UI. |
5146
| `package.json` | Primary `package.json`, describing the package itself as well as all available entrypoints and code formats. This file contains the "exports" mapping used by runtimes and tools to perform module resolution. |
5247
| `index.d.ts` | Bundled `.d.ts` for the primary entrypoint `@angular/core`. |
53-
| `esm2022/` <br /> &nbsp;&nbsp;`core.mjs` <br /> &nbsp;&nbsp;`index.mjs` <br /> &nbsp;&nbsp;`public_api.mjs` | Tree of `@angular/core` sources in unflattened ES2022 format. |
54-
| `esm2022/testing/` | Tree of the `@angular/core/testing` entrypoint in unflattened ES2022 format. |
5548
| `fesm2022/` <br /> &nbsp;&nbsp;`core.mjs` <br /> &nbsp;&nbsp;`core.mjs.map` <br /> &nbsp;&nbsp;`testing.mjs` <br /> &nbsp;&nbsp;`testing.mjs.map` | Code for all entrypoints in flattened \(FESM\) ES2022 format, along with source maps. |
5649
| `testing/` | Directory representing the "testing" entrypoint. |
5750
| `testing/index.d.ts` | Bundled `.d.ts` for the `@angular/core/testing` entrypoint. |
@@ -96,14 +89,10 @@ The `"exports"` field has the following structure:
9689
},
9790
".": {
9891
"types": "./core.d.ts",
99-
"esm": "./esm2022/core.mjs",
100-
"esm2022": "./esm2022/core.mjs",
10192
"default": "./fesm2022/core.mjs"
10293
},
10394
"./testing": {
10495
"types": "./testing/testing.d.ts",
105-
"esm": "./esm2022/testing/testing.mjs",
106-
"esm2022": "./esm2022/testing/testing.mjs",
10796
"default": "./fesm2022/testing.mjs"
10897
}
10998
}
@@ -116,8 +105,6 @@ For each entrypoint, the available formats are:
116105
| Formats | Details |
117106
|:--- |:--- |
118107
| Typings \(`.d.ts` files\) | `.d.ts` files are used by TypeScript when depending on a given package. |
119-
| `es2022` | ES2022 code flattened into a single source file. |
120-
| `esm2022` | ES2022 code in unflattened source files \(this format is included for experimentation - see [this discussion of defaults](#note-about-the-defaults-in-packagejson) for details\). |
121108
| `default` | ES2022 code flattened into a single source.
122109

123110
Tooling that is aware of these keys may preferentially select a desirable code format from `"exports"`.
@@ -263,14 +250,6 @@ To generate a flattened ES Module index file, use the following configuration op
263250

264251
Once the index file \(for example, `my-ui-lib.js`\) is generated by ngc, bundlers and optimizers like Rollup can be used to produce the flattened ESM file.
265252

266-
#### Note about the defaults in package.json
267-
268-
As of webpack v4, the flattening of ES modules optimization should not be necessary for webpack users. It should be possible to get better code-splitting without flattening of modules in webpack.
269-
In practice, size regressions can still be seen when using unflattened modules as input for webpack v4.
270-
This is why `module` and `es2022` package.json entries still point to FESM files.
271-
This issue is being investigated. It is expected to switch the `module` and `es2022` package.json entry points to unflattened files after the size regression issue is resolved.
272-
The APF currently includes unflattened ESM2022 code for the purpose of validating such a future change.
273-
274253
### "sideEffects" flag
275254

276255
By default, EcmaScript Modules are side-effectful: importing from a module ensures that any code at the top level of that module should run.

packages/bazel/src/ng_package/packager.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {analyzeFileAndEnsureNoCrossImports} from './cross_entry_points_imports';
1616
* List of known `package.json` fields which provide information about
1717
* supported package formats and their associated entry paths.
1818
*/
19-
const knownFormatPackageJsonFormatFields = ['main', 'esm2022', 'esm', 'typings', 'module'] as const;
19+
const knownFormatPackageJsonFormatFields = ['main', 'typings', 'module'] as const;
2020

2121
/** Union type matching known `package.json` format fields. */
2222
type KnownPackageJsonFormatFields = (typeof knownFormatPackageJsonFormatFields)[number];
@@ -27,8 +27,6 @@ type KnownPackageJsonFormatFields = (typeof knownFormatPackageJsonFormatFields)[
2727
*/
2828
type ConditionalExport = {
2929
types?: string;
30-
esm2022?: string;
31-
esm?: string;
3230
default?: string;
3331
};
3432

@@ -130,16 +128,6 @@ function main(args: string[]): void {
130128
return path.relative(owningPackageName, file.shortPath);
131129
}
132130

133-
/** Writes an ESM file into the `esm2022` output directory. */
134-
function writeEsm2022File(file: BazelFileInfo) {
135-
// Note: files which do not belong to the owning package of this `ng_package` are omitted.
136-
// this prevents us from accidentally bringing in transitive node module dependencies.
137-
const packageRelativePath = getOwningPackageRelativePath(file);
138-
if (!packageRelativePath.startsWith('..')) {
139-
copyFile(file.path, getEsm2022OutputRelativePath(file));
140-
}
141-
}
142-
143131
/** Gets the output-relative path where the given flat ESM file should be written to. */
144132
function getFlatEsmOutputRelativePath(file: BazelFileInfo) {
145133
// Flat ESM files should be put into their owning package relative sub-path. e.g. if
@@ -150,13 +138,6 @@ function main(args: string[]): void {
150138
return getOwningPackageRelativePath(file);
151139
}
152140

153-
/** Gets the output-relative path where a non-flat ESM2022 file should be written to. */
154-
function getEsm2022OutputRelativePath(file: BazelFileInfo) {
155-
// Path computed relative to the current package in bazel-bin. e.g. a ES2022 output file
156-
// in `bazel-out/<..>/packages/core/src/di.mjs` should be stored in `esm2022/src/di.mjs`.
157-
return path.join('esm2022', getOwningPackageRelativePath(file));
158-
}
159-
160141
/** Gets the output-relative path where the typing file is being written to. */
161142
function getTypingOutputRelativePath(file: BazelFileInfo) {
162143
// Type definitions are intended to be copied into the package output while preserving the
@@ -184,12 +165,9 @@ function main(args: string[]): void {
184165
return getEntryPointSubpath(moduleName) !== '';
185166
}
186167

187-
const crossEntryPointFailures: string[] = [];
188-
189-
esm2022.forEach((file) => {
190-
crossEntryPointFailures.push(...analyzeFileAndEnsureNoCrossImports(file, metadata));
191-
writeEsm2022File(file);
192-
});
168+
const crossEntryPointFailures = esm2022.flatMap((file) =>
169+
analyzeFileAndEnsureNoCrossImports(file, metadata),
170+
);
193171

194172
if (crossEntryPointFailures.length) {
195173
console.error(crossEntryPointFailures);
@@ -342,7 +320,6 @@ function main(args: string[]): void {
342320
const subpath = isSecondaryEntryPoint(moduleName)
343321
? `./${getEntryPointSubpath(moduleName)}`
344322
: '.';
345-
const esmIndexOutRelativePath = getEsm2022OutputRelativePath(entryPoint.index);
346323
const fesm2022OutRelativePath = getFlatEsmOutputRelativePath(entryPoint.fesm2022Bundle);
347324
const typesOutRelativePath = getTypingOutputRelativePath(entryPoint.typings);
348325

@@ -351,8 +328,6 @@ function main(args: string[]): void {
351328
// https://github.com/microsoft/TypeScript/pull/45884.
352329
insertExportMappingOrError(newPackageJson, subpath, {
353330
types: normalizePath(typesOutRelativePath),
354-
esm2022: normalizePath(esmIndexOutRelativePath),
355-
esm: normalizePath(esmIndexOutRelativePath),
356331
// Note: The default conditions needs to be the last one.
357332
default: normalizePath(fesm2022OutRelativePath),
358333
});

packages/bazel/test/ng_package/common_package.spec.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,22 @@ describe('@angular/common ng_package', () => {
9797
'./package.json': {default: './package.json'},
9898
'.': {
9999
types: './index.d.ts',
100-
esm2022: './esm2022/common.mjs',
101-
esm: './esm2022/common.mjs',
102100
default: './fesm2022/common.mjs',
103101
},
104102
'./http': {
105103
types: './http/index.d.ts',
106-
esm2022: './esm2022/http/http.mjs',
107-
esm: './esm2022/http/http.mjs',
108104
default: './fesm2022/http.mjs',
109105
},
110106
'./http/testing': {
111107
types: './http/testing/index.d.ts',
112-
esm2022: './esm2022/http/testing/testing.mjs',
113-
esm: './esm2022/http/testing/testing.mjs',
114108
default: './fesm2022/http/testing.mjs',
115109
},
116110
'./testing': {
117111
types: './testing/index.d.ts',
118-
esm2022: './esm2022/testing/testing.mjs',
119-
esm: './esm2022/testing/testing.mjs',
120112
default: './fesm2022/testing.mjs',
121113
},
122114
'./upgrade': {
123115
types: './upgrade/index.d.ts',
124-
esm2022: './esm2022/upgrade/upgrade.mjs',
125-
esm: './esm2022/upgrade/upgrade.mjs',
126116
default: './fesm2022/upgrade.mjs',
127117
},
128118
}),

packages/bazel/test/ng_package/core_package.spec.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,22 @@ describe('@angular/core ng_package', () => {
6363
'./package.json': {default: './package.json'},
6464
'.': {
6565
types: './index.d.ts',
66-
esm2022: './esm2022/core.mjs',
67-
esm: './esm2022/core.mjs',
6866
default: './fesm2022/core.mjs',
6967
},
7068
'./primitives/event-dispatch': {
7169
types: './primitives/event-dispatch/index.d.ts',
72-
esm2022: './esm2022/primitives/event-dispatch/index.mjs',
73-
esm: './esm2022/primitives/event-dispatch/index.mjs',
7470
default: './fesm2022/primitives/event-dispatch.mjs',
7571
},
7672
'./primitives/signals': {
7773
types: './primitives/signals/index.d.ts',
78-
esm2022: './esm2022/primitives/signals/index.mjs',
79-
esm: './esm2022/primitives/signals/index.mjs',
8074
default: './fesm2022/primitives/signals.mjs',
8175
},
8276
'./rxjs-interop': {
8377
types: './rxjs-interop/index.d.ts',
84-
esm2022: './esm2022/rxjs-interop/rxjs-interop.mjs',
85-
esm: './esm2022/rxjs-interop/rxjs-interop.mjs',
8678
default: './fesm2022/rxjs-interop.mjs',
8779
},
8880
'./testing': {
8981
types: './testing/index.d.ts',
90-
esm2022: './esm2022/testing/testing.mjs',
91-
esm: './esm2022/testing/testing.mjs',
9282
default: './fesm2022/testing.mjs',
9383
},
9484
}),
@@ -140,16 +130,6 @@ describe('@angular/core ng_package', () => {
140130
);
141131
});
142132
});
143-
144-
describe('esm2022', () => {
145-
it('should not contain any *.ngfactory.js files', () => {
146-
expect(shx.find('esm2022').filter((f) => f.includes('.ngfactory'))).toEqual([]);
147-
});
148-
149-
it('should not contain any *.ngsummary.js files', () => {
150-
expect(shx.find('esm2022').filter((f) => f.includes('.ngsummary'))).toEqual([]);
151-
});
152-
});
153133
});
154134

155135
describe('secondary entry-point', () => {

0 commit comments

Comments
 (0)