compile.dart 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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.

import 'package:meta/meta.dart';

import '../base/common.dart';
import '../base/context.dart';
import '../base/file_system.dart';
10
import '../base/logger.dart';
11
import '../build_info.dart';
12 13 14
import '../build_system/build_system.dart';
import '../build_system/targets/dart.dart';
import '../build_system/targets/web.dart';
15
import '../convert.dart';
16
import '../globals.dart';
17 18
import '../platform_plugins.dart';
import '../plugins.dart';
19
import '../project.dart';
20
import '../reporting/reporting.dart';
21

22
/// The [WebCompilationProxy] instance.
23
WebCompilationProxy get webCompilationProxy => context.get<WebCompilationProxy>();
24

25 26 27 28 29 30 31
Future<void> buildWeb(
  FlutterProject flutterProject,
  String target,
  BuildInfo buildInfo,
  bool initializePlatform,
  List<String> dartDefines,
) async {
32 33 34
  if (!flutterProject.web.existsSync()) {
    throwToolExit('Missing index.html.');
  }
35 36
  final bool hasWebPlugins = findPlugins(flutterProject)
    .any((Plugin p) => p.platforms.containsKey(WebPlugin.kConfigKey));
37
  await injectPlugins(flutterProject, checkProjects: true);
38
  final Status status = logger.startProgress('Compiling $target for the Web...', timeout: null);
39
  final Stopwatch sw = Stopwatch()..start();
40 41 42 43 44 45 46 47 48 49 50 51
  try {
    final BuildResult result = await buildSystem.build(const WebReleaseBundle(), Environment(
      outputDir: fs.directory(getWebBuildDirectory()),
      projectDir: fs.currentDirectory,
      buildDir: flutterProject.directory
        .childDirectory('.dart_tool')
        .childDirectory('flutter_build'),
      defines: <String, String>{
        kBuildMode: getNameForBuildMode(buildInfo.mode),
        kTargetFile: target,
        kInitializePlatform: initializePlatform.toString(),
        kHasWebPlugins: hasWebPlugins.toString(),
52
        kDartDefines: jsonEncode(dartDefines),
53 54 55 56
      },
    ));
    if (!result.success) {
      for (ExceptionMeasurement measurement in result.exceptions.values) {
57 58 59 60 61
        printError('Target ${measurement.target} failed: ${measurement.exception}',
          stackTrace: measurement.fatal
            ? measurement.stackTrace
            : null,
        );
62 63
      }
      throwToolExit('Failed to compile application for the Web.');
64
    }
65 66 67 68
  } catch (err) {
    throwToolExit(err.toString());
  } finally {
    status.stop();
69
  }
70
  flutterUsage.sendTiming('build', 'dart2js', Duration(milliseconds: sw.elapsedMilliseconds));
71
}
72 73 74 75 76 77 78

/// An indirection on web compilation.
///
/// Avoids issues with syncing build_runner_core to other repos.
class WebCompilationProxy {
  const WebCompilationProxy();

79 80 81 82 83 84 85
  /// Initialize the web compiler from the `projectDirectory`.
  ///
  /// Returns whether or not the build was successful.
  ///
  /// `release` controls whether we build the bundle for dartdevc or only
  /// the entrypoints for dart2js to later take over.
  Future<bool> initialize({
86
    @required Directory projectDirectory,
87
    @required String projectName,
88
    String testOutputDir,
89
    List<String> testFiles,
90
    BuildMode mode,
91
    bool initializePlatform,
92 93 94 95
  }) async {
    throw UnimplementedError();
  }
}