Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit e3e8bb5

Browse files
committed
Merge pull request #133 from angular/kwalrath-dart-quickstart
dart quickstart: add transformer, use alpha.25
2 parents b206074 + 0bff66b commit e3e8bb5

File tree

1 file changed

+177
-129
lines changed

1 file changed

+177
-129
lines changed

public/docs/dart/latest/quickstart.jade

Lines changed: 177 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,81 @@
77

88

99
p.
10-
These instructions assume that you already have the Dart SDK
10+
These instructions assume that you already have the
11+
<a href="https://www.dartlang.org/downloads/">Dart SDK</a>
1112
and any tools you like to use with Dart.
12-
If not, go
13-
<a href="https://www.dartlang.org/downloads/">download Dart</a>.
14-
Then return here.
13+
If you don't have a favorite editor already, try
14+
<a href="https://confluence.jetbrains.com/display/WI/Getting+started+with+Dart">WebStorm</a>,
15+
which comes with a Dart plugin.
16+
You can also download
17+
<a href="https://www.dartlang.org/tools/">Dart plugins for
18+
other IDEs and editors</a>.
1519

16-
// STEP 1 - Create a project ##########################
20+
p.
21+
Once you have the Dart SDK and any other tools you want, return here.
22+
23+
//- STEP 1 - Create a project ##########################
1724
.l-main-section
1825
h2#section-install-angular 1. Create a project
1926

2027
p.
2128
Create a new directory, and put a file named
2229
<code>pubspec.yaml</code> in it.
2330

24-
pre.prettyprint
25-
code.
26-
&gt; mkdir hello_world
27-
&gt; cd hello_world
28-
&gt; vim pubspec.yaml # Use your favorite editor!
31+
code-example(language="sh").
32+
&gt; <span class="blk">mkdir hello_world</span>
33+
&gt; <span class="blk">cd hello_world</span>
34+
&gt; <span class="blk">vim pubspec.yaml</span> # Use your favorite editor!
2935

3036
p.
31-
In <code>pubspec.yaml</code>, add the angular2 and browser packages as dependencies.
32-
Angular 2 is changing rapidly, so specify an exact version:
33-
<b>2.0.0-alpha.23</b>.
34-
35-
pre.prettyprint.linenums.lang-basic
36-
code.
37-
name: hello_world
38-
version: 0.0.1
39-
dependencies:
40-
angular2: 2.0.0-alpha.23
41-
browser: any
37+
In <code>pubspec.yaml</code>,
38+
specify the angular2 and browser packages as dependencies,
39+
as well as the angular2 transformer.
40+
Angular 2 is changing rapidly, so provide an exact version:
41+
<b>2.0.0-alpha.25</b>.
42+
43+
code-example(language="yaml" format="linenums").
44+
name: hello_world
45+
version: 0.0.1
46+
dependencies:
47+
angular2: 2.0.0-alpha.25
48+
browser: ^0.10.0+2
49+
transformers:
50+
- angular2:
51+
entry_points: web/main.dart
4252
p.
4353
In the same directory, run <code>pub get</code>
4454
to install the angular2 and browser packages
4555
(along with the packages they depend on):
4656

47-
pre.prettyprint.lang-basic
48-
code.
49-
&gt; pub get
57+
code-example(language="sh").
58+
&gt; <span class="blk">pub get</span>
5059

51-
// PENDING: Create template? Link to pub/pubspec docs?
52-
// Is browser really needed?
60+
//- PENDING: Create template? Link to pub/pubspec docs?
5361
5462
55-
// STEP 2 - Import Angular ##########################
63+
//- STEP 2 - Import Angular ##########################
5664
.l-main-section
5765
h2#section-transpile 2. Import Angular
5866

5967
p.
6068
Still in the same directory, create a <code>web</code> directory.
6169
Create a file under <code>web</code> named <code>main.dart</code>.
6270

63-
pre.prettyprint
64-
code.
65-
&gt; mkdir web
66-
&gt; vim web/main.dart # Use your favorite editor!
71+
code-example(language="sh").
72+
&gt; <span class="blk">mkdir web</span>
73+
&gt; <span class="blk">vim web/main.dart</span> # Use your favorite editor!
6774

6875
p.
6976
Edit <code>web/main.dart</code> to import the angular2 library
70-
and (for now) two reflection libraries:
71-
72-
// pre.prettyprint.linenums
73-
pre.prettyprint
74-
code.
75-
import 'package:angular2/angular2.dart';
77+
and two reflection libraries:
7678

77-
// These imports will go away soon:
78-
import 'package:angular2/src/reflection/reflection.dart' show reflector;
79-
import 'package:angular2/src/reflection/reflection_capabilities.dart' show ReflectionCapabilities;
79+
code-example(language="dart" format="linenums").
80+
import 'package:angular2/angular2.dart';
81+
import 'package:angular2/src/reflection/reflection.dart' show reflector;
82+
import 'package:angular2/src/reflection/reflection_capabilities.dart' show ReflectionCapabilities;
8083

81-
// [PENDING: add line numbers once we can specify where they start]
82-
83-
// STEP 3 - Define a component ##########################
84+
//- STEP 3 - Define a component ##########################
8485
.l-main-section
8586

8687
h2#section-angular-create-account 3. Define a component
@@ -89,19 +90,16 @@ p.
8990
Update <code>web/main.dart</code>, adding the following code
9091
after the imports:
9192

92-
pre.prettyprint
93-
code.
94-
@Component(
95-
selector: 'my-app'
96-
)
97-
@View(
98-
template: '&lt;h1&gt;Hello {{ name }}&lt;/h1&gt;'
99-
)
100-
class AppComponent {
101-
String name = 'Alice';
102-
}
103-
104-
// [PENDING: add line numbers once we can specify where they start]
93+
code-example(language="dart" format="linenums:5").
94+
@Component(
95+
selector: 'my-app'
96+
)
97+
@View(
98+
template: '&lt;h1&gt;Hello {{ name }}&lt;/h1&gt;'
99+
)
100+
class AppComponent {
101+
String name = 'Alice';
102+
}
105103

106104
p.
107105
The code you just added creates a component that
@@ -125,16 +123,13 @@ p.
125123
specify a <code>templateUrl</code> property
126124
and give it the path to the HTML file.
127125

128-
pre.prettyprint
129-
code.
130-
@Component(
131-
selector: 'my-app'
132-
)
133-
@View(
134-
template: '&lt;h1&gt;Hello {{ name }}&lt;/h1&gt;'
135-
)
136-
137-
// [PENDING: add line numbers once we can specify where they start]
126+
code-example(language="dart").
127+
@Component(
128+
selector: 'my-app'
129+
)
130+
@View(
131+
template: '&lt;h1&gt;Hello {{ name }}&lt;/h1&gt;'
132+
)
138133

139134
p.
140135
The annotations above specify an HTML tag of <code>&lt;my-app&gt;</code>
@@ -154,32 +149,23 @@ p.
154149
template renders, "Hello Alice" appears instead of
155150
<span ng-non-bindable>"Hello {{ name }}"</span>.
156151

157-
pre.prettyprint
158-
code.
159-
class AppComponent {
160-
String name = 'Alice';
161-
}
162-
163-
// [PENDING: add line numbers once we can specify where they start]
164-
152+
code-example(language="dart").
153+
class AppComponent {
154+
String name = 'Alice';
155+
}
165156

166-
// STEP 4 - Bootstrap ##########################
157+
//- STEP 4 - Bootstrap ##########################
167158
.l-main-section
168159
h2#section-transpile 4. Bootstrap
169160

170161
p.
171162
Add the following code to the bottom of <code>web/main.dart</code>:
172163

173-
pre.prettyprint
174-
code.
175-
main() {
176-
// Temporarily needed.
177-
reflector.reflectionCapabilities = new ReflectionCapabilities();
178-
179-
bootstrap(AppComponent);
180-
}
181-
182-
// [PENDING: add line numbers once we can specify where they start]
164+
code-example(language="dart" format="linenums:15").
165+
main() {
166+
reflector.reflectionCapabilities = new ReflectionCapabilities();
167+
bootstrap(AppComponent);
168+
}
183169

184170
p.
185171
This code adds a <code>main()</code> function
@@ -188,13 +174,18 @@ p.
188174
you defined above.
189175

190176
p.
191-
Setting the value of
192-
<code>reflector.reflectionCapabilities</code>
193-
is a temporary workaround
194-
that you can remove once Angular's transformer is available.
195-
196-
197-
// STEP 5 - Declare the HTML ##########################
177+
Setting the value of <code>reflector.reflectionCapabilities</code>
178+
lets your app use the Dart VM's reflection (dart:mirrors)
179+
when running in Dartium.
180+
Reflection is fast in native Dart,
181+
so using it makes sense during development.
182+
Later, when you build a JavaScript version of your app,
183+
the Angular transformer will
184+
convert the reflection-using code to static code,
185+
so your generated JavaScript can be smaller and faster.
186+
187+
188+
//- STEP 5 - Declare the HTML ##########################
198189
.l-main-section
199190

200191
h2#section-angular-create-account 5. Declare the HTML
@@ -204,53 +195,110 @@ p.
204195
the following code,
205196
which loads <code>main.dart</code> and instantiates the my-app component:
206197

207-
pre.prettyprint.linenums
208-
code.
209-
&lt;!doctype html>
210-
&lt;html>
211-
&lt;head&gt;
212-
&lt;title&gt;Angular 2 Quickstart&lt;/title&gt;
213-
&lt;/head&gt;
214-
&lt;body>
215-
&lt;my-app>&lt;/my-app>
216-
217-
&lt;script type="application/dart" src="main.dart">&lt;/script>
218-
&lt;script src="packages/browser/dart.js">&lt;/script>
219-
&lt;/body>
220-
&lt;/html>
221-
222-
// STEP 6 - Build and run ##########################
198+
code-example(language="html" format="linenums").
199+
&lt;!doctype html>
200+
&lt;html>
201+
&lt;head&gt;
202+
&lt;title&gt;Angular 2 Quickstart&lt;/title&gt;
203+
&lt;/head&gt;
204+
&lt;body>
205+
&lt;my-app>&lt;/my-app>
206+
207+
&lt;script type="application/dart" src="main.dart">&lt;/script>
208+
&lt;script src="packages/browser/dart.js">&lt;/script>
209+
&lt;/body>
210+
&lt;/html>
211+
212+
//- STEP 6 - Run ##########################
223213
.l-main-section
224214

225-
h2#section-angular-run-app 6. Build and run your app
215+
h2#section-angular-run-app 6. Run your app
226216

227217
p.
228-
You have several options for running your app.
229-
The quickest and easiest, for now,
230-
is to open your project in <b>Dart Editor</b>,
231-
right-click <b>web/index.html</b>,
232-
and choose <b>Open in Dartium</b>.
233-
This starts a web server
234-
and opens your app in an experimental browser that contains the Dart VM.
235-
236-
// TODO: screenshot? embedded app?
218+
You have a few options for running your app.
219+
One is to launch a local HTTP server
220+
and then view the app in
221+
<a href="https://www.dartlang.org/tools/dartium/">Dartium</a>.
222+
You can use whatever server you like, such as WebStorm's server
223+
or Python's SimpleHTTPServer.
237224

238225
p.
239226
Another option is to build and serve the app using <code>pub serve</code>,
240-
and then run it by visiting <b>http://localhost:8080</b> in a browser.
241-
Generating the JavaScript takes a few seconds when you first visit the page,
242-
and the generated JavaScript is currently large.
243-
The generated JavaScript will be smaller once
244-
Angular's transformer becomes available.
227+
and then run it by visiting <b>http://localhost:8080</b> in any modern browser.
228+
Pub serve generates the JavaScript on the fly,
229+
which takes a few seconds when you first visit the page.
230+
231+
p.
232+
Once the app is running,
233+
you should see <b>Hello Alice</b> in your browser window.
245234

246-
// [PENDING: Update when transformer is working!]
247235

248-
// WHAT'S NEXT... ##########################
236+
//- STEP 7 - Generate JavaScript ##########################
237+
.l-main-section
238+
239+
h2#section-angular-run-app 7. Generate JavaScript
240+
241+
p.
242+
Before you can deploy your app, you need to generate JavaScript files.
243+
Compiling your Dart code to JavaScript is easy:
244+
just run <code>pub build</code>.
245+
246+
code-example(language="basic").
247+
&gt; <span class="blk">pub build</span>
248+
Loading source assets...
249+
Loading angular2 transformers...
250+
INFO: Formatter is being overwritten.
251+
Building hello_world... (3.1s)
252+
[Info from Dart2JS]:
253+
Compiling hello_world|web/main.dart...
254+
[Info from Dart2JS]:
255+
Took 0:00:16.123086 to compile hello_world|web/main.dart.
256+
Built 41 files to "build".
257+
//- REGENERATE THIS OUTPUT - or delete it? - when updating from 2.0.0-alpha.25
258+
259+
p.
260+
The generated JavaScript appears, along with supporting files,
261+
under the <code>build</code> directory.
262+
263+
p.
264+
When you generate JavaScript for an Angular app,
265+
be sure you use the Angular transformer.
266+
It analyzes your code,
267+
converting reflection-using code to static code
268+
that Dart's build tools can compile to faster, smaller JavaScript.
269+
The highlighted lines in <code>pubspec.yaml</code>
270+
configure the Angular transformer:
271+
272+
code-example(language="yaml" format="linenums").
273+
name: hello_world
274+
version: 0.0.1
275+
dependencies:
276+
angular2: 2.0.0-alpha.25
277+
browser: ^0.10.0+2
278+
<span class="pnk">transformers:
279+
- angular2:
280+
entry_points: web/main.dart</span>
281+
p.
282+
The <code>entry_points</code> entry
283+
identifies the Dart file in your app
284+
that has a <code>main()</code> function.
285+
For more information, see the
286+
<a href="https://github.com/angular/angular/wiki/Angular-2-Dart-Transformer">Angular
287+
transformer wiki page</a>.
288+
289+
//- WHAT'S NEXT... ##########################
249290
.l-main-section
250291
h2#section-transpile Great job! Next step...
251292

252293
p.
253-
We plan to add a developer guide soon.
254-
Until then, check out <a href="resources.html">Angular Resources</a>.
255-
To learn more about Dart, go to
256-
<a href="https://www.dartlang.org">dartlang.org</a>.
294+
Follow the <a href="guide">developer guide</a>
295+
to continue playing with Angular 2 for Dart.
296+
297+
p.
298+
Or read more about Angular or Dart:
299+
300+
ul
301+
li
302+
<a href="resources.html">Angular resources</a>
303+
li
304+
<a href="https://www.dartlang.org">dartlang.org</a>

0 commit comments

Comments
 (0)