1

I know a lot changed since Using Dart classes from JavaScript

It is now possible to use dart classes inside Javascript?

I have some classes that call some APIs that I want to use in my Javascript app for the web.

Ionel Lupu
  • 2,695
  • 6
  • 29
  • 53
  • Nothing has changed here - it is still not possible to call in to Dart code from JS at the top level without making functions available explicitly on the `window` or `context` – Nate Bosch Jun 03 '19 at 22:53
  • I found that `dartdevc` seems to output the code I want but the docs say I shouldn't use it. Even so, it complains about not knowing where `dart_sdk` is in `const dart_sdk = require('dart_sdk')` – Ionel Lupu Jun 04 '19 at 10:34
  • You'd need to also include the dart_sdk.js file from the sdk to use DDC output. The specific copy depends on the options passed to DDC. Easiest way to use DDC is with build_runner and build_web_compilers. Note that DDC output is not guaranteed to be stable so we could change the calling conventions in a way that break calling in to it from js which is why we don't recommend it. – Nate Bosch Jun 04 '19 at 13:26
  • I've created a blank package using `stagehand package-simple` and I want to compile it to its JS counterpart. How does `webdev build` does it and I can't? – Ionel Lupu Jun 05 '19 at 14:58
  • `webdev build` will effectively invoke `dart2js web/main.dart` (not _exactly_ but close enough for this discussion) - you can probably do that on your own. `webdev build --no-release` will do a lot of work that won't be easy to replicate. It will find the right `dart_sdk.js` file from the SDK, it will break your code and dependencies into modules and compile them all, and it will set up a bootstrap file that knows how to bring in those modules. – Nate Bosch Jun 05 '19 at 19:35
  • Long story short, there is no way of converting Dart to Javascript module and use that Javascript in an already existing app? – Ionel Lupu Jun 05 '19 at 19:37
  • 1
    https://stackoverflow.com/a/21936509/374798 is still a supported route and works with dart2js. There is no _supported_ way to call into Dart from js as a full class etc. It's probably possible but it's not documented or guaranteed to be stable. – Nate Bosch Jun 05 '19 at 19:41
  • Ok. I will try that. thank you. – Ionel Lupu Jun 05 '19 at 19:43

0 Answers0