File size: 8,723 Bytes
a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | // Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/utils.dart';
import '../build_info.dart';
import '../features.dart';
import '../globals.dart' as globals;
import '../runner/flutter_command.dart'
show DevelopmentArtifact, FlutterCommandResult, FlutterOptions;
import '../web/compile.dart';
import '../web/file_generators/flutter_service_worker_js.dart';
import '../web/web_constants.dart';
import '../web_template.dart';
import 'build.dart';
class BuildWebCommand extends BuildSubCommand {
BuildWebCommand({
required super.logger,
required FileSystem fileSystem,
required bool verboseHelp,
}) : _fileSystem = fileSystem,
super(verboseHelp: verboseHelp) {
addTreeShakeIconsFlag();
usesTargetOption();
usesOutputDir();
usesPubOption();
usesBuildNumberOption();
usesBuildNameOption();
addBuildModeFlags(verboseHelp: verboseHelp);
usesDartDefineOption();
addEnableExperimentation(hide: !verboseHelp);
addNativeNullAssertions();
//
// Flutter web-specific options
//
argParser.addSeparator('Flutter web options');
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',
);
argParser.addOption(
'pwa-strategy',
defaultsTo: ServiceWorkerStrategy.offlineFirst.cliName,
help: 'The caching strategy to be used by the PWA service worker.',
allowed: ServiceWorkerStrategy.values.map((ServiceWorkerStrategy e) => e.cliName),
allowedHelp: CliEnum.allowedHelp(ServiceWorkerStrategy.values),
);
usesWebResourcesCdnFlag();
//
// Common compilation options among JavaScript and Wasm
//
argParser.addOption(
'optimization-level',
abbr: 'O',
help: 'Sets the optimization level used for Dart compilation to JavaScript/Wasm.',
allowed: const <String>['0', '1', '2', '3', '4'],
);
argParser.addFlag(
'source-maps',
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.',
);
//
// JavaScript compilation options
//
argParser.addSeparator('JavaScript compilation options');
argParser.addFlag(
'csp',
negatable: false,
help:
'Disable dynamic generation of code in the generated output. '
'This is necessary to satisfy CSP restrictions (see http://www.w3.org/TR/CSP/).',
);
argParser.addOption(
'dart2js-optimization',
help:
'Sets the optimization level used for Dart compilation to JavaScript. '
'Deprecated: Please use "-O=<level>" / "--optimization-level=<level>".',
allowed: const <String>['O1', 'O2', 'O3', 'O4'],
);
argParser.addFlag(
'dump-info',
negatable: false,
help:
'Passes "--dump-info" to the Javascript compiler which generates '
'information about the generated code in main.dart.js.info.json.',
);
argParser.addFlag(
'no-frequency-based-minification',
negatable: false,
help:
'Disables the frequency based minifier. '
'Useful for comparing the output between builds.',
);
//
// WebAssembly compilation options
//
argParser.addSeparator('WebAssembly compilation options');
argParser.addFlag(
FlutterOptions.kWebWasmFlag,
help: 'Compile to WebAssembly (with fallback to JavaScript).\n$kWasmMoreInfo',
negatable: false,
);
argParser.addFlag(
'strip-wasm',
help: 'Whether to strip the resulting wasm file of static symbol names.',
defaultsTo: true,
);
}
final FileSystem _fileSystem;
@override
Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{
DevelopmentArtifact.web,
};
@override
final String name = 'web';
@override
bool get hidden => !featureFlags.isWebEnabled;
@override
final String description = 'Build a web application bundle.';
@override
Future<FlutterCommandResult> runCommand() async {
if (!featureFlags.isWebEnabled) {
throwToolExit(
'"build web" is not currently supported. To enable, run "flutter config --enable-web".',
);
}
final String? optimizationLevelArg = stringArg('optimization-level');
final int? optimizationLevel =
optimizationLevelArg != null ? int.parse(optimizationLevelArg) : null;
final String? dart2jsOptimizationLevelValue = stringArg('dart2js-optimization');
final int? jsOptimizationLevel =
dart2jsOptimizationLevelValue != null
? int.parse(dart2jsOptimizationLevelValue.substring(1))
: optimizationLevel;
final List<String> dartDefines = extractDartDefines(
defineConfigJsonMap: extractDartDefineConfigJsonMap(),
);
final bool useWasm = boolArg(FlutterOptions.kWebWasmFlag);
// See also: RunCommandBase.webRenderer and TestCommand.webRenderer.
final WebRendererMode webRenderer = WebRendererMode.fromDartDefines(
dartDefines,
useWasm: useWasm,
);
final bool sourceMaps = boolArg('source-maps');
final List<WebCompilerConfig> compilerConfigs;
if (useWasm) {
if (webRenderer != WebRendererMode.getDefault(useWasm: true)) {
throwToolExit(
'Do not attempt to set a web renderer when using "--${FlutterOptions.kWebWasmFlag}"',
);
}
globals.logger.printBox(title: 'New feature', '''
WebAssembly compilation is new. Understand the details before deploying to production.
$kWasmMoreInfo''');
compilerConfigs = <WebCompilerConfig>[
WasmCompilerConfig(
optimizationLevel: optimizationLevel,
stripWasm: boolArg('strip-wasm'),
sourceMaps: sourceMaps,
),
JsCompilerConfig(
csp: boolArg('csp'),
optimizationLevel: jsOptimizationLevel,
dumpInfo: boolArg('dump-info'),
nativeNullAssertions: boolArg('native-null-assertions'),
noFrequencyBasedMinification: boolArg('no-frequency-based-minification'),
sourceMaps: sourceMaps,
),
];
} else {
compilerConfigs = <WebCompilerConfig>[
JsCompilerConfig(
csp: boolArg('csp'),
optimizationLevel: jsOptimizationLevel,
dumpInfo: boolArg('dump-info'),
nativeNullAssertions: boolArg('native-null-assertions'),
noFrequencyBasedMinification: boolArg('no-frequency-based-minification'),
sourceMaps: sourceMaps,
renderer: webRenderer,
),
];
}
final BuildInfo buildInfo = await getBuildInfo();
final String? baseHref = stringArg('base-href');
if (baseHref != null && !(baseHref.startsWith('/') && baseHref.endsWith('/'))) {
throwToolExit(
'Received a --base-href value of "$baseHref"\n'
'--base-href should start and end with /',
);
}
if (!project.web.existsSync()) {
throwToolExit('Missing index.html.');
}
if (!_fileSystem.currentDirectory
.childDirectory('web')
.childFile('index.html')
.readAsStringSync()
.contains(kBaseHrefPlaceholder) &&
baseHref != null) {
throwToolExit(
"Couldn't find the placeholder for base href. "
'Please add `<base href="$kBaseHrefPlaceholder">` to web/index.html',
);
}
// Currently supporting options [output-dir] and [output] as
// valid approaches for setting output directory of build artifacts
final String? outputDirectoryPath = stringArg('output');
final WebBuilder webBuilder = WebBuilder(
logger: globals.logger,
processManager: globals.processManager,
buildSystem: globals.buildSystem,
fileSystem: globals.fs,
flutterVersion: globals.flutterVersion,
analytics: globals.analytics,
);
await webBuilder.buildWeb(
project,
targetFile,
buildInfo,
ServiceWorkerStrategy.fromCliName(stringArg('pwa-strategy')),
compilerConfigs: compilerConfigs,
baseHref: baseHref,
outputDirectoryPath: outputDirectoryPath,
);
return FlutterCommandResult.success();
}
}
|