Preservation methods during miniaturization of dart2js and tree tremors

I have several files that I wrote in a dart that I want to compile in javascript and include in several html files that work in my Android application.

Files consist of the main method, then there is an api layer with 3 functions that will be called by javascript code at runtime. It is very important that I include as few dart libraries as possible (so tree shake is required), and when the tree shake / minimize process occurs, I need to make sure that the level 3 api functions do not get renamed / optimized because they think that they are not called?

How to tell dart2js to leave the signature of only certain functions, and not to cut them off, because it thinks they are not used?

+4
source share
1 answer

You can use the property , a , which allows you to set variables and methods from the dart in JavaScript, and dart2js will be saved. In your main method, just enable and then call it in your JavaScript. Here is an example:dart:js contextJsObjectcontext['jsMethodName'] = dartMethodName

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>
+3
source

All Articles