Dart中的代码拆分 [英] Code Splitting in Dart

查看:96
本文介绍了Dart中的代码拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法到某种类型的代码分裂在Dart吗?我想推迟加载一些很少使用的代码加速初始代码下载。在Javascript中,我会注入一个新的< script> 标签,在GWT我只是调用 GWT.runAsync()。 Dart中有类似的东西吗?

Is there any way to to some kind of code splitting in Dart? I'd like to defer the loading of some rarely used code to accelerate the initial code download. In Javascript, I'd inject a new <script> tag, in GWT i'd just call GWT.runAsync(). Is there something similar in Dart?

根据这个链接< script> 注入无效(每个HTML页面最多只能有一个Dart脚本标记,我们不支持动态注入一个加载Dart代码的标签。我还发现此固定问题声明:最初的一个[用例]被延迟加载,以避免在稍后需要某些代码时进行大量下载,或者在某些情况下只需要这样做,我们现在有了一个机制。不幸的是,我找不到任何有关如何实现这一点。

According to this link, <script> injection won't work ("Each HTML page can have at most one Dart script tag", "We do not support dynamically injecting a tag that loads Dart code."). I also found this fixed issue claiming: "The initial one [use case] is deferred loading, to avoid massive downloads when some code is needed only later, or perhaps only needed in some situations. We now have a mechanism for this.". Unfortunately, I couldn't find anything on how to implement this. Does anyone know anything about this?

推荐答案

2014年9月更新这已修正!

Dart现在可以使用特殊的 import ... deferred 语法轻松支持延迟加载。例如:

Dart now easily supports deferred loading with special import... deferred syntax. For example:

import analytics.dart deferred as analytics
void main(){
    analytics.loadLibrary.then((_) { // future
        // code ready
        enableAnalyticsControl()
    });
}

这里是关于使用延迟加载的官方教程

恐怕你想要做的事情仍然是不可能的(假设你不使用dart2js)。

I'm afraid what you're trying to do is still not possible (assuming you're not using dart2js that is) .

a href =https://code.google.com/p/dart/issues/detail?id=10171 =nofollow noreferrer>此问题。

See this issue.


正如Kasper在评论3中所说的,到目前为止,这已经讨论了一个部署功能,你将得到与dart2dart。 VM参与支持这一点,让dart2dart生成的代码访问通过库调用加载源。这个库API仍然需要指定。

As Kasper said in comment 3, so far this has been discussed a deployment feature which you will get with dart2dart. The VMs involvement in supporting this ends with giving the dart2dart generated code access to loading sources lazily through a library call. This library API still needs to be specified though.

如果你使用dart2js,可以这样做。 这是有关如何执行此操作的博客文章。 / a>

If you are using dart2js this can be done. Here is a blog post on how to do this.

const lazy = const DeferredLibrary('reverser', uri: './part.js');

然后让你调用 lazy.load _){...

这篇关于Dart中的代码拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆