popeindustries / lit

Fast server-rendering and client-hydration of lit-html templates and web components
MIT License
33 stars 4 forks source link

Export for directives can't be resolved #10

Closed knuthaug closed 1 year ago

knuthaug commented 2 years ago

We are experiencing an issue with using @popeindustries/lit@1.0.4 as a "proxy" for exports from @popeindustries/lit-html.

import { unsafeHTML } from '@popeindustries/lit/directives/unsafe-html.js';

gives the error

error TS2305: Module '"@popeindustries/lit/directives/unsafe-html"' has no exported member 'unsafeHTML'.

Checking the file node_modules/@popeindustries/lit/directives/unsafe-html.js it contains

export * from '@popeindustries/lit-html/directives/unsafe-html.js';

This file path points to nothing in the package @popeindustries/lit-html on disc in node_modules. @popeindustries/lit-html has src and vendor directories (and src/vendor) which contains directives. The export map of @popeindustries/lit-html lists directives as

"./directives/*": {
      "development": "./src/vendor/directives/*",
      "default": "./vendor/directives/*"
  }

which seem to point to the right place. So why can't the typescript compiler resolve this import? Using the import directly from lit-html gives the same error, while pointing directly to e.g. the vendor directory

import { unsafeHTML } from '@popeindustries/lit-html/vendor/directives/unsafe-html.js';

works as intended. This leads me to think that our project can't read/understand the exports map correctly, but shouldn't node 18.9 and typescript 4.8.3 be able to do that? On closer inspection it seems that it might be the wildcard export map that isn't understood, because addition of e.g

"./directives/async-append.js": {
      "development": "./src/vendor/directives/async-append.js",
      "default": "./vendor/directives/async-append.js"
    }

Seems to work well.

node version: 18.9.0 typescript version: 4.8.3

knuthaug commented 2 years ago

Additional testing

'@popeindustries/lit-html/vendor/directives/unsafe-html.js

works well, while

'@popeindustries/lit-html/directives/unsafe-html.js

Does not. Is there som specific config needed in our project to make this work?

popeindustries commented 2 years ago

Interesting. This seems to works correctly in Node/dvlp/esbuild, but not yet in TypeScript. The fix has been merged (https://github.com/microsoft/TypeScript/pull/49644), but evidently not yet released. I guess the only option is to manually add each export

popeindustries commented 1 year ago

Correction: TypeScript can't resolve aliases via exports (./directives.js => ./vendor/directives.js), so need to create modules that alias to vendor module.

pumano commented 1 year ago

same problem with lit-html-server, have problems with typings resolve in TypeScript:

Cannot find module '@popeindustries/lit-html-server' or its corresponding type declarations.ts(2307)

popeindustries commented 1 year ago

Thanks @pumano, I'll look into that too.