Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to only convert a dart function to javascript

I am currently using the following package where the readme illustrates the following

final bool loaded = await JsIsolatedWorker().importScripts(['test.js']);

I am using the isolate worker package so my code can work on web and outside of web. I would like to generate javascript code from my dart code. I have one file with a top level function and I use

dart compile js -O0 -o test.js test.dart

which I found here

https://dart.dev/tools/dart2js

this is my dart file

void main(List<String> args) {
  doComputation('');
}
            
String doComputation(String input) {
  return 'output';
}

I can generate javascript only if I have a main function but this generates a javascript file where the doComutation is not a top level function, so I am not sure if the package can call the function. It looks like it generates an entire program instead of just generating one function.

The generated file is too long to post

So what my question comes down to is this. Is there a way to generate javascript from dart for 1 function with its dependencies included instead of having to generate the entire program? So that I can call this function from dart.

like image 471
anonymous-dev Avatar asked Aug 17 '26 21:08

anonymous-dev


1 Answers

I am not an expert but I also had this problem. Here's what worked for me:

In web_worker.dart:

import 'package:js/js.dart';

main(){
  allowInterop(doComputation);
}

@JS('doComputation')
String doComputation(String computationInput) {
  // Replace this with your actual computation.
  final String computationOutput = "output";
  return computationOutput;
}

Compile it using:

$ dart compile js web_worker.dart -o webWorker.js

Manually edit webWorker.js, the JS file generated by the compiler:

Delete this line at the top of the file:

(function dartProgram() {

and the line at the bottom of the file with the corresponding closing brace:

})();

I don't understand what's going on here but I found the Javascript version of the function doComputation() defined in webWorker.js as a property of the object "A".

I defined a wrapper at the top of the file like this:

function doComputation(computationInput) {
  return A.doComputation(computationInput)
}

and then I was able to use the file with JsIsolatedWorker like this:

final bool loaded =
    await JsIsolatedWorker().importScripts(['../web/webWorker.js']);
if (loaded) {
  final String computationResult = await JsIsolatedWorker()
      .run(functionName: 'doComputation', arguments: computationInput);    
} else {
  debugPrint('Web worker is not available');
}

If someone who understands this better can elaborate or improve on this solution, that would be great. I don't really have any idea what I'm doing. I'm just posting this to hopefully help other people save time in the future by sharing what worked for me.

like image 161
Alec Stewart Avatar answered Aug 19 '26 21:08

Alec Stewart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!