compile.dart 3.98 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
import '../build_system/build_system.dart';
13
import '../build_system/targets/common.dart';
14
import '../build_system/targets/icon_tree_shaker.dart';
15
import '../build_system/targets/web.dart';
16
import '../cache.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
  String serviceWorkerStrategy,
32
) async {
33 34 35
  if (!flutterProject.web.existsSync()) {
    throwToolExit('Missing index.html.');
  }
36
  final bool hasWebPlugins = (await findPlugins(flutterProject))
37
    .any((Plugin p) => p.platforms.containsKey(WebPlugin.kConfigKey));
38
  await injectPlugins(flutterProject, checkProjects: true);
39
  final Status status = globals.logger.startProgress('Compiling $target for the Web...', timeout: null);
40
  final Stopwatch sw = Stopwatch()..start();
41
  try {
42 43
    final BuildResult result = await globals.buildSystem.build(const WebServiceWorker(), Environment(
      projectDir: globals.fs.currentDirectory,
44
      outputDir: globals.fs.directory(getWebBuildDirectory()),
45 46 47 48 49 50 51 52
      buildDir: flutterProject.directory
        .childDirectory('.dart_tool')
        .childDirectory('flutter_build'),
      defines: <String, String>{
        kBuildMode: getNameForBuildMode(buildInfo.mode),
        kTargetFile: target,
        kInitializePlatform: initializePlatform.toString(),
        kHasWebPlugins: hasWebPlugins.toString(),
53
        kDartDefines: encodeDartDefines(buildInfo.dartDefines),
54
        kCspMode: csp.toString(),
55
        kIconTreeShakerFlag: buildInfo.treeShakeIcons.toString(),
56 57
        if (serviceWorkerStrategy != null)
         kServiceWorkerStrategy: serviceWorkerStrategy,
58
        if (buildInfo.extraFrontEndOptions?.isNotEmpty ?? false)
59
          kExtraFrontEndOptions: encodeDartDefines(buildInfo.extraFrontEndOptions),
60
      },
61 62 63 64
      artifacts: globals.artifacts,
      fileSystem: globals.fs,
      logger: globals.logger,
      processManager: globals.processManager,
65 66 67 68 69
      cacheDir: globals.cache.getRoot(),
      engineVersion: globals.artifacts.isLocalEngine
        ? null
        : globals.flutterVersion.engineRevision,
      flutterRootDir: globals.fs.directory(Cache.flutterRoot),
70 71
    ));
    if (!result.success) {
72
      for (final ExceptionMeasurement measurement in result.exceptions.values) {
73
        globals.printError('Target ${measurement.target} failed: ${measurement.exception}',
74 75 76 77
          stackTrace: measurement.fatal
            ? measurement.stackTrace
            : null,
        );
78 79
      }
      throwToolExit('Failed to compile application for the Web.');
80
    }
81
  } on Exception catch (err) {
82 83 84
    throwToolExit(err.toString());
  } finally {
    status.stop();
85
  }
86
  globals.flutterUsage.sendTiming('build', 'dart2js', Duration(milliseconds: sw.elapsedMilliseconds));
87
}
88 89 90 91 92 93 94

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

95 96 97 98 99
  /// 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
100
  /// the entry points for dart2js to later take over.
101
  Future<bool> initialize({
102
    @required Directory projectDirectory,
103
    @required String projectName,
104
    String testOutputDir,
105
    List<String> testFiles,
106
    BuildMode mode,
107
    bool initializePlatform,
108 109 110 111
  }) async {
    throw UnimplementedError();
  }
}