Unverified Commit f1186b07 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Asset server fix for sourcemaps (#44017)

parent ade8dfac
...@@ -442,37 +442,49 @@ class DebugAssetServer extends AssetServer { ...@@ -442,37 +442,49 @@ class DebugAssetServer extends AssetServer {
return Response.ok(file.readAsBytesSync(), headers: <String, String>{ return Response.ok(file.readAsBytesSync(), headers: <String, String>{
'Content-Type': 'text/javascript', 'Content-Type': 'text/javascript',
}); });
} else if (request.url.path.endsWith('dart_sdk.js')) { } else if (request.url.path.endsWith('dart_sdk.js.map')) {
final File file = fs.file(fs.path.join( final File file = fs.file(fs.path.join(
artifacts.getArtifactPath(Artifact.flutterWebSdk), artifacts.getArtifactPath(Artifact.flutterWebSdk),
'kernel', 'kernel',
'amd', 'amd',
'dart_sdk.js', 'dart_sdk.js.map',
)); ));
return Response.ok(file.readAsBytesSync(), headers: <String, String>{ return Response.ok(file.readAsBytesSync());
'Content-Type': 'text/javascript', } else if (request.url.path.endsWith('dart_sdk.js')) {
});
} else if (request.url.path.endsWith('dart_sdk.js.map')) {
final File file = fs.file(fs.path.join( final File file = fs.file(fs.path.join(
artifacts.getArtifactPath(Artifact.flutterWebSdk), artifacts.getArtifactPath(Artifact.flutterWebSdk),
'kernel', 'kernel',
'amd', 'amd',
'dart_sdk.js.map', 'dart_sdk.js',
)); ));
return Response.ok(file.readAsBytesSync()); return Response.ok(file.readAsBytesSync(), headers: <String, String>{
'Content-Type': 'text/javascript',
});
} else if (request.url.path.endsWith('.dart')) { } else if (request.url.path.endsWith('.dart')) {
// This is likely a sourcemap request. The first segment is the // This is likely a sourcemap request. The first segment is the
// package name, and the rest is the path to the file relative to // package name, and the rest is the path to the file relative to
// the package uri. For example, `foo/bar.dart` would represent a // 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 // file at a path like `foo/lib/bar.dart`. If there is no leading
// segment, then we assume it is from the current package. // 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. // 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 `/`. // 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 sdkPath = fs.path.joinAll(request.url.path.split('dart-sdk/').last.split('/'));
final String webSdkPath = artifacts.getArtifactPath(Artifact.flutterWebSdk); final String dartSdkPath = artifacts.getArtifactPath(Artifact.engineDartSdkPath);
return Response.ok(fs.file(fs.path.join(webSdkPath, sdkPath)).readAsBytesSync()); 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 final String packageName = request.url.pathSegments.length == 1
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // 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/base/file_system.dart';
import 'package:flutter_tools/src/build_runner/web_fs.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:flutter_tools/src/project.dart';
import 'package:shelf/shelf.dart'; import 'package:shelf/shelf.dart';
...@@ -52,6 +54,35 @@ void main() { ...@@ -52,6 +54,35 @@ void main() {
expect(await response.readAsString(), 'hello'); 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 { test('can serve an asset with a png content type', () => testbed.run(() async {
final Response response = await assetServer final Response response = await assetServer
.handle(Request('GET', Uri.parse('http://localhost:8080/assets/foo.png'))); .handle(Request('GET', Uri.parse('http://localhost:8080/assets/foo.png')));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment