Chromium Code Reviews| 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 var topLevelHandler = (shelf.Request request) { | 261 var topLevelHandler = (shelf.Request request) { |
| 262 var path = request.url.path; | 262 var path = request.url.path; |
| 263 if (path.endsWith('.dart')) { | 263 // Prefer generated code |
| 264 return sourceHandler(request); | 264 var response = generatedHandler(request); |
| 265 } else { | 265 if (response.statusCode == 404) { |
|
Jennifer Messerly
2016/01/27 20:50:02
this feels a little bit off wrong in the architect
| |
| 266 return mainHandler(request); | 266 // Fall back on original sources |
| 267 response = sourceHandler(request); | |
| 267 } | 268 } |
| 269 return response; | |
| 268 }; | 270 }; |
| 269 | 271 |
| 270 var handler = const shelf.Pipeline() | 272 var handler = const shelf.Pipeline() |
| 271 .addMiddleware(rebuildAndCache) | 273 .addMiddleware(rebuildAndCache) |
| 272 .addHandler(topLevelHandler); | 274 .addHandler(topLevelHandler); |
| 273 await shelf.serve(handler, host, port); | 275 await shelf.serve(handler, host, port); |
| 274 print('Serving $_entryPath at http://$host:$port/'); | 276 print('Serving $_entryPath at http://$host:$port/'); |
| 275 // Give the compiler a head start. This is not needed for correctness, | 277 // 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 | 278 // but will likely speed up the first load. Regardless of whether compile |
| 277 // succeeds we should still start the server. | 279 // 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]) { | 322 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 321 var src = resolver.resolveAbsolute(uri, actualUri); | 323 var src = resolver.resolveAbsolute(uri, actualUri); |
| 322 return src.exists() ? src : null; | 324 return src.exists() ? src : null; |
| 323 } | 325 } |
| 324 | 326 |
| 325 Uri restoreAbsolute(Source source) => resolver.restoreAbsolute(source); | 327 Uri restoreAbsolute(Source source) => resolver.restoreAbsolute(source); |
| 326 } | 328 } |
| 327 | 329 |
| 328 final _log = new Logger('dev_compiler.src.server'); | 330 final _log = new Logger('dev_compiler.src.server'); |
| 329 final _earlyErrorResult = new CheckerResults(const [], true); | 331 final _earlyErrorResult = new CheckerResults(const [], true); |
| OLD | NEW |