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

import 'dart:async';

7 8
import 'package:meta/meta.dart';

9
import '../base/common.dart';
10
import '../build_info.dart';
11
import '../build_system/targets/web.dart';
12
import '../features.dart';
13
import '../project.dart';
14
import '../runner/flutter_command.dart'
15
    show DevelopmentArtifact, FlutterCommandResult;
16 17 18 19
import '../web/compile.dart';
import 'build.dart';

class BuildWebCommand extends BuildSubCommand {
20 21 22
  BuildWebCommand({
    @required bool verboseHelp,
  }) {
23
    addTreeShakeIconsFlag(enabledByDefault: false);
24 25
    usesTargetOption();
    usesPubOption();
26
    addBuildModeFlags(excludeDebug: true);
27
    usesDartDefineOption();
28
    addEnableExperimentation(hide: !verboseHelp);
29
    addNullSafetyModeOptions(hide: !verboseHelp);
30 31 32 33 34 35
    argParser.addFlag('web-initialize-platform',
        defaultsTo: true,
        negatable: true,
        hide: true,
        help: 'Whether to automatically invoke webOnlyInitializePlatform.',
    );
36 37 38
    argParser.addFlag('csp',
      defaultsTo: false,
      negatable: false,
39
      help: 'Disable dynamic generation of code in the generated output. '
40 41
        'This is necessary to satisfy CSP restrictions (see http://www.w3.org/TR/CSP/).'
    );
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    argParser.addOption('pwa-strategy',
      defaultsTo: kOfflineFirst,
      help:
        'The caching strategy to be used by the PWA service worker.\n'
        'offline-first will attempt to cache the app shell eagerly and '
        'then lazily cache all subsequent assets as they are loaded. When '
        'making a network request for an asset, the offline cache will be '
        'preferred.\n'
        'none will generate a service worker with no body. This is useful for '
        'local testing or in cases where the service worker caching functionality '
        'is not desirable',
      allowed: <String>[
        kOfflineFirst,
        kNoneWorker,
      ]
    );
58 59
  }

60
  @override
61 62 63 64
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async =>
      const <DevelopmentArtifact>{
        DevelopmentArtifact.web,
      };
65

66 67 68 69
  @override
  final String name = 'web';

  @override
70
  bool get hidden => !featureFlags.isWebEnabled;
71

72
  @override
73
  final String description = 'Build a web application bundle.';
74 75 76

  @override
  Future<FlutterCommandResult> runCommand() async {
77 78 79
    if (!featureFlags.isWebEnabled) {
      throwToolExit('"build web" is not currently supported.');
    }
80
    final FlutterProject flutterProject = FlutterProject.current();
81
    final String target = stringArg('target');
82
    final BuildInfo buildInfo = getBuildInfo();
83 84 85
    if (buildInfo.isDebug) {
      throwToolExit('debug builds cannot be built directly for the web. Try using "flutter run"');
    }
86 87 88 89
    await buildWeb(
      flutterProject,
      target,
      buildInfo,
90
      boolArg('web-initialize-platform'),
91
      boolArg('csp'),
92
      stringArg('pwa-strategy'),
93
    );
94
    return FlutterCommandResult.success();
95 96
  }
}