OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Development server that compiles Dart to JS on the fly. | 5 /// Development server that compiles Dart to JS on the fly. |
6 library dev_compiler.src.server; | 6 library dev_compiler.src.server; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 // TODO(jmesserly): this logic is duplicated in a few places | 247 // TODO(jmesserly): this logic is duplicated in a few places |
248 this._entryPath = compiler.options.sourceOptions.useImplicitHtml | 248 this._entryPath = compiler.options.sourceOptions.useImplicitHtml |
249 ? SourceResolverOptions.implicitHtmlFile | 249 ? SourceResolverOptions.implicitHtmlFile |
250 : entryPath; | 250 : entryPath; |
251 | 251 |
252 Future start() async { | 252 Future start() async { |
253 // Create output directory if needed. shelf_static will fail otherwise. | 253 // Create output directory if needed. shelf_static will fail otherwise. |
254 var out = new Directory(outDir); | 254 var out = new Directory(outDir); |
255 if (!await out.exists()) await out.create(recursive: true); | 255 if (!await out.exists()) await out.create(recursive: true); |
256 | 256 |
257 var mainHandler = | 257 var generatedHandler = |
258 shelf_static.createStaticHandler(outDir, defaultDocument: _entryPath); | 258 shelf_static.createStaticHandler(outDir, defaultDocument: _entryPath); |
259 var sourceHandler = shelf_static.createStaticHandler(compiler.inputBaseDir, | 259 var sourceHandler = shelf_static.createStaticHandler(compiler.inputBaseDir, |
260 serveFilesOutsidePath: true); | 260 serveFilesOutsidePath: true); |
| 261 // TODO(vsm): Is there a better builtin way to compose these handlers? |
261 var topLevelHandler = (shelf.Request request) { | 262 var topLevelHandler = (shelf.Request request) { |
262 var path = request.url.path; | 263 var path = request.url.path; |
263 if (path.endsWith('.dart')) { | 264 // Prefer generated code |
264 return sourceHandler(request); | 265 var response = generatedHandler(request); |
265 } else { | 266 if (response.statusCode == 404) { |
266 return mainHandler(request); | 267 // Fall back on original sources |
| 268 response = sourceHandler(request); |
267 } | 269 } |
| 270 return response; |
268 }; | 271 }; |
269 | 272 |
270 var handler = const shelf.Pipeline() | 273 var handler = const shelf.Pipeline() |
271 .addMiddleware(rebuildAndCache) | 274 .addMiddleware(rebuildAndCache) |
272 .addHandler(topLevelHandler); | 275 .addHandler(topLevelHandler); |
273 await shelf.serve(handler, host, port); | 276 await shelf.serve(handler, host, port); |
274 print('Serving $_entryPath at http://$host:$port/'); | 277 print('Serving $_entryPath at http://$host:$port/'); |
275 // Give the compiler a head start. This is not needed for correctness, | 278 // Give the compiler a head start. This is not needed for correctness, |
276 // but will likely speed up the first load. Regardless of whether compile | 279 // but will likely speed up the first load. Regardless of whether compile |
277 // succeeds we should still start the server. | 280 // succeeds we should still start the server. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | 323 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
321 var src = resolver.resolveAbsolute(uri, actualUri); | 324 var src = resolver.resolveAbsolute(uri, actualUri); |
322 return src.exists() ? src : null; | 325 return src.exists() ? src : null; |
323 } | 326 } |
324 | 327 |
325 Uri restoreAbsolute(Source source) => resolver.restoreAbsolute(source); | 328 Uri restoreAbsolute(Source source) => resolver.restoreAbsolute(source); |
326 } | 329 } |
327 | 330 |
328 final _log = new Logger('dev_compiler.src.server'); | 331 final _log = new Logger('dev_compiler.src.server'); |
329 final _earlyErrorResult = new CheckerResults(const [], true); | 332 final _earlyErrorResult = new CheckerResults(const [], true); |
OLD | NEW |