From f1186b0758082e78a80aeff306f42c6bef26cd5c Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 1 Nov 2019 22:33:52 -0700 Subject: [PATCH] Asset server fix for sourcemaps (#44017) --- .../lib/src/build_runner/web_fs.dart | 36 ++++++++++++------- .../general.shard/web/asset_server_test.dart | 31 ++++++++++++++++ 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_runner/web_fs.dart b/packages/flutter_tools/lib/src/build_runner/web_fs.dart index c723bf090ce..da25e084039 100644 --- a/packages/flutter_tools/lib/src/build_runner/web_fs.dart +++ b/packages/flutter_tools/lib/src/build_runner/web_fs.dart @@ -442,6 +442,14 @@ class DebugAssetServer extends AssetServer { return Response.ok(file.readAsBytesSync(), headers: { '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: { '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 diff --git a/packages/flutter_tools/test/general.shard/web/asset_server_test.dart b/packages/flutter_tools/test/general.shard/web/asset_server_test.dart index aeb75e26418..7921aedf020 100644 --- a/packages/flutter_tools/test/general.shard/web/asset_server_test.dart +++ b/packages/flutter_tools/test/general.shard/web/asset_server_test.dart @@ -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, { + '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, { + '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')));