Skip to content

Commit

Permalink
fix: include index in typesVersions because it's always matched (svel…
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm authored Feb 21, 2023
1 parent b8ed332 commit e439064
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-dodos-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-migrate': patch
---

fix: include index in typesVersions because it's always matched
2 changes: 1 addition & 1 deletion documentation/docs/30-advanced/70-packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ The second option is to (ab)use the `typesVersions` feature from TypeScript to w
}
```

`>4.0` tells TypeScript to check the inner map if the used TypeScript version is greater than 4 (which should in practice always be true). The inner map tells TypeScript that the typings for `your-library/foo` are found within `./dist/foo.d.ts`, which essentially replicates the `exports` condition. You also have `*` as a wildcard at your disposal to make many type definitions at once available without repeating yourself.
`>4.0` tells TypeScript to check the inner map if the used TypeScript version is greater than 4 (which should in practice always be true). The inner map tells TypeScript that the typings for `your-library/foo` are found within `./dist/foo.d.ts`, which essentially replicates the `exports` condition. You also have `*` as a wildcard at your disposal to make many type definitions at once available without repeating yourself. Note that if you opt into `typesVersions` you have to declare all type imports through it, including the root import (which is defined as `"index": [..]`).

You can read more about that feature [here](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions).

Expand Down
12 changes: 8 additions & 4 deletions packages/migrate/migrations/package/migrate_pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,16 @@ export function update_pkg_json(config, pkg, files) {
file.is_svelte ? `${file.dest}.d.ts` : file.dest.slice(0, -'.js'.length) + '.d.ts'
}`;

if (has_type && key.slice(2) /* don't add root index type */) {
if (has_type) {
const type_key = key.slice(2) || 'index';
if (!pkg.exports[key]) {
types_versions[key.slice(2)] = [out_dir_type_path];
types_versions[type_key] = [out_dir_type_path];
} else {
const path_without_ext = pkg.exports[key].slice(
0,
-path.extname(pkg.exports[key]).length
);
types_versions[key.slice(2)] = [
types_versions[type_key] = [
`./${out_dir}/${(pkg.exports[key].types ?? path_without_ext + '.d.ts').slice(2)}`
];
}
Expand Down Expand Up @@ -188,7 +189,10 @@ export function update_pkg_json(config, pkg, files) {

// https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions
// A hack to get around the limitation that TS doesn't support "exports" field with moduleResolution: 'node'
if (Object.keys(types_versions).length > 0) {
if (
Object.keys(types_versions).length > 1 ||
(Object.keys(types_versions).length > 0 && !types_versions['index'])
) {
pkg.typesVersions = { '>4.0': types_versions };
}

Expand Down
1 change: 1 addition & 0 deletions packages/migrate/migrations/package/migrate_pkg.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ test('Updates package.json', () => {
svelte: './package/index.js',
typesVersions: {
'>4.0': {
index: ['./package/index.d.ts'],
'foo/Bar.svelte': ['./package/foo/Bar.svelte.d.ts'],
baz: ['./package/baz.d.ts'],
bar: ['./package/bar/index.d.ts'],
Expand Down

0 comments on commit e439064

Please sign in to comment.