-
Notifications
You must be signed in to change notification settings - Fork 83
Add minimal bootstrapper for ddc_library_bundle format #2523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ba7690
Add minimal bootstrapper for ddc_library_bundle format
srujzs f9e3f81
Update changelogs
srujzs a840020
Add trailing comma
srujzs 475b9ba
Revert extention -> extension change
srujzs 8894f5a
Hot reload -> hot restart in error message
srujzs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// Copyright (c) 2024, 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 'package:dwds/src/debugging/metadata/provider.dart'; | ||
import 'package:dwds/src/loaders/ddc.dart'; | ||
import 'package:dwds/src/loaders/strategy.dart'; | ||
import 'package:dwds/src/readers/asset_reader.dart'; | ||
import 'package:dwds/src/services/expression_compiler.dart'; | ||
import 'package:shelf/shelf.dart'; | ||
|
||
// TODO(srujzs): This is mostly a copy of `DdcStrategy`. Some of the | ||
// functionality in here may not make sense with the library bundle format yet. | ||
class DdcLibraryBundleStrategy extends LoadStrategy { | ||
@override | ||
final ReloadConfiguration reloadConfiguration; | ||
|
||
/// Returns a map of module name to corresponding server path (excluding .js) | ||
/// for the provided Dart application entrypoint. | ||
/// | ||
/// For example: | ||
/// | ||
/// web/main -> main.ddc | ||
/// packages/path/path -> packages/path/path.ddc | ||
/// | ||
final Future<Map<String, String>> Function(MetadataProvider metadataProvider) | ||
_moduleProvider; | ||
|
||
/// Returns a map of module name to corresponding digest value. | ||
/// | ||
/// For example: | ||
/// | ||
/// web/main -> 8363b363f74b41cac955024ab8b94a3f | ||
/// packages/path/path -> d348c2a4647e998011fe305f74f22961 | ||
/// | ||
final Future<Map<String, String>> Function(MetadataProvider metadataProvider) | ||
// ignore: unused_field | ||
_digestsProvider; | ||
|
||
/// Returns the module for the corresponding server path. | ||
/// | ||
/// For example: | ||
/// | ||
/// /packages/path/path.ddc.js -> packages/path/path | ||
/// | ||
final Future<String?> Function( | ||
MetadataProvider metadataProvider, | ||
String sourcePath, | ||
) _moduleForServerPath; | ||
|
||
/// Returns a map from module id to module info. | ||
/// | ||
/// For example: | ||
/// | ||
/// web/main -> {main.ddc.full.dill, main.ddc.dill} | ||
/// | ||
final Future<Map<String, ModuleInfo>> Function( | ||
MetadataProvider metadataProvider, | ||
) _moduleInfoForProvider; | ||
|
||
/// Returns the server path for the provided module. | ||
/// | ||
/// For example: | ||
/// | ||
/// web/main -> main.ddc.js | ||
/// | ||
final Future<String?> Function( | ||
MetadataProvider metadataProvider, | ||
String module, | ||
) _serverPathForModule; | ||
|
||
/// Returns the source map path for the provided module. | ||
/// | ||
/// For example: | ||
/// | ||
/// web/main -> main.ddc.js.map | ||
/// | ||
final Future<String?> Function( | ||
MetadataProvider metadataProvider, | ||
String module, | ||
) _sourceMapPathForModule; | ||
|
||
/// Returns the server path for the app uri. | ||
/// | ||
/// For example: | ||
/// | ||
/// org-dartlang-app://web/main.dart -> main.dart | ||
/// | ||
/// Will return `null` if the provided uri is not | ||
/// an app URI. | ||
final String? Function(String appUri) _serverPathForAppUri; | ||
|
||
/// Returns the relative path in google3, determined by the [absolutePath]. | ||
/// | ||
/// Returns `null` if not a google3 app. | ||
final String? Function(String absolutePath) _g3RelativePath; | ||
|
||
final BuildSettings _buildSettings; | ||
|
||
DdcLibraryBundleStrategy( | ||
this.reloadConfiguration, | ||
this._moduleProvider, | ||
this._digestsProvider, | ||
this._moduleForServerPath, | ||
this._serverPathForModule, | ||
this._sourceMapPathForModule, | ||
this._serverPathForAppUri, | ||
this._moduleInfoForProvider, | ||
AssetReader assetReader, | ||
this._buildSettings, | ||
this._g3RelativePath, | ||
String? packageConfigPath, | ||
) : super(assetReader, packageConfigPath: packageConfigPath); | ||
|
||
@override | ||
Handler get handler => (request) async { | ||
// TODO(markzipan): Implement a hot restarter that uses digests for | ||
// the DDC module system. | ||
return Response.notFound(request.url.toString()); | ||
}; | ||
|
||
@override | ||
String get id => 'ddc-library-bundle'; | ||
|
||
// DDC doesn't have a 'ddc-library-bundle' format flag. Instead, it treats the | ||
// combination of the DDC module format and canary mode as the DDC library | ||
// bundle format, so we just pass 'ddc' here. | ||
@override | ||
String get moduleFormat => 'ddc'; | ||
|
||
@override | ||
String get loadLibrariesModule => 'ddc_module_loader.ddk.js'; | ||
|
||
// TODO(srujzs): Refactor code that uses this to avoid loading individual | ||
// libraries, as that's no longer supported in the new module format. | ||
@override | ||
String get loadModuleSnippet => | ||
"function() { throw new Error('LoadStrategy.loadModuleSnippet is used. " | ||
"This is currently unsupported in the DDC library bundle format.'); }"; | ||
|
||
@override | ||
Future<String> bootstrapFor(String entrypoint) async => | ||
await _ddcLoaderSetup(entrypoint); | ||
|
||
@override | ||
String loadClientSnippet(String clientScript) => | ||
'window.\$dartLoader.forceLoadModule("$clientScript");\n'; | ||
|
||
Future<String> _ddcLoaderSetup(String entrypoint) async { | ||
final metadataProvider = metadataProviderFor(entrypoint); | ||
final modulePaths = await _moduleProvider(metadataProvider); | ||
final scripts = <Map<String, String?>>[]; | ||
modulePaths.forEach((name, path) { | ||
scripts.add(<String, String>{'src': '$path.js', 'id': name}); | ||
}); | ||
return ''' | ||
$baseUrlScript | ||
var scripts = ${const JsonEncoder.withIndent(" ").convert(scripts)}; | ||
window.\$dartLoader.loadConfig.loadScriptFn = function(loader) { | ||
loader.addScriptsToQueue(scripts, null); | ||
loader.loadEnqueuedModules(); | ||
}; | ||
window.\$dartLoader.loader.nextAttempt(); | ||
'''; | ||
} | ||
|
||
@override | ||
Future<String?> moduleForServerPath(String entrypoint, String serverPath) => | ||
_moduleForServerPath(metadataProviderFor(entrypoint), serverPath); | ||
|
||
@override | ||
Future<Map<String, ModuleInfo>> moduleInfoForEntrypoint(String entrypoint) => | ||
_moduleInfoForProvider(metadataProviderFor(entrypoint)); | ||
|
||
@override | ||
Future<String?> serverPathForModule(String entrypoint, String module) => | ||
_serverPathForModule(metadataProviderFor(entrypoint), module); | ||
|
||
@override | ||
Future<String?> sourceMapPathForModule(String entrypoint, String module) => | ||
_sourceMapPathForModule(metadataProviderFor(entrypoint), module); | ||
|
||
@override | ||
String? serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri); | ||
|
||
@override | ||
BuildSettings get buildSettings => _buildSettings; | ||
|
||
@override | ||
String? g3RelativePath(String absolutePath) => _g3RelativePath(absolutePath); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) 2024, 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:async'; | ||
|
||
import 'restarter.dart'; | ||
|
||
class DdcLibraryBundleRestarter implements Restarter { | ||
@override | ||
Future<bool> restart({String? runId, Future? readyToRunMain}) async { | ||
throw UnimplementedError( | ||
"Hot restart isn't supported for the DDC library bundle format yet.", | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.