How do I minify JavaScript code compiled from Dart Editor?
Asked Answered
E

2

16

I am using Dart Editor to build a Dart app. I am compiling to JavaScript to run on all browsers. I want to minify the output JavaScript. How can I do this without dropping to the command line?

I know that on the command line, I can use dart2js --minify app.dart. How do I make this automatic from Dart Editor?

Ectoblast answered 25/9, 2013 at 23:57 Comment(0)
B
5

There are two quick and easy ways to minify your complied Javascript code through the Dart Editor. The recommended way is make a small addition to your pubspec.yaml file.

Here's an example:

  Name: my-app 
  description: An Angular web application 
  dependencies: 
    angular: any 
    browser: any 
  transformers: 
  - angular 

Include this additional option and you're done:

 Name: my-app 
  description: An Angular web application
  dependencies:
    angular: any
    browser: any
  transformers:
  - angular
  - $dart2js: 
    {'minify':true}

The second method is to change the launch options of your app and deselect the VM setting Run in checked mode. In order words: Run > Managed Launches > Click on App Launch File > VM settings > Un-check "Run in checked mode".

I haven't tried this last option yet, but according to the documentation it should auto-minify when run in "production mode".

Source: https://www.dartlang.org/tools/pub/dart2js-transformer.html

P.S.: It is important that you set the $dart2js field with a map or it will fail to build properly. This is currently either a bug or documentation issue.

Borries answered 24/4, 2014 at 0:21 Comment(4)
Thanks for the up-to-date answer!Ectoblast
The first way gives me: "Invalid YAML in document. {'minify':true}"Ague
It seems the issue has been fixed. You no longer need include the brackets to enable the minify option. Just use $dart2js: minify:true and it should work. =)Borries
{'minify':true} needs additional indentation i.e. add more spaces in front of it. Or alternatively put "- $dart2js: {'minify':true}" all in one line.Mer
E
13

Starting with Dart Editor version 0.7.5_r27776, you can configure dart2js options in the "Launch Configuration" menu.

On a Mac, open Launch Config options with Cmd-Shift-M. Or, select the drop-down arrow next to the green run button and select "Manage Launches":

enter image description here

Then, find your "run as javascript" config for your app. It will have a gray globe icon.

Look for "compiler options" and add --minify

enter image description here

Ectoblast answered 25/9, 2013 at 23:57 Comment(3)
There is no compiler flags in the current development dart version. Do I use the Browser arguments line instead. thanksBernicebernie
I'm using Dart Editor version 1.2.0.dev_01_00 (DEV) and I see Compiler flags. Are you trying to configure a browser launch (and not a Dartium launch)?Ectoblast
This option doesn't exist anymore in the last version 1.2.0-dev.5.15 Any alternative way of doing it?Oneupmanship
B
5

There are two quick and easy ways to minify your complied Javascript code through the Dart Editor. The recommended way is make a small addition to your pubspec.yaml file.

Here's an example:

  Name: my-app 
  description: An Angular web application 
  dependencies: 
    angular: any 
    browser: any 
  transformers: 
  - angular 

Include this additional option and you're done:

 Name: my-app 
  description: An Angular web application
  dependencies:
    angular: any
    browser: any
  transformers:
  - angular
  - $dart2js: 
    {'minify':true}

The second method is to change the launch options of your app and deselect the VM setting Run in checked mode. In order words: Run > Managed Launches > Click on App Launch File > VM settings > Un-check "Run in checked mode".

I haven't tried this last option yet, but according to the documentation it should auto-minify when run in "production mode".

Source: https://www.dartlang.org/tools/pub/dart2js-transformer.html

P.S.: It is important that you set the $dart2js field with a map or it will fail to build properly. This is currently either a bug or documentation issue.

Borries answered 24/4, 2014 at 0:21 Comment(4)
Thanks for the up-to-date answer!Ectoblast
The first way gives me: "Invalid YAML in document. {'minify':true}"Ague
It seems the issue has been fixed. You no longer need include the brackets to enable the minify option. Just use $dart2js: minify:true and it should work. =)Borries
{'minify':true} needs additional indentation i.e. add more spaces in front of it. Or alternatively put "- $dart2js: {'minify':true}" all in one line.Mer

© 2022 - 2024 — McMap. All rights reserved.