Asset server fix for sourcemaps (#44017)

This commit is contained in:
Jonah Williams 2019-11-01 22:33:52 -07:00 committed by GitHub
parent ade8dfac3d
commit f1186b0758
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 12 deletions

View file

@ -442,6 +442,14 @@ class DebugAssetServer extends AssetServer {
return Response.ok(file.readAsBytesSync(), headers: <String, String>{
'Content-Type': 'text/javascript',
});
} else if (request.url.path.endsWith('dart_sdk.js.map')) {
final File file = fs.file(fs.path.join(
artifacts.getArtifactPath(Artifact.flutterWebSdk),
'kernel',
'amd',
'dart_sdk.js.map',
));
return Response.ok(file.readAsBytesSync());
} else if (request.url.path.endsWith('dart_sdk.js')) {
final File file = fs.file(fs.path.join(
artifacts.getArtifactPath(Artifact.flutterWebSdk),
@ -452,27 +460,31 @@ class DebugAssetServer extends AssetServer {
return Response.ok(file.readAsBytesSync(), headers: <String, String>{
'Content-Type': 'text/javascript',
});
} else if (request.url.path.endsWith('dart_sdk.js.map')) {
final File file = fs.file(fs.path.join(
artifacts.getArtifactPath(Artifact.flutterWebSdk),
'kernel',
'amd',
'dart_sdk.js.map',
));
return Response.ok(file.readAsBytesSync());
} else if (request.url.path.endsWith('.dart')) {
// This is likely a sourcemap request. The first segment is the
// package name, and the rest is the path to the file relative to
// the package uri. For example, `foo/bar.dart` would represent a
// file at a path like `foo/lib/bar.dart`. If there is no leading
// segment, then we assume it is from the current package.
String basePath = request.url.path;
basePath = basePath.replaceFirst('packages/build_web_compilers/', '');
basePath = basePath.replaceFirst('packages/', '');
// Handle sdk requests that have mangled urls from engine build.
if (request.url.path.contains('flutter_web_sdk')) {
if (request.url.path.contains('dart-sdk')) {
// Note: the request is a uri and not a file path, so they always use `/`.
final String sdkPath = fs.path.joinAll(request.url.path.split('flutter_web_sdk/').last.split('/'));
final String webSdkPath = artifacts.getArtifactPath(Artifact.flutterWebSdk);
return Response.ok(fs.file(fs.path.join(webSdkPath, sdkPath)).readAsBytesSync());
final String sdkPath = fs.path.joinAll(request.url.path.split('dart-sdk/').last.split('/'));
final String dartSdkPath = artifacts.getArtifactPath(Artifact.engineDartSdkPath);
final File candidateFile = fs.file(fs.path.join(dartSdkPath, sdkPath));
return Response.ok(candidateFile.readAsBytesSync());
}
// See if it is a flutter sdk path.
final String webSdkPath = artifacts.getArtifactPath(Artifact.flutterWebSdk);
final File candidateFile = fs.file(fs.path.join(webSdkPath,
basePath.split('/').join(platform.pathSeparator)));
if (candidateFile.existsSync()) {
return Response.ok(candidateFile.readAsBytesSync());
}
final String packageName = request.url.pathSegments.length == 1

View file

@ -2,8 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/build_runner/web_fs.dart';
import 'package:flutter_tools/src/globals.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:shelf/shelf.dart';
@ -52,6 +54,35 @@ void main() {
expect(await response.readAsString(), 'hello');
}));
test('can serve a sourcemap from dart:ui', () => testbed.run(() async {
final String flutterWebSdkPath = artifacts.getArtifactPath(Artifact.flutterWebSdk);
final File windowSourceFile = fs.file(fs.path.join(flutterWebSdkPath, 'lib', 'ui', 'src', 'ui', 'window.dart'))
..createSync(recursive: true)
..writeAsStringSync('test');
final Response response = await assetServer
.handle(Request('GET', Uri.parse('http://localhost:8080/packages/build_web_compilers/lib/ui/src/ui/window.dart')));
expect(response.headers, <String, String>{
'content-length': windowSourceFile.lengthSync().toString(),
});
expect(await response.readAsString(), 'test');
}));
test('can serve a sourcemap from the dart:sdk', () => testbed.run(() async {
final String dartSdkPath = artifacts.getArtifactPath(Artifact.engineDartSdkPath);
final File listSourceFile = fs.file(fs.path.join(dartSdkPath, 'lib', 'core', 'list.dart'))
..createSync(recursive: true)
..writeAsStringSync('test');
final Response response = await assetServer
.handle(Request('GET', Uri.parse('http://localhost:8080/packages/dart-sdk/lib/core/list.dart')));
expect(response.headers, <String, String>{
'content-length': listSourceFile.lengthSync().toString(),
});
expect(await response.readAsString(), 'test');
}));
test('can serve an asset with a png content type', () => testbed.run(() async {
final Response response = await assetServer
.handle(Request('GET', Uri.parse('http://localhost:8080/assets/foo.png')));