build_web.dart 4.47 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 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 '../globals_null_migrated.dart' as globals;
14
import '../project.dart';
15
import '../runner/flutter_command.dart'
16
    show DevelopmentArtifact, FlutterCommandResult;
17 18 19 20
import '../web/compile.dart';
import 'build.dart';

class BuildWebCommand extends BuildSubCommand {
21 22 23
  BuildWebCommand({
    @required bool verboseHelp,
  }) {
24
    addTreeShakeIconsFlag(enabledByDefault: false);
25 26
    usesTargetOption();
    usesPubOption();
27
    addBuildModeFlags(verboseHelp: verboseHelp, excludeDebug: true);
28
    usesDartDefineOption();
29
    usesWebRendererOption();
30
    addEnableExperimentation(hide: !verboseHelp);
31
    addNullSafetyModeOptions(hide: !verboseHelp);
32
    addNativeNullAssertions(hide: false);
33 34 35
    argParser.addFlag('csp',
      defaultsTo: false,
      negatable: false,
36
      help: 'Disable dynamic generation of code in the generated output. '
37
            'This is necessary to satisfy CSP restrictions (see http://www.w3.org/TR/CSP/).'
38
    );
39 40 41
    argParser.addFlag(
      'source-maps',
      defaultsTo: false,
42 43 44
      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.'
45
    );
46

47 48
    argParser.addOption('pwa-strategy',
      defaultsTo: kOfflineFirst,
49
      help: 'The caching strategy to be used by the PWA service worker.',
50 51 52
      allowed: <String>[
        kOfflineFirst,
        kNoneWorker,
53 54 55 56 57 58 59 60 61 62
      ],
      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',
      },
63
    );
64 65 66 67 68 69 70
    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'
    );

71 72
  }

73
  @override
74 75 76 77
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async =>
      const <DevelopmentArtifact>{
        DevelopmentArtifact.web,
      };
78

79 80 81 82
  @override
  final String name = 'web';

  @override
83
  bool get hidden => !featureFlags.isWebEnabled;
84

85
  @override
86
  final String description = 'Build a web application bundle.';
87 88 89

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