fix(zone.js): add types condition to subpath exports

Consider the following example:

```ts
if (ngDevMode) {
  await import('zone.js/plugins/task-tracking');
}
```

Under `moduleResolution: "bundler"`, `"node16"` or `"nodenext"` this fails to
type-check:

```
Could not find a declaration file for module 'zone.js/plugins/task-tracking'.
'.../node_modules/zone.js/fesm2015/task-tracking.js' implicitly has an 'any' type.
```

The `exports` map declares `types` only on the root entry, so every subpath
resolves to a bare `.js` file. Legacy `moduleResolution: "node"` ignores
`exports` and finds files by path, which is why nobody hit this until the
newer resolution modes became common.

Prior to this commit there was nothing a subpath `types` condition could
point at, because the package shipped a single declaration file at its root.
With this commit the build emits one declaration next to each bundle, and
every subpath gets a `types` condition. The bundles patch globals and export
nothing, so each declaration is `export {};` and adds no API surface to
maintain.

Legacy `node` resolution does not change. Those subpaths did not resolve
there before this commit either.

(cherry picked from commit d3a3ebf0a0)
This commit is contained in:
arturovt
2026-11-17 17:33:08 +00:00
committed by Alon Mishne
parent 3df30ae4e6
commit 9c0a263a71
5 changed files with 33 additions and 3 deletions
+3
View File
@@ -72,6 +72,9 @@ npm_package(
] + [
"//packages/zone.js/fesm2015:" + b + "-es2015.min.dist"
for b in BUNDLES_ENTRY_POINTS.keys()
] + [
"//packages/zone.js/fesm2015:" + b + "-types"
for b in BUNDLES_ENTRY_POINTS.keys()
] + [
":zone_js_types",
"//packages/zone.js/lib:zone_d_ts_types",
+3 -1
View File
@@ -1,8 +1,10 @@
load("//packages/zone.js:tools.bzl", "generate_dist")
load("//packages/zone.js:bundles.bzl", "BUNDLES_ENTRY_POINTS")
load("//packages/zone.js:tools.bzl", "generate_dist", "generate_types")
generate_dist(
bundles = BUNDLES_ENTRY_POINTS.items(),
output_format = "es2015",
umd = "",
)
generate_types(bundles = BUNDLES_ENTRY_POINTS.items())
+5
View File
@@ -63,22 +63,27 @@
"default": "./fesm2015/zone.js"
},
"./testing": {
"types": "./fesm2015/zone-testing.d.ts",
"require": "./bundles/zone-testing.umd.js",
"default": "./fesm2015/zone-testing.js"
},
"./node": {
"types": "./fesm2015/zone-node.d.ts",
"require": "./bundles/zone-node.umd.js",
"default": "./fesm2015/zone-node.js"
},
"./mix": {
"types": "./fesm2015/zone-mix.d.ts",
"require": "./bundles/zone-mix.umd.js",
"default": "./fesm2015/zone-mix.js"
},
"./plugins/*.min": {
"types": "./fesm2015/*.d.ts",
"require": "./bundles/*.umd.min.js",
"default": "./fesm2015/*.min.js"
},
"./plugins/*": {
"types": "./fesm2015/*.d.ts",
"require": "./bundles/*.umd.js",
"default": "./fesm2015/*.js"
}
@@ -186,10 +186,18 @@ describe('Zone.js npm_package', () => {
});
it('should contain all files in fesm2015', () => {
// Every entry point also ships an empty declaration, so that subpath
// imports resolve under `moduleResolution: bundler` and `node16`.
const expectedFesm2015 = [
...expected,
...expected
.filter((file) => !file.endsWith('.min.js'))
.map((file) => file.replace(/\.js$/, '.d.ts')),
].sort();
const list = readdirSync('./fesm2015').sort();
expect(list.length).toBe(expected.length);
expect(list.length).toBe(expectedFesm2015.length);
for (let i = 0; i < list.length; i++) {
expect(list[i]).toEqual(expected[i]);
expect(list[i]).toEqual(expectedFesm2015[i]);
}
});
});
+12
View File
@@ -107,6 +107,18 @@ def generate_rollup_bundle(bundles):
],
)
def generate_types(bundles):
for b in bundles:
module_name = b[0]
native.genrule(
name = module_name + "-types",
outs = [module_name + ".d.ts"],
visibility = ["//visibility:public"],
# The bundles only patch globals and export nothing, so the
# declaration exists purely to give the subpath a `types` target.
cmd = "echo 'export {};' > $@",
)
def generate_dist(bundles, output_format, umd):
module_format = "esm.js"
if output_format == "es5":