Release and debug version of Dart build

Issue

I’m fresh Dart appretience and I’m trying the ‘one-hour-codelab’ tutorial. I’m using IntellijIDEA 14 and its Dart plugin.

When I build ‘Debug’, everything works OK in Dartium.

When I build ‘Release’, Dart code gets translated into Javascript, but HTML code is still referencing the Dart source file.

I assume there is some solution for this, do you know it?

Thanks
Rene

Solution

The source is meant to still point at the .dart files, since if the browser has a Dart VM in it, you want to use that rather than the generated JS. It’s the job of the dart.js script (which is part of the browser package) to figure out if the browser you are running on has a Dart VM or not, and if not, to substitute in the appropriate JS scripts.

For example, the source of your index.html file might look like this:

<html><body>
    <script type="application/dart" src="main.dart"></script>
    <script src="packages/browser/dart.js"></script>
</body></html>

In browser with the Dart VM (like Dartium) the dev tools will show those same script tags. However, in vanilla Chrome or another browser, the HTML you see in the dev tools will look like this:

<html><body>
    <script type="application/dart" src="http://localhost:8080/main.js">/script>
    <script src="packages/browser/dart.js"></script>
</body></html>

This is because the dart.js script has replaced the main.dart script with the corresponding JS file.

If you aren’t seeing this translation happen, make sure that you are including the dart.js script in your index.html file, and that you are using the browser package by adding it to the dependencies of you pubspec.yaml file:

dependencies:
  browser: any

It’s worth noting that the --mode=release option for the pub build command doesn’t include the .dart files in its output, but other modes will (https://www.dartlang.org/tools/pub/cmd/pub-build.html). I suppose that since no browsers in the wild currently have a Dart VM in them, that pub build assumes you only want to release JS files. I suspect this might change if/when vanilla Chrome gets the Dart VM added in. In the meantime, after you build your project, if you want it to also work in Dartium, you’ll need to add in the .dart files to the build output. If you wanted to get extra fancy, you could minify your Dart first by using dart2js with the --output-type=dart flag set (https://www.dartlang.org/tools/dart2js/#options).

Answered By – Michael Fenwick

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *