cph-cachet / carp.core-kotlin

Infrastructure-agnostic framework for distributed data collection.
https://carp.cachet.dk/core/
MIT License
21 stars 3 forks source link

Web compatible ts package #455

Closed jakdan99 closed 8 months ago

jakdan99 commented 9 months ago

The current TS sources don't work on browser targets. They seem to be targeting node. This PR fixes that.

Could not get it working with the tsconfig.json file, so I added and configured Babel to transpile the files into web compatible js files

Whathecode commented 9 months ago

You mentioned on Slack you got the following error before this change:

Uncaught Error: Dynamic require of "@cachet/carp-common-generated" is not supported

Can you elaborate what this actually means? When did this occur? What is your build environment, etc.? And. How do these changes fix that?

I looked a bit more into potential incompatibility with the browser given the current setup, and with my albeit limited knowledge, it should work as is. The current Kotlin build outputs UMD modules (configured here). The wrappers written in TypeScript also output UMD modules. It should be possible to consume these from the browser. How are you importing the library in React (I believe that's what you were using)? Are you certain you aren't trying to import it as an ES6 module?

The latest version of Kotlin allows outputting ES6 modules, but that is still experimental and would bring along other bigger changes (seemingly it doesn't export namespaces). We could consider compiling to ES6 later, but would first like to understand the problem you are running into better.

Whathecode commented 9 months ago

Rather than this PR, I'm in the process of finding a nicer solution. Pulling in more dependencies like babel should be unnecessary, and makes this project harder to maintain. From all I could read, this should work with latest versions of node, which can compile to ES modules and interop with CommonJS modules (which UMD modules expose).

I'm working on compiling the tests and wrappers/facades as ES modules, but they still import the generated UMD modules. As you can see in ongoing changes, this does mean changing the way import/exports are currently done in the wrappers. But, I think I got the hang of it, so may be able to wrap this up soon.

I'll create a new PR and give a shout here so you can try whether you have more luck with those sources. I have a suspicion it will, since you will then be importing ES modules into an ES project.

Whathecode commented 8 months ago

The alternate approach in #456 turns out to work, so this PR can be abandoned.