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' ;
6+
57import 'package:dwds/src/debugging/metadata/provider.dart' ;
68import 'package:dwds/src/loaders/strategy.dart' ;
79import 'package:dwds/src/readers/asset_reader.dart' ;
810import 'package:dwds/src/services/expression_compiler.dart' ;
11+ import 'package:path/path.dart' as p;
912import 'package:shelf/shelf.dart' ;
1013
14+ String removeJsExtension (String path) =>
15+ path.endsWith ('.js' ) ? p.withoutExtension (path) : path;
16+
17+ String addJsExtension (String path) => '$path .js' ;
18+
19+ /// JavaScript snippet to determine the base URL of the current path.
20+ const _baseUrlScript = '''
21+ var baseUrl = (function () {
22+ // Attempt to detect --precompiled mode for tests, and set the base url
23+ // appropriately, otherwise set it to '/'.
24+ var pathParts = location.pathname.split("/");
25+ if (pathParts[0] == "") {
26+ pathParts.shift();
27+ }
28+ if (pathParts.length > 1 && pathParts[1] == "test") {
29+ return "/" + pathParts.slice(0, 2).join("/") + "/";
30+ }
31+ // Attempt to detect base url using <base href> html tag
32+ // base href should start and end with "/"
33+ if (typeof document !== 'undefined') {
34+ var el = document.getElementsByTagName('base');
35+ if (el && el[0] && el[0].getAttribute("href") && el[0].getAttribute
36+ ("href").startsWith("/") && el[0].getAttribute("href").endsWith("/")){
37+ return el[0].getAttribute("href");
38+ }
39+ }
40+ // return default value
41+ return "/";
42+ }());
43+ ''' ;
44+
1145/// A load strategy for the legacy module system.
1246class LegacyStrategy extends LoadStrategy {
1347 @override
1448 final ReloadConfiguration reloadConfiguration;
1549
50+ /// Returns a map of module name to corresponding server path (excluding .js)
51+ /// for the provided Dart application entrypoint.
52+ ///
53+ /// For example:
54+ ///
55+ /// web/main -> main.ddc
56+ /// packages/path/path -> packages/path/path.ddc
57+ ///
58+ final Future <Map <String , String >> Function (MetadataProvider metadataProvider)
59+ _moduleProvider;
60+
61+ /// Returns a map of module name to corresponding digest value.
62+ ///
63+ /// For example:
64+ ///
65+ /// web/main -> 8363b363f74b41cac955024ab8b94a3f
66+ /// packages/path/path -> d348c2a4647e998011fe305f74f22961
67+ ///
68+ final Future <Map <String , String >> Function (MetadataProvider metadataProvider)
69+ _digestsProvider;
70+
1671 /// Returns the module for the corresponding server path.
1772 ///
1873 /// For example:
@@ -75,6 +130,8 @@ class LegacyStrategy extends LoadStrategy {
75130
76131 LegacyStrategy (
77132 this .reloadConfiguration,
133+ this ._moduleProvider,
134+ this ._digestsProvider,
78135 this ._moduleForServerPath,
79136 this ._serverPathForModule,
80137 this ._sourceMapPathForModule,
@@ -87,7 +144,11 @@ class LegacyStrategy extends LoadStrategy {
87144 ) : super (assetReader, packageConfigPath: packageConfigPath);
88145
89146 @override
90- Handler get handler => (request) => Response .notFound (request.url.toString ());
147+ Handler get handler => (request) async {
148+ // TODO(markzipan): Implement a hot restarter that uses digests for
149+ // the DDC module system.
150+ return Response .notFound (request.url.toString ());
151+ };
91152
92153 @override
93154 String get id => 'legacy' ;
@@ -102,12 +163,27 @@ class LegacyStrategy extends LoadStrategy {
102163 String get loadModuleSnippet => 'dart_library.import' ;
103164
104165 @override
105- Future <String > bootstrapFor (String entrypoint) async => '' ;
166+ Future <String > bootstrapFor (String entrypoint) async =>
167+ await _legacyLoaderSetup (entrypoint);
106168
107169 @override
108170 String loadClientSnippet (String clientScript) =>
109171 'window.\$ dartLoader.forceLoadModule("$clientScript ");\n ' ;
110172
173+ Future <String > _legacyLoaderSetup (String entrypoint) async {
174+ final metadataProvider = metadataProviderFor (entrypoint);
175+ final modulePaths = await _moduleProvider (metadataProvider);
176+ final scripts = < Map <String , String ?>> [];
177+ modulePaths.forEach ((name, path) {
178+ scripts.add (< String , String > {'src' : '$path .js' , 'id' : name});
179+ });
180+ return '''
181+ $_baseUrlScript
182+ var scripts = ${const JsonEncoder .withIndent (" " ).convert (scripts )};
183+ window.\$ dartLoader.loadScripts(scripts);
184+ ''' ;
185+ }
186+
111187 @override
112188 Future <String ?> moduleForServerPath (String entrypoint, String serverPath) =>
113189 _moduleForServerPath (metadataProviderFor (entrypoint), serverPath);
0 commit comments