Converter icon indicating copy to clipboard operation
Converter copied to clipboard

Class imports for Typescript >= 4.7 ES modules

Open pslaski opened this issue 2 years ago • 6 comments

I have a small typescript library with defined classes. Class code generated by ScalablyTyped is annotated by

@JSImport("library/dist/types/class", "Class")

The problem is

Relative import paths need full extensions (we have to write import "./foo.js" instead of import "./foo")

taken from announcing-typescript-4-7

Using given class in node runtime ends with

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'library/dist/types/class' imported from /target/scala-2.13/app-test-fastopt/main.js
Did you mean to import library/dist/types/class.js?

The correct import annotation should be

@JSImport("library/dist/types/class.js", "Class")

pslaski avatar Sep 20 '22 13:09 pslaski

That paragraph from the typescript announcement... It's a wonder anything works at all.

Anyways, what did you do to end up in this situation? just using typescript 4.7 when building the library? Is it publically viewable?

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'library/dist/types/class' imported from /target/scala-2.13/app-test-fastopt/main.js Did you mean to import library/dist/types/class.js?

is this from webpack? node?

oyvindberg avatar Sep 20 '22 19:09 oyvindberg

Unfortunately, it is not public. I refactored all classes into types which are generated without imports. It is for node env

pslaski avatar Sep 21 '22 14:09 pslaski

But the same thing is for constant values and functions

pslaski avatar Sep 22 '22 15:09 pslaski

just to add more context - my library is a ES6 module and according to https://nodejs.org/api/packages.html#packages_determining_module_system

It does not support folders as modules, directory indexes (e.g. './startup/index.js') must be fully specified.
It does no extension searching. A file extension must be provided when the specifier is a relative or absolute file URL.

node also wants full paths

pslaski avatar Oct 04 '22 15:10 pslaski

Anyways, what did you do to end up in this situation? just using typescript 4.7 when building the library Sorry @oyvindberg, I misunderstood your question. So I have a small library with some data types and API written with typescript which I am publishing as ES6 module. I'm using it in ScalaJS project and given error came from munit test suite which afaik is running in NodeJS environment (probably Node 16)

BTW @oyvindberg I could try to help you with this issue, I just need some guidance which components would be a good starting point. At first glance, some configuration option + some simple JsImport transformation would be enough

pslaski avatar Oct 05 '22 15:10 pslaski

I have found another fix https://nodejs.org/docs/latest-v16.x/api/esm.html#customizing-esm-specifier-resolution-algorithm this flag is disabling requirement of having full import paths

pslaski avatar Oct 10 '22 13:10 pslaski