OLD | NEW |
---|---|
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:async/async.dart'; | 9 import 'package:async/async.dart'; |
10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
(...skipping 17 matching lines...) Expand all Loading... | |
28 /// barback doesn't provide a mechanism to tell you which files have changed. | 28 /// barback doesn't provide a mechanism to tell you which files have changed. |
29 class DartDevcEnvironment { | 29 class DartDevcEnvironment { |
30 final _AssetCache _assetCache; | 30 final _AssetCache _assetCache; |
31 final Barback _barback; | 31 final Barback _barback; |
32 final Map<String, String> _environmentConstants; | 32 final Map<String, String> _environmentConstants; |
33 final BarbackMode _mode; | 33 final BarbackMode _mode; |
34 ModuleReader _moduleReader; | 34 ModuleReader _moduleReader; |
35 final PackageGraph _packageGraph; | 35 final PackageGraph _packageGraph; |
36 ScratchSpace _scratchSpace; | 36 ScratchSpace _scratchSpace; |
37 | 37 |
38 static final _sdkResources = <String, String>{ | |
39 'dart_sdk.js': 'lib/dev_compiler/amd/dart_sdk.js', | |
40 'require.js': 'lib/dev_compiler/amd/require.js', | |
41 'dart_stack_trace_mapper.js': | |
42 'lib/dev_compiler/web/dart_stack_trace_mapper.js', | |
43 'ddc_web_compiler.js': 'lib/dev_compiler/web/ddc_web_compiler.js', | |
44 }; | |
45 | |
38 DartDevcEnvironment( | 46 DartDevcEnvironment( |
39 this._barback, this._mode, this._environmentConstants, this._packageGraph) | 47 this._barback, this._mode, this._environmentConstants, this._packageGraph) |
40 : _assetCache = new _AssetCache(_packageGraph) { | 48 : _assetCache = new _AssetCache(_packageGraph) { |
41 _moduleReader = new ModuleReader(_readModule); | 49 _moduleReader = new ModuleReader(_readModule); |
42 _scratchSpace = new ScratchSpace(_getAsset); | 50 _scratchSpace = new ScratchSpace(_getAsset); |
43 } | 51 } |
44 | 52 |
45 /// Deletes the [_scratchSpace] and shuts down the workers. | 53 /// Deletes the [_scratchSpace] and shuts down the workers. |
46 Future cleanUp() { | 54 Future cleanUp() { |
47 return Future.wait([ | 55 return Future.wait([ |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 } | 92 } |
85 | 93 |
86 // Build all required modules for the apps that were discovered. | 94 // Build all required modules for the apps that were discovered. |
87 var allFutureAssets = <Future<Asset>>[]; | 95 var allFutureAssets = <Future<Asset>>[]; |
88 for (var module in modulesToBuild) { | 96 for (var module in modulesToBuild) { |
89 allFutureAssets | 97 allFutureAssets |
90 .addAll(_buildAsset(module.jsId, logError: logError).values); | 98 .addAll(_buildAsset(module.jsId, logError: logError).values); |
91 } | 99 } |
92 // Copy all JS resoureces for each of the app dirs that were discovered. | 100 // Copy all JS resoureces for each of the app dirs that were discovered. |
93 for (var dir in appDirs) { | 101 for (var dir in appDirs) { |
94 allFutureAssets | 102 for (var name in _sdkResources.keys) { |
95 ..add(_buildJsResource(new AssetId(_packageGraph.entrypoint.root.name, | 103 allFutureAssets.add(_buildJsResource(new AssetId( |
96 p.url.join(dir, 'dart_sdk.js')))) | 104 _packageGraph.entrypoint.root.name, p.url.join(dir, name)))); |
97 ..add(_buildJsResource(new AssetId(_packageGraph.entrypoint.root.name, | 105 } |
98 p.url.join(dir, 'require.js')))); | |
99 } | 106 } |
100 var assets = await Future.wait(allFutureAssets); | 107 var assets = await Future.wait(allFutureAssets); |
101 jsAssets.addAll(assets.where((asset) => asset != null)); | 108 jsAssets.addAll(assets.where((asset) => asset != null)); |
102 | 109 |
103 return jsAssets; | 110 return jsAssets; |
104 } catch (e) { | 111 } catch (e) { |
105 logError(e); | 112 logError(e); |
106 return new AssetSet(); | 113 return new AssetSet(); |
107 } | 114 } |
108 } | 115 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 } else if (id.path.endsWith(linkedSummaryExtension)) { | 154 } else if (id.path.endsWith(linkedSummaryExtension)) { |
148 assets = { | 155 assets = { |
149 id: createLinkedSummary(id, _moduleReader, _scratchSpace, logError) | 156 id: createLinkedSummary(id, _moduleReader, _scratchSpace, logError) |
150 }; | 157 }; |
151 } else if (_isEntrypointId(id)) { | 158 } else if (_isEntrypointId(id)) { |
152 var dartId = _entrypointDartId(id); | 159 var dartId = _entrypointDartId(id); |
153 if (dartId != null) { | 160 if (dartId != null) { |
154 assets = bootstrapDartDevcEntrypoint( | 161 assets = bootstrapDartDevcEntrypoint( |
155 dartId, _mode, _moduleReader, _barback.getAssetById); | 162 dartId, _mode, _moduleReader, _barback.getAssetById); |
156 } | 163 } |
157 } else if (id.path.endsWith('require.js') || | 164 } else if (_hasJsResource(id)) { |
158 id.path.endsWith('dart_sdk.js')) { | |
159 assets = {id: _buildJsResource(id)}; | 165 assets = {id: _buildJsResource(id)}; |
160 } else if (id.path.endsWith('require.js.map') || | 166 } else if (id.path.endsWith('require.js.map') || |
161 id.path.endsWith('dart_sdk.js.map')) { | 167 id.path.endsWith('dart_sdk.js.map')) { |
162 assets = {id: new Future.error(new AssetNotFoundException(id))}; | 168 assets = {id: new Future.error(new AssetNotFoundException(id))}; |
163 } else if (id.path.endsWith('.js') || id.path.endsWith('.js.map')) { | 169 } else if (id.path.endsWith('.js') || id.path.endsWith('.js.map')) { |
164 var jsId = id.extension == '.map' ? id.changeExtension('') : id; | 170 var jsId = id.extension == '.map' ? id.changeExtension('') : id; |
165 assets = createDartdevcModule(jsId, _moduleReader, _scratchSpace, | 171 assets = createDartdevcModule(jsId, _moduleReader, _scratchSpace, |
166 _environmentConstants, _mode, logError); | 172 _environmentConstants, _mode, logError); |
167 // Pre-emptively start building all transitive JS deps under the | 173 // Pre-emptively start building all transitive JS deps under the |
168 // assumption they will be needed in the near future. | 174 // assumption they will be needed in the near future. |
(...skipping 26 matching lines...) Expand all Loading... | |
195 asset.id.package == id.package && | 201 asset.id.package == id.package && |
196 asset.id.extension == '.dart' && | 202 asset.id.extension == '.dart' && |
197 topLevelDir(asset.id.path) == moduleDir); | 203 topLevelDir(asset.id.path) == moduleDir); |
198 if (moduleAssets.isEmpty) throw new AssetNotFoundException(id); | 204 if (moduleAssets.isEmpty) throw new AssetNotFoundException(id); |
199 var moduleMode = | 205 var moduleMode = |
200 moduleDir == 'lib' ? ModuleMode.public : ModuleMode.private; | 206 moduleDir == 'lib' ? ModuleMode.public : ModuleMode.private; |
201 var modules = await computeModules(moduleMode, moduleAssets); | 207 var modules = await computeModules(moduleMode, moduleAssets); |
202 var encoded = JSON.encode(modules); | 208 var encoded = JSON.encode(modules); |
203 return new Asset.fromString(id, encoded); | 209 return new Asset.fromString(id, encoded); |
204 } | 210 } |
205 | 211 |
Bob Nystrom
2017/05/22 20:21:44
Doc comment?
| |
206 /// Builds the `dart_sdk.js` or `require.js` assets by copying them from the | 212 bool _hasJsResource(AssetId id) => |
207 /// SDK. | 213 _sdkResources.containsKey(p.url.basename(id.path)); |
214 | |
215 /// Builds [_sdkResources] assets by copying them from the SDK. | |
208 Future<Asset> _buildJsResource(AssetId id) async { | 216 Future<Asset> _buildJsResource(AssetId id) async { |
209 var sdk = cli_util.getSdkDir(); | 217 var sdk = cli_util.getSdkDir(); |
210 | 218 var basename = p.url.basename(id.path); |
211 switch (p.url.basename(id.path)) { | 219 var resourcePath = _sdkResources[basename]; |
212 case 'dart_sdk.js': | 220 if (resourcePath == null) return null; |
213 var sdkAmdJsPath = | 221 return new Asset.fromPath(id, p.url.join(sdk.path, resourcePath)); |
214 p.url.join(sdk.path, 'lib/dev_compiler/amd/dart_sdk.js'); | |
215 return new Asset.fromPath(id, sdkAmdJsPath); | |
216 case 'require.js': | |
217 var requireJsPath = | |
218 p.url.join(sdk.path, 'lib/dev_compiler/amd/require.js'); | |
219 return new Asset.fromFile(id, new File(requireJsPath)); | |
220 default: | |
221 return null; | |
222 } | |
223 } | 222 } |
224 | 223 |
225 /// Whether or not this looks like a request for an entrypoint or bootstrap | 224 /// Whether or not this looks like a request for an entrypoint or bootstrap |
226 /// file. | 225 /// file. |
227 bool _isEntrypointId(AssetId id) => | 226 bool _isEntrypointId(AssetId id) => |
228 id.path.endsWith('.bootstrap.js') || | 227 id.path.endsWith('.bootstrap.js') || |
229 id.path.endsWith('.bootstrap.js.map') || | 228 id.path.endsWith('.bootstrap.js.map') || |
230 id.path.endsWith('.dart.js') || | 229 id.path.endsWith('.dart.js') || |
231 id.path.endsWith('.dart.js.map'); | 230 id.path.endsWith('.dart.js.map'); |
232 | 231 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 | 308 |
310 /// Reads a [Result] from the actual underlying caches, or returns `null`. | 309 /// Reads a [Result] from the actual underlying caches, or returns `null`. |
311 Future<Result> _getResult(AssetId id) { | 310 Future<Result> _getResult(AssetId id) { |
312 var packageCache = _assets[id.package]; | 311 var packageCache = _assets[id.package]; |
313 if (packageCache == null) return null; | 312 if (packageCache == null) return null; |
314 var futureResult = packageCache[id.path]; | 313 var futureResult = packageCache[id.path]; |
315 if (futureResult == null) return null; | 314 if (futureResult == null) return null; |
316 return futureResult; | 315 return futureResult; |
317 } | 316 } |
318 } | 317 } |
OLD | NEW |