2
votes

I already using the minify argument when building with dart2js.

I looked at the output and I see that the import 'dart:html causes problems in terms of the output file size (2kb .dart file becomes 182kb .js file). For example it imports SVG package though in my code I never touch any <svg> DOM Elements.

I understand that the compiler doesn't know if I'm going to use svg DOM Elements or not. And I understand that the using of var is one of the reasons of that behavior.

But if I will not use any var keywords, the compiler still doesn't have enough 'power' to strip all unused packages and functions.

Is there any directive I can use to forbid the import of certain packages. I mean built-in packages right now. I'm using IntelliJ IDEA and it doesn't allow me to change anything in the Dart default setup.

UPD: Tried to use

import 'dart:html' show querySelector, Element

to import only that method and class, but file size is still 182kb.

The only solution I see for now is to make a few stripped versions of the default 'dart:html' package. The one without WebGL, SVG and some other features.

Because maybe Dart compiler works good, but there is just some methods and classes that I don't use, but the code uses. Like.. the initial package methods checking if some elements are SVG or something like that.

2
For other purpose I have a minimal working dart:html project with querySelector building with release the size got 37kb - main.dart.js. One step ahead could be diffing against a minimal project. - Jonas Bojesen

2 Answers

3
votes

There is a tool for analyzing the output of a dart2js build, especially for references and dependencies. Just tested and gave a better overview in my case.

https://github.com/dart-lang/dump-info-visualizer

hosted : https://dart-lang.github.io/dump-info-visualizer/

Build with option --dump-info https://webdev.dartlang.org/tools/dart2js#options

1
votes

Even when you don't import anything you will get some minimal output size. Dart provides a lot of features like classes with inheritance and mixins (and a lot more) and dart2js output contains code that implements these features. This is like adding a JS library like jQuery. Therefore main() {} will already result in an output size of several dozen kb. Adding another line of code probably will only add a few additional bytes.

pub build by default does tree-shaking and minifications, therefore no additional options are required.