diff --git a/.changeset/thirty-dodos-play.md b/.changeset/thirty-dodos-play.md new file mode 100644 index 000000000000..e388e3f1cc20 --- /dev/null +++ b/.changeset/thirty-dodos-play.md @@ -0,0 +1,5 @@ +--- +'svelte-migrate': patch +--- + +fix: include index in typesVersions because it's always matched diff --git a/documentation/docs/30-advanced/70-packaging.md b/documentation/docs/30-advanced/70-packaging.md index d69f9b4ffeb8..afb39a8f6523 100644 --- a/documentation/docs/30-advanced/70-packaging.md +++ b/documentation/docs/30-advanced/70-packaging.md @@ -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). diff --git a/packages/migrate/migrations/package/migrate_pkg.js b/packages/migrate/migrations/package/migrate_pkg.js index 954bbb36335c..611430938f11 100644 --- a/packages/migrate/migrations/package/migrate_pkg.js +++ b/packages/migrate/migrations/package/migrate_pkg.js @@ -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)}` ]; } @@ -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 }; } diff --git a/packages/migrate/migrations/package/migrate_pkg.spec.js b/packages/migrate/migrations/package/migrate_pkg.spec.js index 933234225887..43e0db640d27 100644 --- a/packages/migrate/migrations/package/migrate_pkg.spec.js +++ b/packages/migrate/migrations/package/migrate_pkg.spec.js @@ -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'],