|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | + |
| 7 | +import 'package:dwds/src/debugging/metadata/provider.dart'; |
| 8 | +import 'package:dwds/src/loaders/ddc.dart'; |
| 9 | +import 'package:dwds/src/loaders/strategy.dart'; |
| 10 | +import 'package:dwds/src/readers/asset_reader.dart'; |
| 11 | +import 'package:dwds/src/services/expression_compiler.dart'; |
| 12 | +import 'package:shelf/shelf.dart'; |
| 13 | + |
| 14 | +// TODO(srujzs): This is mostly a copy of `DdcStrategy`. Some of the |
| 15 | +// functionality in here may not make sense with the library bundle format yet. |
| 16 | +class DdcLibraryBundleStrategy extends LoadStrategy { |
| 17 | + @override |
| 18 | + final ReloadConfiguration reloadConfiguration; |
| 19 | + |
| 20 | + /// Returns a map of module name to corresponding server path (excluding .js) |
| 21 | + /// for the provided Dart application entrypoint. |
| 22 | + /// |
| 23 | + /// For example: |
| 24 | + /// |
| 25 | + /// web/main -> main.ddc |
| 26 | + /// packages/path/path -> packages/path/path.ddc |
| 27 | + /// |
| 28 | + final Future<Map<String, String>> Function(MetadataProvider metadataProvider) |
| 29 | + _moduleProvider; |
| 30 | + |
| 31 | + /// Returns a map of module name to corresponding digest value. |
| 32 | + /// |
| 33 | + /// For example: |
| 34 | + /// |
| 35 | + /// web/main -> 8363b363f74b41cac955024ab8b94a3f |
| 36 | + /// packages/path/path -> d348c2a4647e998011fe305f74f22961 |
| 37 | + /// |
| 38 | + final Future<Map<String, String>> Function(MetadataProvider metadataProvider) |
| 39 | + // ignore: unused_field |
| 40 | + _digestsProvider; |
| 41 | + |
| 42 | + /// Returns the module for the corresponding server path. |
| 43 | + /// |
| 44 | + /// For example: |
| 45 | + /// |
| 46 | + /// /packages/path/path.ddc.js -> packages/path/path |
| 47 | + /// |
| 48 | + final Future<String?> Function( |
| 49 | + MetadataProvider metadataProvider, |
| 50 | + String sourcePath, |
| 51 | + ) _moduleForServerPath; |
| 52 | + |
| 53 | + /// Returns a map from module id to module info. |
| 54 | + /// |
| 55 | + /// For example: |
| 56 | + /// |
| 57 | + /// web/main -> {main.ddc.full.dill, main.ddc.dill} |
| 58 | + /// |
| 59 | + final Future<Map<String, ModuleInfo>> Function( |
| 60 | + MetadataProvider metadataProvider, |
| 61 | + ) _moduleInfoForProvider; |
| 62 | + |
| 63 | + /// Returns the server path for the provided module. |
| 64 | + /// |
| 65 | + /// For example: |
| 66 | + /// |
| 67 | + /// web/main -> main.ddc.js |
| 68 | + /// |
| 69 | + final Future<String?> Function( |
| 70 | + MetadataProvider metadataProvider, |
| 71 | + String module, |
| 72 | + ) _serverPathForModule; |
| 73 | + |
| 74 | + /// Returns the source map path for the provided module. |
| 75 | + /// |
| 76 | + /// For example: |
| 77 | + /// |
| 78 | + /// web/main -> main.ddc.js.map |
| 79 | + /// |
| 80 | + final Future<String?> Function( |
| 81 | + MetadataProvider metadataProvider, |
| 82 | + String module, |
| 83 | + ) _sourceMapPathForModule; |
| 84 | + |
| 85 | + /// Returns the server path for the app uri. |
| 86 | + /// |
| 87 | + /// For example: |
| 88 | + /// |
| 89 | + /// org-dartlang-app://web/main.dart -> main.dart |
| 90 | + /// |
| 91 | + /// Will return `null` if the provided uri is not |
| 92 | + /// an app URI. |
| 93 | + final String? Function(String appUri) _serverPathForAppUri; |
| 94 | + |
| 95 | + /// Returns the relative path in google3, determined by the [absolutePath]. |
| 96 | + /// |
| 97 | + /// Returns `null` if not a google3 app. |
| 98 | + final String? Function(String absolutePath) _g3RelativePath; |
| 99 | + |
| 100 | + final BuildSettings _buildSettings; |
| 101 | + |
| 102 | + DdcLibraryBundleStrategy( |
| 103 | + this.reloadConfiguration, |
| 104 | + this._moduleProvider, |
| 105 | + this._digestsProvider, |
| 106 | + this._moduleForServerPath, |
| 107 | + this._serverPathForModule, |
| 108 | + this._sourceMapPathForModule, |
| 109 | + this._serverPathForAppUri, |
| 110 | + this._moduleInfoForProvider, |
| 111 | + AssetReader assetReader, |
| 112 | + this._buildSettings, |
| 113 | + this._g3RelativePath, |
| 114 | + String? packageConfigPath, |
| 115 | + ) : super(assetReader, packageConfigPath: packageConfigPath); |
| 116 | + |
| 117 | + @override |
| 118 | + Handler get handler => (request) async { |
| 119 | + // TODO(markzipan): Implement a hot restarter that uses digests for |
| 120 | + // the DDC module system. |
| 121 | + return Response.notFound(request.url.toString()); |
| 122 | + }; |
| 123 | + |
| 124 | + @override |
| 125 | + String get id => 'ddc-library-bundle'; |
| 126 | + |
| 127 | + // DDC doesn't have a 'ddc-library-bundle' format flag. Instead, it treats the |
| 128 | + // combination of the DDC module format and canary mode as the DDC library |
| 129 | + // bundle format, so we just pass 'ddc' here. |
| 130 | + @override |
| 131 | + String get moduleFormat => 'ddc'; |
| 132 | + |
| 133 | + @override |
| 134 | + String get loadLibrariesModule => 'ddc_module_loader.ddk.js'; |
| 135 | + |
| 136 | + // TODO(srujzs): Refactor code that uses this to avoid loading individual |
| 137 | + // libraries, as that's no longer supported in the new module format. |
| 138 | + @override |
| 139 | + String get loadModuleSnippet => |
| 140 | + "function() { throw new Error('LoadStrategy.loadModuleSnippet is used. " |
| 141 | + "This is currently unsupported in the DDC library bundle format.'); }"; |
| 142 | + |
| 143 | + @override |
| 144 | + Future<String> bootstrapFor(String entrypoint) async => |
| 145 | + await _ddcLoaderSetup(entrypoint); |
| 146 | + |
| 147 | + @override |
| 148 | + String loadClientSnippet(String clientScript) => |
| 149 | + 'window.\$dartLoader.forceLoadModule("$clientScript");\n'; |
| 150 | + |
| 151 | + Future<String> _ddcLoaderSetup(String entrypoint) async { |
| 152 | + final metadataProvider = metadataProviderFor(entrypoint); |
| 153 | + final modulePaths = await _moduleProvider(metadataProvider); |
| 154 | + final scripts = <Map<String, String?>>[]; |
| 155 | + modulePaths.forEach((name, path) { |
| 156 | + scripts.add(<String, String>{'src': '$path.js', 'id': name}); |
| 157 | + }); |
| 158 | + return ''' |
| 159 | +$baseUrlScript |
| 160 | +var scripts = ${const JsonEncoder.withIndent(" ").convert(scripts)}; |
| 161 | +window.\$dartLoader.loadConfig.loadScriptFn = function(loader) { |
| 162 | + loader.addScriptsToQueue(scripts, null); |
| 163 | + loader.loadEnqueuedModules(); |
| 164 | +}; |
| 165 | +window.\$dartLoader.loader.nextAttempt(); |
| 166 | +'''; |
| 167 | + } |
| 168 | + |
| 169 | + @override |
| 170 | + Future<String?> moduleForServerPath(String entrypoint, String serverPath) => |
| 171 | + _moduleForServerPath(metadataProviderFor(entrypoint), serverPath); |
| 172 | + |
| 173 | + @override |
| 174 | + Future<Map<String, ModuleInfo>> moduleInfoForEntrypoint(String entrypoint) => |
| 175 | + _moduleInfoForProvider(metadataProviderFor(entrypoint)); |
| 176 | + |
| 177 | + @override |
| 178 | + Future<String?> serverPathForModule(String entrypoint, String module) => |
| 179 | + _serverPathForModule(metadataProviderFor(entrypoint), module); |
| 180 | + |
| 181 | + @override |
| 182 | + Future<String?> sourceMapPathForModule(String entrypoint, String module) => |
| 183 | + _sourceMapPathForModule(metadataProviderFor(entrypoint), module); |
| 184 | + |
| 185 | + @override |
| 186 | + String? serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri); |
| 187 | + |
| 188 | + @override |
| 189 | + BuildSettings get buildSettings => _buildSettings; |
| 190 | + |
| 191 | + @override |
| 192 | + String? g3RelativePath(String absolutePath) => _g3RelativePath(absolutePath); |
| 193 | +} |
0 commit comments