- Dart:Scalable Application Development
- Davy Mitchell Sergey Akopkokhyants Ivo Balbaert
- 391字
- 2021-07-09 18:56:17
Compiling to JavaScript
It is great that we have our enhanced text editor application running in Dartium, but that is not what most web users have installed. What if I want to use it in my daily-driver web browser; for example, Firefox or Internet Explorer?
In order to run on the entire modern Web, Dart code can be compiled via a tool called dart2js (written in Dart) to high quality and performance JavaScript. This can be run as a pub
command:
pub build --mode=release
We first encountered this tool when we toured the SDK's command-line tools in the previous chapter. For easy use during development, it is accessible from WebStorm.
In WebStorm's Project tab, right-click on the pubspec.yaml
file, and select Pub: Build .... The mode option is then presented for Release or Debug—for production, choose Release. This creates a folder called build
with the JavaScript version of the program. This can be served by any web server and used from any modern browser:

It may be surprising, if text was entered in the Dartium version, that the editor is blank when run from a regular browser. One downside of using local storage is that each web browser has its own independent storage and the user will have to re-enter data if they switch browsers, even if they are on the same computer.
Minification of JavaScript output
The output files from the dart2js compiler can get quite large if certain advanced Dart features are at play, such as mirrors, which are used for reflection (sometimes called introspection).
Fortunately, the default setting of the Dart editor is to produce a minified output. There is always the overhead of the Dart runtime in the JavaScript that is disproportionately large for small applications. As Dart has all the source code ahead of runtime and knowledge of the entire application, it can compile all the code (including packages), determine which are not required, and then, within the packages that are used, remove any parts that are not used.
This sophisticated technique is called tree shaking. Any unused code is shaken off and not included in the final application that is delivered to the end user, keeping it quick to load and execute. Compare this with JavaScript, where if a library is referenced, then the entire library is sent to the client for interpretation.
- UML和模式應(yīng)用(原書(shū)第3版)
- Unity 5.x By Example
- iOS應(yīng)用逆向工程(第2版)
- JavaScript入門(mén)經(jīng)典
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲(chóng)案例實(shí)戰(zhàn)全流程詳解(入門(mén)與提高篇)
- “笨辦法”學(xué)C語(yǔ)言
- Arduino計(jì)算機(jī)視覺(jué)編程
- Backbone.js Testing
- Spark技術(shù)內(nèi)幕:深入解析Spark內(nèi)核架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)原理
- 透視C#核心技術(shù):系統(tǒng)架構(gòu)及移動(dòng)端開(kāi)發(fā)
- 輕松學(xué)Scratch 3.0 少兒編程(全彩)
- 用Go語(yǔ)言自制編譯器
- C#程序開(kāi)發(fā)參考手冊(cè)
- Learn C Programming
- jQuery基礎(chǔ)教程(第4版)