|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
| 5 | +import 'dart:convert'; |
5 | 6 | import 'dart:io'; |
6 | 7 |
|
7 | 8 | import 'package:build/build.dart'; |
@@ -38,21 +39,34 @@ void validateOptions(Map<String, dynamic> config, List<String> supportedOptions, |
38 | 39 | } |
39 | 40 | } |
40 | 41 |
|
41 | | -/// Fixes up the [uris] from a source map so they make sense in a browser |
42 | | -/// context. |
| 42 | +/// If [id] exists, assume it is a source map and fix up the source uris from |
| 43 | +/// it so they make sense in a browser context, then write the modified version |
| 44 | +/// using [writer]. |
43 | 45 | /// |
44 | 46 | /// - Strips the scheme from the uri |
45 | 47 | /// - Strips the top level directory if its not `packages` |
46 | | -/// |
47 | | -/// Copied to `web/stack_trace_mapper.dart`, these need to be kept in sync. |
48 | | -List<String> fixSourceMapSources(List<String> uris) { |
49 | | - return uris.map((source) { |
| 48 | +Future<void> fixAndCopySourceMap( |
| 49 | + AssetId id, ScratchSpace scratchSpace, AssetWriter writer) async { |
| 50 | + // Copied to `web/stack_trace_mapper.dart`, these need to be kept in sync. |
| 51 | + String fixMappedSource(String source) { |
50 | 52 | var uri = Uri.parse(source); |
51 | 53 | // We only want to rewrite multi-root scheme uris. |
52 | 54 | if (uri.scheme.isEmpty) return source; |
53 | 55 | var newSegments = uri.pathSegments.first == 'packages' |
54 | 56 | ? uri.pathSegments |
55 | 57 | : uri.pathSegments.skip(1); |
56 | 58 | return Uri(path: p.url.joinAll(['/', ...newSegments])).toString(); |
57 | | - }).toList(); |
| 59 | + } |
| 60 | + |
| 61 | + var file = scratchSpace.fileFor(id); |
| 62 | + if (await file.exists()) { |
| 63 | + var content = await file.readAsString(); |
| 64 | + var json = jsonDecode(content) as Map<String, Object?>; |
| 65 | + var sources = json['sources'] as List<Object?>; |
| 66 | + // Modify `sources` in place for fewer allocations. |
| 67 | + for (var i = 0; i < sources.length; i++) { |
| 68 | + sources[i] = fixMappedSource(sources[i] as String); |
| 69 | + } |
| 70 | + await writer.writeAsString(id, jsonEncode(json)); |
| 71 | + } |
58 | 72 | } |
0 commit comments