Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Jonah Williams | 9bc5656 | 2019-02-14 22:42:30 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'package:meta/meta.dart'; |
| 6 | |
Jonah Williams | 9bc5656 | 2019-02-14 22:42:30 -0800 | [diff] [blame] | 7 | import '../base/common.dart'; |
| 8 | import '../base/context.dart'; |
| 9 | import '../base/file_system.dart'; |
Jonah Williams | c91b657 | 2019-06-06 21:05:55 -0700 | [diff] [blame] | 10 | import '../base/logger.dart'; |
Jonah Williams | 9bc5656 | 2019-02-14 22:42:30 -0800 | [diff] [blame] | 11 | import '../build_info.dart'; |
Jonah Williams | b2f3839 | 2019-10-01 14:03:42 -0700 | [diff] [blame] | 12 | import '../build_system/build_system.dart'; |
| 13 | import '../build_system/targets/dart.dart'; |
| 14 | import '../build_system/targets/web.dart'; |
Yegor | e7073f9 | 2019-11-09 17:08:53 -0800 | [diff] [blame] | 15 | import '../convert.dart'; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame^] | 16 | import '../globals.dart' as globals; |
Jonah Williams | b2f3839 | 2019-10-01 14:03:42 -0700 | [diff] [blame] | 17 | import '../platform_plugins.dart'; |
| 18 | import '../plugins.dart'; |
Jonah Williams | c91b657 | 2019-06-06 21:05:55 -0700 | [diff] [blame] | 19 | import '../project.dart'; |
Zachary Anderson | ef146f6 | 2019-07-29 07:24:02 -0700 | [diff] [blame] | 20 | import '../reporting/reporting.dart'; |
Jonah Williams | 9bc5656 | 2019-02-14 22:42:30 -0800 | [diff] [blame] | 21 | |
Jonah Williams | da600ba | 2019-05-29 22:46:28 -0700 | [diff] [blame] | 22 | /// The [WebCompilationProxy] instance. |
Jonah Williams | c91b657 | 2019-06-06 21:05:55 -0700 | [diff] [blame] | 23 | WebCompilationProxy get webCompilationProxy => context.get<WebCompilationProxy>(); |
Jonah Williams | da600ba | 2019-05-29 22:46:28 -0700 | [diff] [blame] | 24 | |
Yegor | e7073f9 | 2019-11-09 17:08:53 -0800 | [diff] [blame] | 25 | Future<void> buildWeb( |
| 26 | FlutterProject flutterProject, |
| 27 | String target, |
| 28 | BuildInfo buildInfo, |
| 29 | bool initializePlatform, |
| 30 | List<String> dartDefines, |
| 31 | ) async { |
Jonah Williams | f530b80 | 2019-06-10 15:37:23 -0700 | [diff] [blame] | 32 | if (!flutterProject.web.existsSync()) { |
| 33 | throwToolExit('Missing index.html.'); |
| 34 | } |
Jonah Williams | b2f3839 | 2019-10-01 14:03:42 -0700 | [diff] [blame] | 35 | final bool hasWebPlugins = findPlugins(flutterProject) |
| 36 | .any((Plugin p) => p.platforms.containsKey(WebPlugin.kConfigKey)); |
Jonah Williams | ae18f07 | 2019-10-04 00:36:09 -0700 | [diff] [blame] | 37 | await injectPlugins(flutterProject, checkProjects: true); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame^] | 38 | final Status status = globals.logger.startProgress('Compiling $target for the Web...', timeout: null); |
Zachary Anderson | b847ba5 | 2019-06-07 13:50:45 -0700 | [diff] [blame] | 39 | final Stopwatch sw = Stopwatch()..start(); |
Jonah Williams | 27105cb | 2019-10-21 16:46:44 -0700 | [diff] [blame] | 40 | try { |
| 41 | final BuildResult result = await buildSystem.build(const WebReleaseBundle(), Environment( |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame^] | 42 | outputDir: globals.fs.directory(getWebBuildDirectory()), |
| 43 | projectDir: globals.fs.currentDirectory, |
Jonah Williams | 27105cb | 2019-10-21 16:46:44 -0700 | [diff] [blame] | 44 | buildDir: flutterProject.directory |
| 45 | .childDirectory('.dart_tool') |
| 46 | .childDirectory('flutter_build'), |
| 47 | defines: <String, String>{ |
| 48 | kBuildMode: getNameForBuildMode(buildInfo.mode), |
| 49 | kTargetFile: target, |
| 50 | kInitializePlatform: initializePlatform.toString(), |
| 51 | kHasWebPlugins: hasWebPlugins.toString(), |
Yegor | e7073f9 | 2019-11-09 17:08:53 -0800 | [diff] [blame] | 52 | kDartDefines: jsonEncode(dartDefines), |
Jonah Williams | 27105cb | 2019-10-21 16:46:44 -0700 | [diff] [blame] | 53 | }, |
| 54 | )); |
| 55 | if (!result.success) { |
| 56 | for (ExceptionMeasurement measurement in result.exceptions.values) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame^] | 57 | globals.printError('Target ${measurement.target} failed: ${measurement.exception}', |
Jonah Williams | a7367b6 | 2019-11-18 14:04:11 -0800 | [diff] [blame] | 58 | stackTrace: measurement.fatal |
| 59 | ? measurement.stackTrace |
| 60 | : null, |
| 61 | ); |
Jonah Williams | 27105cb | 2019-10-21 16:46:44 -0700 | [diff] [blame] | 62 | } |
| 63 | throwToolExit('Failed to compile application for the Web.'); |
Jonah Williams | 9bc5656 | 2019-02-14 22:42:30 -0800 | [diff] [blame] | 64 | } |
Jonah Williams | 27105cb | 2019-10-21 16:46:44 -0700 | [diff] [blame] | 65 | } catch (err) { |
| 66 | throwToolExit(err.toString()); |
| 67 | } finally { |
| 68 | status.stop(); |
Jonah Williams | c91b657 | 2019-06-06 21:05:55 -0700 | [diff] [blame] | 69 | } |
Jonah Williams | b2f3839 | 2019-10-01 14:03:42 -0700 | [diff] [blame] | 70 | flutterUsage.sendTiming('build', 'dart2js', Duration(milliseconds: sw.elapsedMilliseconds)); |
Jonah Williams | 9bc5656 | 2019-02-14 22:42:30 -0800 | [diff] [blame] | 71 | } |
Jonah Williams | da600ba | 2019-05-29 22:46:28 -0700 | [diff] [blame] | 72 | |
| 73 | /// An indirection on web compilation. |
| 74 | /// |
| 75 | /// Avoids issues with syncing build_runner_core to other repos. |
| 76 | class WebCompilationProxy { |
| 77 | const WebCompilationProxy(); |
| 78 | |
Jonah Williams | c91b657 | 2019-06-06 21:05:55 -0700 | [diff] [blame] | 79 | /// Initialize the web compiler from the `projectDirectory`. |
| 80 | /// |
| 81 | /// Returns whether or not the build was successful. |
| 82 | /// |
| 83 | /// `release` controls whether we build the bundle for dartdevc or only |
Greg Spencer | a60bf8e | 2019-11-22 08:43:55 -0800 | [diff] [blame] | 84 | /// the entry points for dart2js to later take over. |
Jonah Williams | c91b657 | 2019-06-06 21:05:55 -0700 | [diff] [blame] | 85 | Future<bool> initialize({ |
Jonah Williams | da600ba | 2019-05-29 22:46:28 -0700 | [diff] [blame] | 86 | @required Directory projectDirectory, |
Jonah Williams | 477ae6b | 2019-09-09 18:16:03 -0700 | [diff] [blame] | 87 | @required String projectName, |
Jonah Williams | b3382b9 | 2019-06-05 15:27:49 -0700 | [diff] [blame] | 88 | String testOutputDir, |
Yegor | 903ebd7 | 2019-10-24 21:35:14 -0700 | [diff] [blame] | 89 | List<String> testFiles, |
Jonah Williams | f362419 | 2019-08-28 14:05:01 -0700 | [diff] [blame] | 90 | BuildMode mode, |
Jonah Williams | 69a2964 | 2019-09-13 10:20:30 -0700 | [diff] [blame] | 91 | bool initializePlatform, |
Jonah Williams | da600ba | 2019-05-29 22:46:28 -0700 | [diff] [blame] | 92 | }) async { |
| 93 | throw UnimplementedError(); |
| 94 | } |
Jonah Williams | da600ba | 2019-05-29 22:46:28 -0700 | [diff] [blame] | 95 | } |