build_web.dart 4.42 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
import '../base/common.dart';
6
import '../build_info.dart';
7
import '../build_system/targets/web.dart';
8
import '../features.dart';
9
import '../globals.dart' as globals;
10
import '../project.dart';
11
import '../runner/flutter_command.dart'
12
    show DevelopmentArtifact, FlutterCommandResult;
13 14 15 16
import '../web/compile.dart';
import 'build.dart';

class BuildWebCommand extends BuildSubCommand {
17
  BuildWebCommand({
18
    required bool verboseHelp,
19
  }) : super(verboseHelp: verboseHelp) {
20
    addTreeShakeIconsFlag(enabledByDefault: false);
21 22
    usesTargetOption();
    usesPubOption();
23 24
    usesBuildNumberOption();
    usesBuildNameOption();
25
    addBuildModeFlags(verboseHelp: verboseHelp, excludeDebug: true);
26
    usesDartDefineOption();
27
    usesWebRendererOption();
28
    addEnableExperimentation(hide: !verboseHelp);
29
    addNullSafetyModeOptions(hide: !verboseHelp);
30
    addNativeNullAssertions();
31 32
    argParser.addFlag('csp',
      negatable: false,
33
      help: 'Disable dynamic generation of code in the generated output. '
34
            'This is necessary to satisfy CSP restrictions (see http://www.w3.org/TR/CSP/).'
35
    );
36 37
    argParser.addFlag(
      'source-maps',
38 39 40
      help: 'Generate a sourcemap file. These can be used by browsers '
            'to view and debug the original source code of a compiled and minified Dart '
            'application.'
41
    );
42

43 44
    argParser.addOption('pwa-strategy',
      defaultsTo: kOfflineFirst,
45
      help: 'The caching strategy to be used by the PWA service worker.',
46 47 48
      allowed: <String>[
        kOfflineFirst,
        kNoneWorker,
49 50 51 52 53 54 55 56 57 58
      ],
      allowedHelp: <String, String>{
        kOfflineFirst: 'Attempt to cache the application 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.',
        kNoneWorker:   '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',
      },
59
    );
60 61 62 63 64 65 66
    argParser.addOption('base-href',
      help: 'Overrides the href attribute of the <base> tag in web/index.html. '
          'No change is done to web/index.html file if this flag is not provided. '
          'The value has to start and end with a slash "/". '
          'For more information: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base'
    );

67 68
  }

69
  @override
70 71 72 73
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async =>
      const <DevelopmentArtifact>{
        DevelopmentArtifact.web,
      };
74

75 76 77 78
  @override
  final String name = 'web';

  @override
79
  bool get hidden => !featureFlags.isWebEnabled;
80

81
  @override
82
  final String description = 'Build a web application bundle.';
83 84 85

  @override
  Future<FlutterCommandResult> runCommand() async {
86
    if (!featureFlags.isWebEnabled) {
87
      throwToolExit('"build web" is not currently supported. To enable, run "flutter config --enable-web".');
88
    }
89
    final FlutterProject flutterProject = FlutterProject.current();
90
    final String target = stringArg('target')!;
91
    final BuildInfo buildInfo = await getBuildInfo();
92 93 94
    if (buildInfo.isDebug) {
      throwToolExit('debug builds cannot be built directly for the web. Try using "flutter run"');
    }
95 96
    final String? baseHref = stringArg('base-href');
    if (baseHref != null && !(baseHref.startsWith('/') && baseHref.endsWith('/'))) {
97 98
      throwToolExit('base-href should start and end with /');
    }
99 100 101
    if (!flutterProject.web.existsSync()) {
      throwToolExit('Missing index.html.');
    }
102 103 104 105 106
    if (!globals.fs.currentDirectory
        .childDirectory('web')
        .childFile('index.html')
        .readAsStringSync()
        .contains(kBaseHrefPlaceholder) &&
107
        baseHref != null) {
108 109 110 111 112
      throwToolExit(
        "Couldn't find the placeholder for base href. "
        r'Please add `<base href="$FLUTTER_BASE_HREF">` to web/index.html'
      );
    }
113
    displayNullSafetyMode(buildInfo);
114 115 116 117
    await buildWeb(
      flutterProject,
      target,
      buildInfo,
118
      boolArg('csp'),
119
      stringArg('pwa-strategy')!,
120
      boolArg('source-maps'),
121
      boolArg('native-null-assertions'),
122
      baseHref,
123
    );
124
    return FlutterCommandResult.success();
125 126
  }
}