compile.dart 3.33 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9
// 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
import '../build_system/build_system.dart';
import '../build_system/targets/dart.dart';
14
import '../build_system/targets/icon_tree_shaker.dart';
15
import '../build_system/targets/web.dart';
16
import '../convert.dart';
17
import '../globals.dart' as globals;
18 19
import '../platform_plugins.dart';
import '../plugins.dart';
20
import '../project.dart';
21

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

25 26 27 28 29
Future<void> buildWeb(
  FlutterProject flutterProject,
  String target,
  BuildInfo buildInfo,
  bool initializePlatform,
30
  bool csp,
31
) 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 = globals.logger.startProgress('Compiling $target for the Web...', timeout: null);
39
  final Stopwatch sw = Stopwatch()..start();
40
  try {
41
    final BuildResult result = await globals.buildSystem.build(const WebServiceWorker(), Environment.test(
42
      globals.fs.currentDirectory,
43
      outputDir: globals.fs.directory(getWebBuildDirectory()),
44 45 46 47 48 49 50 51
      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(buildInfo.dartDefines),
53
        kCspMode: csp.toString(),
54
        kIconTreeShakerFlag: buildInfo.treeShakeIcons.toString(),
55 56 57
      },
    ));
    if (!result.success) {
58
      for (final ExceptionMeasurement measurement in result.exceptions.values) {
59
        globals.printError('Target ${measurement.target} failed: ${measurement.exception}',
60 61 62 63
          stackTrace: measurement.fatal
            ? measurement.stackTrace
            : null,
        );
64 65
      }
      throwToolExit('Failed to compile application for the Web.');
66
    }
67
  } on Exception catch (err) {
68 69 70
    throwToolExit(err.toString());
  } finally {
    status.stop();
71
  }
72
  globals.flutterUsage.sendTiming('build', 'dart2js', Duration(milliseconds: sw.elapsedMilliseconds));
73
}
74 75 76 77 78 79 80

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

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
86
  /// the entry points for dart2js to later take over.
87
  Future<bool> initialize({
88
    @required Directory projectDirectory,
89
    @required String projectName,
90
    String testOutputDir,
91
    List<String> testFiles,
92
    BuildMode mode,
93
    bool initializePlatform,
94 95 96 97
  }) async {
    throw UnimplementedError();
  }
}