dart2js缩小和树木摇晃期间的保存方法 [英] Preserving methods during dart2js minification and tree-shaking
问题描述
我有一些文件,我写在dart,我想编译为javascript和包括在我的Android应用程序中运行的几个html文件。
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.
文件由一个main方法组成,然后有一个api层,有3个函数,其他javascript代码将在运行时调用。非常重要的是,我尽可能少地使用dart的库(所以树摇动是必须的),当树摇动/缩小过程发生时,我需要确保3 api层函数不被重命名/优化出来,因为它认为他们没有被调用?
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?
如何告诉dart2js单独留下某些函数的签名,而不是修剪它们,因为它认为
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?
推荐答案
您可以使用 dart:js
/ code>属性,一个
JsObject
,它允许您将变量和方法从dart暴露到JavaScript,并且将由dart2js保存。在你的main方法中,只需包括 context ['jsMethodName'] = dartMethodName
,然后在你的JavaScript中调用它。示例如下:
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
$ b
index.html
<script src="library.dart.js"></script>
<script>
console.log(myMethod());
</script>
这篇关于dart2js缩小和树木摇晃期间的保存方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!