| // Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'dart:convert'; | |
| import 'dart:io'; | |
| import 'package:args/args.dart'; | |
| import 'package:expect/expect.dart'; | |
| import 'package:path/path.dart' as path; | |
| import '../util.dart'; | |
| const String helperJsLoadIdLookupToken = 'LOAD_ID_LOOKUP'; | |
| const String helperJsModuleDirToken = 'MODULE_DIR'; | |
| final String goldenPath = | |
| '${path.dirname(Platform.script.path)}/data/deferred_map.golden.json'; | |
| final String mainDart = '${path.dirname(Platform.script.path)}/data/main.dart'; | |
| final String helperJs = '${path.dirname(Platform.script.path)}/helper.js'; | |
| Future<void> main(List<String> args) async { | |
| final parser = ArgParser() | |
| ..addFlag('update-golden', abbr: 'g', negatable: false); | |
| final argsResult = parser.parse(args); | |
| await withTempDir((tmpDirPath) async { | |
| final tmpDir = File(tmpDirPath); | |
| final deferredMapUri = tmpDir.uri.resolve('deferred_map.json'); | |
| final outFilename = '${tmpDir.path}/out.wasm'; | |
| // Compile the test | |
| await run([ | |
| dartAotExecutable, | |
| dart2wasmSnapshot, | |
| mainDart, | |
| '--platform=$platformDill', | |
| '--enable-deferred-loading', | |
| '--deferred-map=$deferredMapUri', | |
| outFilename, | |
| ]); | |
| final deferredMapContent = await File.fromUri( | |
| deferredMapUri, | |
| ).readAsString(); | |
| final goldenFile = File(goldenPath); | |
| if (argsResult.flag('update-golden')) { | |
| print('Updating golden:\n$deferredMapContent'); | |
| await goldenFile.writeAsString(deferredMapContent); | |
| return; | |
| } | |
| // Ensure the deferred map JSON matches the golden. | |
| Expect.equals(await goldenFile.readAsString(), deferredMapContent); | |
| // Extract a map from each load ID to its corresponding module. | |
| final loadIdToModules = <String, List<String>>{}; | |
| final json = const JsonDecoder().convert(deferredMapContent); | |
| (json as Map).forEach((_, info) { | |
| ((info as Map)['imports'] as Map).forEach((loadId, modules) { | |
| loadIdToModules[loadId] = (modules as List).cast<String>(); | |
| }); | |
| }); | |
| // Fill in the helper JS file with the module mapping. | |
| final loadIdLookup = const JsonEncoder().convert(loadIdToModules); | |
| final helpersTemplate = await File(helperJs).readAsString(); | |
| final populatedHelperJs = helpersTemplate | |
| .replaceAll(helperJsLoadIdLookupToken, loadIdLookup) | |
| .replaceAll(helperJsModuleDirToken, tmpDir.path); | |
| final helperFile = File.fromUri(tmpDir.uri.resolve('helper.js')); | |
| await helperFile.writeAsString(populatedHelperJs); | |
| // Load the helper JS and run the compiled code | |
| await run([ | |
| 'pkg/dart2wasm/tool/run_benchmark', | |
| helperFile.path, | |
| outFilename, | |
| ]); | |
| }); | |
| } |