Skip to content

Commit

Permalink
fix(ses): fix broken types for CJS consumers
Browse files Browse the repository at this point in the history
Due to changes in TS v5.5, CJS consumers cannot import types from a `type: module` package unless the declaration file has the `.d.cts` extension.  This may or may not work the other way.

See https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/\#respecting-file-extensions-and-packagejson-in-other-module-modes for explanation.
  • Loading branch information
boneskull authored and kriskowal committed May 6, 2024
1 parent 8911ba8 commit a950b99
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/ses/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,25 @@
"unpkg": "./dist/ses.umd.js",
"types": "./types.d.ts",
"exports": {
".": {
"types": "./types.d.ts",
"import": "./index.js",
"require": "./dist/ses.cjs"
".": {
"import": {
"types": "./types.d.ts",
"default": "./index.js"
},
"require": {
"types": "./dist/types.d.cts",
"default": "./dist/ses.cjs"
}
},
"./lockdown": {
"types": "./types.d.ts",
"import": "./index.js",
"require": "./dist/ses.cjs"
"./lockdown": {
"import": {
"types": "./types.d.ts",
"default": "./index.js"
},
"require": {
"types": "./dist/types.d.cts",
"default": "./dist/ses.cjs"
}
},
"./tools.js": "./tools.js",
"./assert-shim.js": "./assert-shim.js",
Expand Down
24 changes: 24 additions & 0 deletions packages/ses/scripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ const main = async () => {
...bundleFilePaths.map(dest => write(dest, versionedBundle)),
...terseFilePaths.map(dest => write(dest, terse)),
]);

// When importing types from a CJS package, TS v5.5+ considers the "module"
// field in `ses`' `package.json`, so any .d.ts file is considered to be "ESM
// types" (I'm sure this makes sense if you think hard enojugh about it; let
// us know if it does). For CJS, we need to provide a `.d.cts` file instead.
// It's unclear if this file can be identical to the original in _all_ cases,
// or just ours. I imagine ES-specific types (e.g., `import.meta`) would break
// in CJS, but generally consumers have `skipLibCheck` enabled, so ¯\_(ツ)_/¯

// Also: this operation is in this script for portability's sake.

/** The "ESM types" */
const sourceDTS = /** @type {string} */ (
packageJson.exports['.'].import.types
);
/** The "CJS types" */
const destDTS = /** @type {string} */ (
packageJson.exports['.'].require.types
);
await fs.promises.copyFile(
fileURLToPath(new URL(sourceDTS, root)),
fileURLToPath(new URL(destDTS, root)),
);
console.log(`Copied ${sourceDTS} to ${destDTS}`);
};

main().catch(err => {
Expand Down

0 comments on commit a950b99

Please sign in to comment.