Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Splitting in Dart

Tags:

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?

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?

like image 958
MarioP Avatar asked May 04 '13 20:05

MarioP


1 Answers

Update Sep 2014: this has been fixed!

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()
    });
}

Here is an official tutorial about using deferred loading.


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

See this issue.

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.

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');

Which will then let you call lazy.load().then((_) { ...

like image 186
Benjamin Gruenbaum Avatar answered Oct 14 '22 20:10

Benjamin Gruenbaum