kit icon indicating copy to clipboard operation
kit copied to clipboard

`package` not generating `.d.ts` files for all components in new repo

Open rgossiaux opened this issue 3 years ago • 4 comments

Describe the bug

I set up a small test repo with Kit just now to demonstrate some other issue. All I did was create the repo with npm init svelte@next, npm install, npm install -D svelte2tsx typescript, create a few component files in src/lib, and create a src/lib/index.ts file.

For some reason when I run npm run package the component files Component.svelte, ComponentTwo.svelte, and ComponentThree.svelte don't have any .d.ts files generated in the package directory. Only Base.svelte.d.ts is generated. There are no errors or any other indications that something is wrong.

Reproduction

https://github.com/rgossiaux/svelte-typing-tests

npm install and npm run package

Logs

No response

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 2.18 GB / 64.00 GB
    Shell: 3.3.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 16.8.0 - /var/folders/rn/2kdgb0jj25q2v656j3qc_ywr0000gn/T/fnm_multishells/23841_1640400023703/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 7.21.0 - /var/folders/rn/2kdgb0jj25q2v656j3qc_ywr0000gn/T/fnm_multishells/23841_1640400023703/bin/npm
  Browsers:
    Chrome: 98.0.4758.80
    Safari: 15.2
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.17
    @sveltejs/kit: next => 1.0.0-next.260
    svelte: ^3.44.0 => 3.46.4

Severity

blocking all usage of SvelteKit

Additional Information

No response

rgossiaux avatar Feb 09 '22 00:02 rgossiaux

This evening I took a quick look into this. All the files are being passed into emitDts, but it looks like TypeScript is only calling host.writeFile for some of them.

I think it might be related to $$Generic, though without a root cause I'm not entirely sure. The three files that did not have emitted types all had $$Generic, and when I changed one of the types away from $$Generic in one of those components, its definition file was now emitted correctly.

rgossiaux avatar Mar 02 '22 08:03 rgossiaux

I remember svelte2tsx's emitDts() function would silently fail if there are type errors in one of the components. I'm not sure if it's fixed yet though, but it may be the cause of this issue.

bluwy avatar Mar 03 '22 05:03 bluwy

Ahhhhhhh... I figured it out! At least, partially. There's an issue because these components use the svelte.JSX namespace, but the package command does not load the svelte-jsx.d.ts file where this is defined (but svelte-check and the language server do, so there's no error otherwise).

If I add a config.svelteJsxPath, similar to the existing config.svelteShimsPath, to emitDts here:

https://github.com/sveltejs/language-tools/blob/05024a657784be7a5b57665f4cb39f8b649718a1/packages/svelte2tsx/src/emitDts.ts#L74

and here in kit:

https://github.com/sveltejs/kit/blob/49c787086f1e9f75cdba618ba455be33fe073685/packages/kit/src/packaging/index.js#L316

then we do get .d.ts files for ComponentTwo and ComponentThree.

Making this change also fixes the issue I reported at the bottom of this comment: https://github.com/sveltejs/language-tools/issues/442#issuecomment-1033206304 which is blocking me from shipping a big improvement to the TypeScript support for svelte-headlessui. I've been bashing my head against this for a while so I'm glad to have figured out what's wrong.

Does this sound like a reasonable fix? Maybe the svelte-native-jsx.d.ts file should be added as well (?).

cc @dummdidumm


edit: I think, separately from this particular fix, there should still be some error message if emitDts does not successfully emit a dts file.

rgossiaux avatar Mar 07 '22 02:03 rgossiaux

I have noticed the following:

All the following works:

type T = $$Generic;
type U = $$Generic<string>;

import type { TypeArbitrary } from './types.ts';
type V = $$Generic<TypeArbitrary >;

This does not work:

type TypeArbitrary = string;
type U = $$Generic<TypeArbitrary >;

It is as if defining a type used in a component as an extension of a generic type was not working.

Ennoriel avatar Nov 14 '22 17:11 Ennoriel