简体   繁体   English

Dart2js生成大型javascript文件,即使代码很少?

[英]Dart2js producing large javascript files, even with little code?

I'm new to dart, and am using Dart Editor on Windows. 我是dart的新手,在Windows上使用Dart编辑器。

I noticed that my compiled javascript is huge, so I kept removing more and more code to see what was causing it, but I don't think I'm getting anywhere. 我注意到我编译的javascript很大,所以我不断删除越来越多的代码来查看导致它的原因,但我认为我无处可去。 Here's my program right now: 这是我的程序:

import 'dart:html';

void main() {
  var video = querySelector("#vid");
}

That's literally it. 这就是字面意思。 I've stripped out everything but one instruction. 除了一条指令,我已经删除了所有内容。

And this is the produced javascript (it won't fit inline): 这是生成的javascript(它不适合内联):

https://gist.github.com/DSteve595/504887a19a05614bcc94 https://gist.github.com/DSteve595/504887a19a05614bcc94

What am I doing wrong? 我究竟做错了什么? This program's practically empty! 这个程序几乎是空的!

The file you linked to in the gist is 48k. 您在要点中链接的文件是48k。 If you ran dart2js with the --minify option, you could bring the size down quite a bit. 如果你使用--minify选项运行dart2js,你可以将大小降低一点。

Your code may be trivial, but dart2js has to load a significant number of libraries by default, and it has to load the dart:html library that you import as well. 您的代码可能很简单,但dart2js默认情况下必须加载大量库,并且它还必须加载您导入的dart:html库。 For perspective, how big do you think your file would be if you wrote your program in JavaScript and imported all of jQuery? 对于透视图,如果您使用JavaScript编写程序并导入所有jQuery,您认为您的文件有多大?

You can read more about dart2js at https://www.dartlang.org/docs/dart-up-and-running/contents/ch04-tools-dart2js.html . 您可以在https://www.dartlang.org/docs/dart-up-and-running/contents/ch04-tools-dart2js.html上阅读有关dart2js的更多信息。

dart2js needs to generate a significant amount of code to emulate Dart behaviour in JavaScript. dart2js需要生成大量代码来模拟JavaScript中的Dart行为。 Types, error checking, math, etc. all need to be emulated in JavaScript. 类型,错误检查,数学等都需要在JavaScript中进行模拟。 This results in a lot of extra code and a trivial "Hello World" app will look monstrously large. 这导致了许多额外的代码,而一个简单的“Hello World”应用程序看起来会非常庞大​​。

You can reduce the size of the delivered JavaScript by minifying it and gzipping it. 您可以通过缩小和压缩它来减小交付的JavaScript的大小。

dart2js has been getting better over time, but there will always be a size cost to emulating Dart behaviour in JavaScript. 随着时间的推移,dart2js一直在变得越来越好,但是在JavaScript中模拟Dart行为总会有大小成本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM