5

I have a few files that I have written in dart that I want to compile to javascript and include in a few html files that are running in my Android application.

The files consist of a main method, then there is an api layer with 3 functions that other javascript code will call at run-time. It's very important that I include as little of dart's libraries as possible (so tree-shaking is a must), and when the tree-shaking / minification process happens, I need to ensure that the 3 api layer functions don't get renamed / optimized out because it thinks they aren't being called?

How do I tell dart2js to leave the signature of certain functions alone, and not to prune them out because it thinks they aren't being used?

spierce7
  • 14,797
  • 13
  • 65
  • 106
  • Not sure if I follow. Are you wanting to write a library in Dart, and then compile it with dart2js, and then to call the compiled library from javascript? – Greg Lowe Feb 28 '15 at 21:25
  • @GregLowe I re-wrote the question because it was too confusing. Basically yes, I have a javascript library in Dart, and I want to compile it / optimize it with dart2js and then have other javascript (that the compiler won't know about) call it. – spierce7 Feb 28 '15 at 21:50
  • 1
    I haven't tried it myself yet but dart-js-interop allows to expose Dart methods to JS and I assume they should be preserved by treeshaking and minification. It's probably not that simple otherwise this would be an officially supported use case. There were plans mentioned to support this scenario eventually but I haven't noticed any activities in this direction so far. – Günter Zöchbauer Feb 28 '15 at 22:28
  • 1
    @spierce7 I agree with Guenter, that use case isn't very well supported at the moment. Though some libraries may have a fairly simple api and can be implemented this way without too much trouble - so it depends on the library really. – Greg Lowe Mar 01 '15 at 01:14
  • 1
    Thanks for the replies guys. I think I'll shy away from dart on this then and just implement in js. It looks like if there was a library for this, it'd be https://github.com/dart-lang/js-interop which is what I think Gunter was pointing to earlier. – spierce7 Mar 01 '15 at 02:19
  • https://groups.google.com/a/dartlang.org/d/msg/misc/i6cy9DfT_8A/MF5Iot5C8-4J – Greg Lowe Mar 01 '15 at 10:07
  • The currently experimental https://github.com/dart-lang/dev_compiler should provide what you want. – Günter Zöchbauer May 28 '15 at 14:51

1 Answers1

3

You can use the dart:js library's context property, a JsObject which allows you to expose variables and methods from dart to JavaScript, and will be preserved by dart2js. In your main method, simply include context['jsMethodName'] = dartMethodName, and then call it in your JavaScript. Here's an example:

library.dart

import 'dart:js';

void main(){
  print("main called!");
  context['myMethod'] = myMethod;
}

void myMethod(){
  print("My Method Called!");
}

index.html

<script src="library.dart.js"></script>
<script>
  console.log(myMethod());
</script>
hkk
  • 2,069
  • 1
  • 23
  • 48