From 4d3b51e28435446db849819ec98099cc51be070a Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 9 May 2019 08:57:26 -0700 Subject: [PATCH] Allow flutter web to be compiled with flutter (#32360) --- examples/flutter_gallery/lib/main_web.dart | 16 ++++++++++++++++ examples/flutter_gallery/web/index.html | 12 ++++++++++++ .../flutter/lib/src/foundation/platform.dart | 18 ++++++++++++------ .../flutter_tools/lib/src/web/web_device.dart | 17 ++++++++++++++--- 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 examples/flutter_gallery/lib/main_web.dart create mode 100644 examples/flutter_gallery/web/index.html diff --git a/examples/flutter_gallery/lib/main_web.dart b/examples/flutter_gallery/lib/main_web.dart new file mode 100644 index 0000000000..f53fa2c5bb --- /dev/null +++ b/examples/flutter_gallery/lib/main_web.dart @@ -0,0 +1,16 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Thanks for checking out Flutter! +// Like what you see? Tweet us @FlutterDev + +import 'dart:ui' as ui; + +import 'package:flutter/material.dart'; +import 'gallery/app.dart'; + +Future main() async { + await ui.webOnlyInitializePlatform(); // ignore: undefined_function + runApp(const GalleryApp()); +} diff --git a/examples/flutter_gallery/web/index.html b/examples/flutter_gallery/web/index.html new file mode 100644 index 0000000000..ba232943dd --- /dev/null +++ b/examples/flutter_gallery/web/index.html @@ -0,0 +1,12 @@ + + + + + Flutter Gallery + + + + + \ No newline at end of file diff --git a/packages/flutter/lib/src/foundation/platform.dart b/packages/flutter/lib/src/foundation/platform.dart index a9b199f283..aaf6529d41 100644 --- a/packages/flutter/lib/src/foundation/platform.dart +++ b/packages/flutter/lib/src/foundation/platform.dart @@ -49,16 +49,22 @@ enum TargetPlatform { // and we'd never be able to introduce dedicated behavior for that platform // (since doing so would be a big breaking change). TargetPlatform get defaultTargetPlatform { + // TODO(jonahwilliams): consider where this constant should live. + const bool kIsWeb = identical(1, 1.0); TargetPlatform result; - if (Platform.isIOS) { - result = TargetPlatform.iOS; - } else if (Platform.isAndroid) { + if (kIsWeb) { result = TargetPlatform.android; - } else if (Platform.isFuchsia) { - result = TargetPlatform.fuchsia; + } else { + if (Platform.isIOS) { + result = TargetPlatform.iOS; + } else if (Platform.isAndroid) { + result = TargetPlatform.android; + } else if (Platform.isFuchsia) { + result = TargetPlatform.fuchsia; + } } assert(() { - if (Platform.environment.containsKey('FLUTTER_TEST')) + if (!kIsWeb && Platform.environment.containsKey('FLUTTER_TEST')) result = TargetPlatform.android; return true; }()); diff --git a/packages/flutter_tools/lib/src/web/web_device.dart b/packages/flutter_tools/lib/src/web/web_device.dart index 3d3a1baa2d..0c6905b77e 100644 --- a/packages/flutter_tools/lib/src/web/web_device.dart +++ b/packages/flutter_tools/lib/src/web/web_device.dart @@ -3,6 +3,8 @@ // found in the LICENSE file. import '../application_package.dart'; +import '../asset.dart'; +import '../base/common.dart'; import '../base/context.dart'; import '../base/file_system.dart'; import '../base/io.dart'; @@ -10,6 +12,7 @@ import '../base/logger.dart'; import '../base/platform.dart'; import '../base/process_manager.dart'; import '../build_info.dart'; +import '../bundle.dart'; import '../device.dart'; import '../globals.dart'; import '../project.dart'; @@ -111,6 +114,13 @@ class WebDevice extends Device { printError('Failed to compile ${package.name} to JavaScript'); return LaunchResult.failed(); } + final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle(); + final int build = await assetBundle.build(); + if (build != 0) { + throwToolExit('Error: Failed to build asset bundle'); + } + await writeBundle(fs.directory(getAssetBuildDirectory()), assetBundle.entries); + _package = package; _server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); _server.listen(_basicAssetServer); @@ -141,7 +151,7 @@ class WebDevice extends Device { await request.response.close(); return; } - // Resolve all get requests to the build/web/asset directory. + // Resolve all get requests to the build/web/ or build/flutter_assets directory. final Uri uri = request.uri; File file; String contentType; @@ -152,8 +162,9 @@ class WebDevice extends Device { file = fs.file(fs.path.join(getWebBuildDirectory(), 'main.dart.js')); contentType = 'text/javascript'; } else { - file = fs.file(fs.path.join(getAssetBuildDirectory(), uri.path)); + file = fs.file(fs.path.join(getAssetBuildDirectory(), uri.path.replaceFirst('/assets/', ''))); } + if (!file.existsSync()) { request.response.statusCode = HttpStatus.notFound; await request.response.close(); @@ -201,7 +212,7 @@ class ChromeLauncher { Future launch(String host) async { if (platform.isMacOS) { - await processManager.start([ + return processManager.start([ _kMacosLocation, host, ]);