Skip to content

Commit 387401f

Browse files
authored
Support wasm source maps (#3759)
Fixes #3757
1 parent 144c598 commit 387401f

11 files changed

+54
-50
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pubspec.lock
77
# Common build output
88
/*/build/
99

10-
# Include .packages files from tests which are hand coded
11-
!build_runner_core/test/fixtures/**/.dart_tool/
10+
# Include package config files from tests which are hand coded
11+
!build_runner_core/test/fixtures/**/.dart_tool/package_config.json
1212
!build_runner_core/test/fixtures/**/pubspec.lock
1313

1414
# Extra files from dart2js that we don't want

build_web_compilers/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.1.0-beta.2
2+
3+
- Add source maps for dart2wasm builds.
4+
15
## 4.1.0-beta.1
26

37
- Fix loading compiled modules from subdirectories.

build_web_compilers/build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ builders:
113113
- .digests
114114
- .dart.ddc_merged_metadata
115115
- .wasm
116+
- .wasm.map
116117
- .mjs
117118
required_inputs:
118119
- .dart

build_web_compilers/lib/src/common.dart

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert';
56
import 'dart:io';
67

78
import 'package:build/build.dart';
@@ -38,21 +39,34 @@ void validateOptions(Map<String, dynamic> config, List<String> supportedOptions,
3839
}
3940
}
4041

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].
4345
///
4446
/// - Strips the scheme from the uri
4547
/// - 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) {
5052
var uri = Uri.parse(source);
5153
// We only want to rewrite multi-root scheme uris.
5254
if (uri.scheme.isEmpty) return source;
5355
var newSegments = uri.pathSegments.first == 'packages'
5456
? uri.pathSegments
5557
: uri.pathSegments.skip(1);
5658
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+
}
5872
}

build_web_compilers/lib/src/dart2js_bootstrap.dart

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'package:glob/glob.dart';
1414
import 'package:glob/list_local_fs.dart';
1515
import 'package:path/path.dart' as p;
1616
import 'package:pool/pool.dart';
17-
import 'package:scratch_space/scratch_space.dart';
1817

1918
import 'common.dart';
2019
import 'platforms.dart';
@@ -140,28 +139,16 @@ https://github.com/dart-lang/build/blob/master/docs/faq.md#how-can-i-resolve-ski
140139
// Explicitly write out the original js file and sourcemap - we can't output
141140
// these as part of the archive because they already have asset nodes.
142141
await scratchSpace.copyOutput(jsOutputId, buildStep);
143-
var jsSourceMapId =
144-
dartEntrypointId.changeExtension(jsEntrypointSourceMapExtension);
145-
await _copyModifiedSourceMap(jsSourceMapId, scratchSpace, buildStep);
142+
await fixAndCopySourceMap(
143+
dartEntrypointId.changeExtension(jsEntrypointSourceMapExtension),
144+
scratchSpace,
145+
buildStep);
146146
} else {
147147
log.severe('ExitCode:${result.exitCode}\nStdOut:\n${result.stdout}\n'
148148
'StdErr:\n${result.stderr}');
149149
}
150150
}
151151

152-
/// If [id] exists, modifies it to something the browser can understand and
153-
/// writes it using [writer].
154-
Future<void> _copyModifiedSourceMap(
155-
AssetId id, ScratchSpace scratchSpace, AssetWriter writer) async {
156-
var file = scratchSpace.fileFor(id);
157-
if (await file.exists()) {
158-
var content = await file.readAsString();
159-
var json = jsonDecode(content) as Map<String, Object?>;
160-
json['sources'] = fixSourceMapSources((json['sources'] as List).cast());
161-
await writer.writeAsString(id, jsonEncode(json));
162-
}
163-
}
164-
165152
final _resourcePool = Pool(maxWorkersPerTask);
166153

167154
const _dart2jsVmArgsEnvVar = 'BUILD_DART2JS_VM_ARGS';

build_web_compilers/lib/src/dart2wasm_bootstrap.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ https://github.com/dart-lang/build/blob/master/docs/faq.md#how-can-i-resolve-ski
107107
if (result.exitCode == 0 && await wasmOutputFile.exists()) {
108108
log.info('${result.stdout}\n${result.stderr}');
109109

110-
await scratchSpace.copyOutput(
111-
dartEntrypointId.changeExtension(wasmExtension), buildStep);
110+
await scratchSpace.copyOutput(wasmOutputId, buildStep);
111+
112+
await fixAndCopySourceMap(
113+
dartEntrypointId.changeExtension(wasmSourceMapExtension),
114+
scratchSpace,
115+
buildStep);
112116

113117
final loaderContents = await scratchSpace
114118
.fileFor(dartEntrypointId.changeExtension(moduleJsExtension))

build_web_compilers/lib/src/dev_compiler_builder.dart

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,22 +264,17 @@ Future<void> _createDevCompilerModule(
264264
}
265265

266266
if (debugMode) {
267-
// We need to modify the sources in the sourcemap to remove the custom
268-
// `multiRootScheme` that we use.
269-
var sourceMapId =
270-
module.primarySource.changeExtension(jsSourceMapExtension);
271-
var file = scratchSpace.fileFor(sourceMapId);
272-
var content = await file.readAsString();
273-
var json = jsonDecode(content) as Map<String, Object?>;
274-
json['sources'] = fixSourceMapSources((json['sources'] as List).cast());
275-
await buildStep.writeAsString(sourceMapId, jsonEncode(json));
267+
await fixAndCopySourceMap(
268+
module.primarySource.changeExtension(jsSourceMapExtension),
269+
scratchSpace,
270+
buildStep);
276271

277272
// Copy the metadata output, modifying its contents to remove the temp
278273
// directory from paths
279274
var metadataId = module.primarySource.changeExtension(metadataExtension);
280-
file = scratchSpace.fileFor(metadataId);
281-
content = await file.readAsString();
282-
json = jsonDecode(content) as Map<String, Object?>;
275+
var file = scratchSpace.fileFor(metadataId);
276+
var content = await file.readAsString();
277+
var json = jsonDecode(content) as Map<String, Object?>;
283278
_fixMetadataSources(json, scratchSpace.tempDir.uri);
284279
await buildStep.writeAsString(metadataId, jsonEncode(json));
285280

build_web_compilers/lib/src/sdk_js_compile_builder.dart

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:convert';
76
import 'dart:io';
87

98
import 'package:bazel_worker/bazel_worker.dart';
@@ -124,12 +123,7 @@ Future<void> _createDevCompilerModule(
124123

125124
// Copy the output back using the buildStep.
126125
await scratchSpace.copyOutput(jsOutputId, buildStep);
127-
// We need to modify the sources in the sourcemap to remove the custom
128-
// `multiRootScheme` that we use.
129-
var sourceMapId = jsOutputId.changeExtension(_jsSourceMapExtension);
130-
var file = scratchSpace.fileFor(sourceMapId);
131-
var content = await file.readAsString();
132-
var json = jsonDecode(content) as Map<String, Object?>;
133-
json['sources'] = fixSourceMapSources((json['sources'] as List).cast());
134-
await buildStep.writeAsString(sourceMapId, jsonEncode(json));
126+
127+
await fixAndCopySourceMap(jsOutputId.changeExtension(_jsSourceMapExtension),
128+
scratchSpace, buildStep);
135129
}

build_web_compilers/lib/src/web_entrypoint_builder.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'dev_compiler_bootstrap.dart';
1919
const ddcBootstrapExtension = '.dart.bootstrap.js';
2020
const jsEntrypointExtension = '.dart.js';
2121
const wasmExtension = '.wasm';
22+
const wasmSourceMapExtension = '.wasm.map';
2223
const moduleJsExtension = '.mjs';
2324
const jsEntrypointSourceMapExtension = '.dart.js.map';
2425
const jsEntrypointArchiveExtension = '.dart.js.tar.gz';
@@ -244,6 +245,7 @@ final class EntrypointBuilderOptions {
244245
if (optionsFor(WebCompiler.Dart2Wasm) case final dart2wasm?) ...[
245246
dart2wasm.extension,
246247
wasmExtension,
248+
wasmSourceMapExtension,
247249
],
248250
if (loaderExtension case final loader?) loader,
249251
]

build_web_compilers/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: build_web_compilers
2-
version: 4.1.0-beta.1
2+
version: 4.1.0-beta.2
33
description: Builder implementations wrapping the dart2js and DDC compilers.
44
repository: https://github.com/dart-lang/build/tree/master/build_web_compilers
55
# This package can't be part of the workspace because it requires a very recent

0 commit comments

Comments
 (0)