jacobmischka/svelte-flatpickr

moduleResolution: "bundler" support

hjsung-brique opened this issue · 3 comments

I tried use svelte-flatpickr with tsconfig.json setting as below:

{
	"extends": "./.svelte-kit/tsconfig.json",
	"compilerOptions": {
		"allowJs": true,
		"checkJs": true,
		...
		"module": "ESNext",
		"moduleResolution": "Bundler",
		"types": ["vitest/globals"]
	}
}

When I import module as
import Flatpickr from 'svelte-flatpickr';
and executed npm run check, I see error :

Error: Cannot find module 'svelte-flatpickr' or its corresponding type declarations. (ts)
        import Flatpickr from 'svelte-flatpickr';

It was because "moduleResolution": "Bundler" option in tsconfig.json makes fail to find type definitions. (https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-resolution)

I tried to fix package.json file of svelte-flatpickr module as below, and error resolved.

{
     ...
      "exports": {
		".": {
			"svelte": "./src/Flatpickr.svelte",
			"types": "./src/types.d.ts"
		}
	}
}

How about adding "types" field to svelte-flatpickr module's package.json file?

LGTM, do you mind submitting a PR?

Thank you! Should be released in 3.3.4.

After PR, I reviewed official svelte site about packaging (https://kit.svelte.dev/docs/packaging#typescript), and found that they recommend add "typesVersions" field to package.json entries like :

{	"exports": {
		"./foo": {
			"types": "./dist/foo.d.ts",
			"svelte": "./dist/foo.js"
		}
	},
	"typesVersions": {
		">4.0": {
			"foo": ["./dist/foo.d.ts"]
		}
	}
}

As I use typescript version 5.0.0, it was fine without "typesVersions" field. For anyone using lower version of typescript, "typesVersions" field might necessary. I hope it helps for later updates.