[flutter_tools] Generate correct entrypoint module name for experimental web compiler (#49486)

This commit is contained in:
Jonah Williams 2020-01-27 17:41:29 -08:00 committed by GitHub
parent 7f715628b0
commit ace2c2297f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 49 deletions

View file

@ -68,7 +68,6 @@ class WebAssetServer {
// RandomAccessFile and read on demand.
final Map<String, Uint8List> _files = <String, Uint8List>{};
final Map<String, Uint8List> _sourcemaps = <String, Uint8List>{};
final RegExp _drivePath = RegExp(r'\/[A-Z]:\/');
final Packages _packages;
@ -92,6 +91,7 @@ class WebAssetServer {
await response.close();
return;
}
// TODO(jonahwilliams): better path normalization in frontend_server to remove
// this workaround.
String requestPath = request.uri.path;
@ -347,17 +347,20 @@ class WebDevFS implements DevFS {
'web',
'dart_stack_trace_mapper.js',
));
final String entrypoint = PackageUriMapper(mainPath, '.packages', null, null)
.map(mainPath)
?.pathSegments?.join('/');
_webAssetServer.writeFile(
'/main.dart.js',
generateBootstrapScript(
requireUrl: _filePathToUriFragment(requireJS.path),
mapperUrl: _filePathToUriFragment(stackTraceMapper.path),
entrypoint: '${_filePathToUriFragment(mainPath)}.lib.js',
entrypoint: entrypoint != null ? '/packages/$entrypoint.lib.js' : '$mainPath.lib.js',
));
_webAssetServer.writeFile(
'/main_module.js',
generateMainModule(
entrypoint: '${_filePathToUriFragment(mainPath)}.lib.js',
entrypoint: entrypoint != null ? '/packages/$entrypoint.lib.js' : '$mainPath.lib.js',
));
_webAssetServer.writeFile('/dart_sdk.js', dartSdk.readAsStringSync());
_webAssetServer.writeFile(

View file

@ -35,21 +35,23 @@ void main() {
MockHttpHeaders headers;
Completer<void> closeCompleter;
WebAssetServer webAssetServer;
MockPlatform windows;
MockPlatform linux;
Packages packages;
MockPlatform windows;
setUpAll(() async {
packages = await loadPackagesFile(Uri.base.resolve('.packages'));
packages = await loadPackagesFile(Uri.base.resolve('.packages'), loader: (Uri uri) async {
return utf8.encode('\n');
});
});
setUp(() {
windows = MockPlatform();
linux = MockPlatform();
when(windows.environment).thenReturn(const <String, String>{});
when(windows.isWindows).thenReturn(true);
windows = MockPlatform();
when(linux.isWindows).thenReturn(false);
when(linux.environment).thenReturn(const <String, String>{});
when(windows.environment).thenReturn(const <String, String>{});
when(windows.isWindows).thenReturn(true);
testbed = Testbed(setup: () {
mockHttpServer = MockHttpServer();
requestController = StreamController<HttpRequest>.broadcast();
@ -139,29 +141,6 @@ void main() {
Platform: () => linux,
}));
test('serves JavaScript files from in memory cache on Windows', () => testbed.run(() async {
final File source = globals.fs.file('source')
..writeAsStringSync('main() {}');
final File sourcemap = globals.fs.file('sourcemap')
..writeAsStringSync('{}');
final File manifest = globals.fs.file('manifest')
..writeAsStringSync(json.encode(<String, Object>{'/C:/foo.js': <String, Object>{
'code': <int>[0, source.lengthSync()],
'sourcemap': <int>[0, 2],
}}));
webAssetServer.write(source, manifest, sourcemap);
when(request.uri).thenReturn(Uri.parse('http://foobar/C:/foo.js'));
requestController.add(request);
await closeCompleter.future;
verify(headers.add('Content-Length', source.lengthSync())).called(1);
verify(headers.add('Content-Type', 'application/javascript')).called(1);
verify(response.add(source.readAsBytesSync())).called(1);
}, overrides: <Type, Generator>{
Platform: () => windows,
}));
test('serves JavaScript files from in memory cache not from manifest', () => testbed.run(() async {
webAssetServer.writeFile('/foo.js', 'main() {}');
@ -193,7 +172,30 @@ void main() {
verify(response.statusCode = 404).called(1);
}));
test('serves Dart files from in filesystem on Windows', () => testbed.run(() async {
test('serves JavaScript files from in memory cache on Windows', () => testbed.run(() async {
final File source = globals.fs.file('source')
..writeAsStringSync('main() {}');
final File sourcemap = globals.fs.file('sourcemap')
..writeAsStringSync('{}');
final File manifest = globals.fs.file('manifest')
..writeAsStringSync(json.encode(<String, Object>{'/C:/foo.js': <String, Object>{
'code': <int>[0, source.lengthSync()],
'sourcemap': <int>[0, 2],
}}));
webAssetServer.write(source, manifest, sourcemap);
when(request.uri).thenReturn(Uri.parse('http://foobar/C:/foo.js'));
requestController.add(request);
await closeCompleter.future;
verify(headers.add('Content-Length', source.lengthSync())).called(1);
verify(headers.add('Content-Type', 'application/javascript')).called(1);
verify(response.add(source.readAsBytesSync())).called(1);
}, overrides: <Type, Generator>{
Platform: () => windows,
}));
test('serves Dart files from in filesystem on Windows', () => testbed.run(() async {
final File source = globals.fs.file('foo.dart').absolute
..createSync(recursive: true)
..writeAsStringSync('void main() {}');
@ -208,6 +210,22 @@ void main() {
Platform: () => windows,
}));
test('serves asset files from in filesystem with known mime type on Windows', () => testbed.run(() async {
final File source = globals.fs.file(globals.fs.path.join('build', 'flutter_assets', 'foo.png'))
..createSync(recursive: true)
..writeAsBytesSync(kTransparentImage);
when(request.uri).thenReturn(Uri.parse('http://foobar/assets/foo.png'));
requestController.add(request);
await closeCompleter.future;
verify(headers.add('Content-Length', source.lengthSync())).called(1);
verify(headers.add('Content-Type', 'image/png')).called(1);
verify(response.addStream(any)).called(1);
}, overrides: <Type, Generator>{
Platform: () => windows,
}));
test('serves Dart files from in filesystem on Linux/macOS', () => testbed.run(() async {
final File source = globals.fs.file('foo.dart').absolute
..createSync(recursive: true)
@ -245,23 +263,6 @@ void main() {
verify(response.addStream(any)).called(1);
}));
test('serves asset files from in filesystem with known mime type on Windows', () => testbed.run(() async {
final File source = globals.fs.file(globals.fs.path.join('build', 'flutter_assets', 'foo.png'))
..createSync(recursive: true)
..writeAsBytesSync(kTransparentImage);
when(request.uri).thenReturn(Uri.parse('http://foobar/assets/foo.png'));
requestController.add(request);
await closeCompleter.future;
verify(headers.add('Content-Length', source.lengthSync())).called(1);
verify(headers.add('Content-Type', 'image/png')).called(1);
verify(response.addStream(any)).called(1);
}, overrides: <Type, Generator>{
Platform: () => windows,
}));
test('serves asset files files from in filesystem with unknown mime type and length > 12', () => testbed.run(() async {
final File source = globals.fs.file(globals.fs.path.join('build', 'flutter_assets', 'foo'))
..createSync(recursive: true)