compile.dart 5.12 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import '../artifacts.dart';
8 9
import '../base/common.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 23 24 25
Future<void> buildWeb(
  FlutterProject flutterProject,
  String target,
  BuildInfo buildInfo,
26
  bool csp,
27
  String serviceWorkerStrategy,
28
  bool sourceMaps,
29
  bool nativeNullAssertions,
30
) async {
31 32 33
  if (!flutterProject.web.existsSync()) {
    throwToolExit('Missing index.html.');
  }
34
  final bool hasWebPlugins = (await findPlugins(flutterProject))
35
    .any((Plugin p) => p.platforms.containsKey(WebPlugin.kConfigKey));
36
  final Directory outputDirectory = globals.fs.directory(getWebBuildDirectory());
37 38
  outputDirectory.createSync(recursive: true);

39
  await injectPlugins(flutterProject, webPlatform: true);
40
  final Status status = globals.logger.startProgress('Compiling $target for the Web...');
41
  final Stopwatch sw = Stopwatch()..start();
42
  try {
43 44
    final BuildResult result = await globals.buildSystem.build(const WebServiceWorker(), Environment(
      projectDir: globals.fs.currentDirectory,
45
      outputDir: outputDirectory,
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,
        kHasWebPlugins: hasWebPlugins.toString(),
53
        kDartDefines: encodeDartDefines(buildInfo.dartDefines),
54
        kCspMode: csp.toString(),
55
        kIconTreeShakerFlag: buildInfo.treeShakeIcons.toString(),
56
        kSourceMapsEnabled: sourceMaps.toString(),
57
        kNativeNullAssertions: nativeNullAssertions.toString(),
58 59
        if (serviceWorkerStrategy != null)
         kServiceWorkerStrategy: serviceWorkerStrategy,
60
        if (buildInfo.extraFrontEndOptions?.isNotEmpty ?? false)
61
          kExtraFrontEndOptions: encodeDartDefines(buildInfo.extraFrontEndOptions),
62
      },
63 64 65 66
      artifacts: globals.artifacts,
      fileSystem: globals.fs,
      logger: globals.logger,
      processManager: globals.processManager,
67 68 69 70 71
      cacheDir: globals.cache.getRoot(),
      engineVersion: globals.artifacts.isLocalEngine
        ? null
        : globals.flutterVersion.engineRevision,
      flutterRootDir: globals.fs.directory(Cache.flutterRoot),
72 73
    ));
    if (!result.success) {
74
      for (final ExceptionMeasurement measurement in result.exceptions.values) {
75
        globals.printError('Target ${measurement.target} failed: ${measurement.exception}',
76 77 78 79
          stackTrace: measurement.fatal
            ? measurement.stackTrace
            : null,
        );
80 81
      }
      throwToolExit('Failed to compile application for the Web.');
82
    }
83
  } on Exception catch (err) {
84 85 86
    throwToolExit(err.toString());
  } finally {
    status.stop();
87
  }
88
  globals.flutterUsage.sendTiming('build', 'dart2js', Duration(milliseconds: sw.elapsedMilliseconds));
89
}
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/// Web rendering backend mode.
enum WebRendererMode {
  /// Auto detects which rendering backend to use.
  autoDetect,
  /// Always uses canvaskit.
  canvaskit,
  /// Always uses html.
  html,
}

/// The correct precompiled artifact to use for each build and render mode.
const Map<WebRendererMode, Map<NullSafetyMode, Artifact>> kDartSdkJsArtifactMap = <WebRendererMode, Map<NullSafetyMode, Artifact>>{
  WebRendererMode.autoDetect: <NullSafetyMode, Artifact> {
    NullSafetyMode.sound: Artifact.webPrecompiledCanvaskitAndHtmlSoundSdk,
    NullSafetyMode.unsound: Artifact.webPrecompiledCanvaskitAndHtmlSdk,
  },
  WebRendererMode.canvaskit: <NullSafetyMode, Artifact> {
    NullSafetyMode.sound: Artifact.webPrecompiledCanvaskitSoundSdk,
    NullSafetyMode.unsound: Artifact.webPrecompiledCanvaskitSdk,
  },
  WebRendererMode.html: <NullSafetyMode, Artifact> {
    NullSafetyMode.sound: Artifact.webPrecompiledSoundSdk,
    NullSafetyMode.unsound: Artifact.webPrecompiledSdk,
  },
};

/// The correct source map artifact to use for each build and render mode.
const Map<WebRendererMode, Map<NullSafetyMode, Artifact>> kDartSdkJsMapArtifactMap = <WebRendererMode, Map<NullSafetyMode, Artifact>>{
  WebRendererMode.autoDetect: <NullSafetyMode, Artifact> {
    NullSafetyMode.sound: Artifact.webPrecompiledCanvaskitAndHtmlSoundSdkSourcemaps,
    NullSafetyMode.unsound: Artifact.webPrecompiledCanvaskitAndHtmlSdkSourcemaps,
  },
  WebRendererMode.canvaskit: <NullSafetyMode, Artifact> {
    NullSafetyMode.sound: Artifact.webPrecompiledCanvaskitSoundSdkSourcemaps,
    NullSafetyMode.unsound: Artifact.webPrecompiledCanvaskitSdkSourcemaps,
  },
  WebRendererMode.html: <NullSafetyMode, Artifact> {
    NullSafetyMode.sound: Artifact.webPrecompiledSoundSdkSourcemaps,
    NullSafetyMode.unsound: Artifact.webPrecompiledSdkSourcemaps,
  },
};