Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/wild-pugs-resolve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
'graphql-modules': patch
---

Restore the `types` conditions in the published `exports` map.

`package.json` has declared `exports["."].types` and `exports["./*"].types` for
years, but `bob build` discards them: its `rewriteExports` rebuilds every entry
as `{ require, import }` only. Every published tarball up to and including
3.1.2 therefore ships an `exports` map with no `types` condition, which makes
the declarations unreachable under `moduleResolution` `bundler`, `node16` and
`nodenext` — TypeScript reports

```
error TS7016: Could not find a declaration file for module 'graphql-modules'.
There are types at '.../graphql-modules/index.d.ts', but this result could
not be resolved when respecting package.json "exports".
```

Only the legacy `node10` resolver worked, because it reads the top-level
`typings` field and ignores `exports` entirely.

The conditions are now reinstated in `scripts/fix-async-context.js`, which
already post-processes `dist/package.json` for the same class of loss — bob also
strips the `imports` field. Fixes the problem reported in #2480.
32 changes: 32 additions & 0 deletions scripts/fix-async-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const path = require('path');

const __project = path.resolve(__dirname, '..');

// bob publishes from this directory, and rewrites exports paths relative to it.
const DIST_DIR = 'dist';

console.log('Fixing async context imports in dist package.json...');

const distPkg = JSON.parse(
Expand Down Expand Up @@ -48,6 +51,35 @@ for (const file of [node, browser]) {

console.log('Updating imports field in dist package.json...');
distPkg.imports = srcPkg.imports;

// Restore the "types" conditions as well. bob's rewriteExports rebuilds every
// entry as { require, import } only, so any "types" condition in the source
// exports map is dropped on the way to dist -- even though package.json has
// declared it for years. Without it, a consumer on moduleResolution
// bundler/node16/nodenext cannot see the declarations at all: only the legacy
// node10 resolver can, via "typings". That is the cause of #2480 and #2686.
console.log('Restoring types conditions in dist package.json exports...');
for (const [subpath, srcCondition] of Object.entries(srcPkg.exports || {})) {
if (
!srcCondition ||
typeof srcCondition !== 'object' ||
!srcCondition.types
) {
continue;
}

const distCondition = distPkg.exports && distPkg.exports[subpath];
if (!distCondition || typeof distCondition !== 'object') {
continue;
}

// "types" first: that is the documented order, and it keeps the condition
// ahead of the runtime ones for any resolver that short-circuits.
distPkg.exports[subpath] = {
types: srcCondition.types.replace(`${DIST_DIR}/`, ''),
...distCondition,
};
}
fs.writeFileSync(
path.join(__project, 'packages', 'graphql-modules', 'dist', 'package.json'),
JSON.stringify(distPkg, null, 2) + '\n'
Expand Down
Loading