2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
+ import 'dart:convert' ;
6
+
5
7
import 'package:dwds/src/debugging/metadata/provider.dart' ;
6
8
import 'package:dwds/src/loaders/strategy.dart' ;
7
9
import 'package:dwds/src/readers/asset_reader.dart' ;
8
10
import 'package:dwds/src/services/expression_compiler.dart' ;
11
+ import 'package:path/path.dart' as p;
9
12
import 'package:shelf/shelf.dart' ;
10
13
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
+
11
45
/// A load strategy for the legacy module system.
12
46
class LegacyStrategy extends LoadStrategy {
13
47
@override
14
48
final ReloadConfiguration reloadConfiguration;
15
49
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
+
16
71
/// Returns the module for the corresponding server path.
17
72
///
18
73
/// For example:
@@ -75,6 +130,8 @@ class LegacyStrategy extends LoadStrategy {
75
130
76
131
LegacyStrategy (
77
132
this .reloadConfiguration,
133
+ this ._moduleProvider,
134
+ this ._digestsProvider,
78
135
this ._moduleForServerPath,
79
136
this ._serverPathForModule,
80
137
this ._sourceMapPathForModule,
@@ -87,7 +144,11 @@ class LegacyStrategy extends LoadStrategy {
87
144
) : super (assetReader, packageConfigPath: packageConfigPath);
88
145
89
146
@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
+ };
91
152
92
153
@override
93
154
String get id => 'legacy' ;
@@ -102,12 +163,27 @@ class LegacyStrategy extends LoadStrategy {
102
163
String get loadModuleSnippet => 'dart_library.import' ;
103
164
104
165
@override
105
- Future <String > bootstrapFor (String entrypoint) async => '' ;
166
+ Future <String > bootstrapFor (String entrypoint) async =>
167
+ await _legacyLoaderSetup (entrypoint);
106
168
107
169
@override
108
170
String loadClientSnippet (String clientScript) =>
109
171
'window.\$ dartLoader.forceLoadModule("$clientScript ");\n ' ;
110
172
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
+
111
187
@override
112
188
Future <String ?> moduleForServerPath (String entrypoint, String serverPath) =>
113
189
_moduleForServerPath (metadataProviderFor (entrypoint), serverPath);
0 commit comments