diff --git a/packages/flutter_tools/lib/src/commands/build_web.dart b/packages/flutter_tools/lib/src/commands/build_web.dart index c90fb78cc3..091e676d09 100644 --- a/packages/flutter_tools/lib/src/commands/build_web.dart +++ b/packages/flutter_tools/lib/src/commands/build_web.dart @@ -208,7 +208,6 @@ class BuildWebCommand extends BuildSubCommand { ]; } - final String target = stringArg('target')!; final BuildInfo buildInfo = await getBuildInfo(); final String? baseHref = stringArg('base-href'); if (baseHref != null && !(baseHref.startsWith('/') && baseHref.endsWith('/'))) { @@ -246,7 +245,7 @@ class BuildWebCommand extends BuildSubCommand { ); await webBuilder.buildWeb( project, - target, + targetFile, buildInfo, ServiceWorkerStrategy.fromCliName(stringArg('pwa-strategy')), compilerConfigs: compilerConfigs, diff --git a/packages/flutter_tools/test/commands.shard/hermetic/build_web_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/build_web_test.dart index 6b2c933f10..01f7fa1583 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/build_web_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/build_web_test.dart @@ -43,6 +43,7 @@ void main() { writePackageConfigFile(mainLibName: 'foo', directory: fileSystem.currentDirectory); fileSystem.file(fileSystem.path.join('web', 'index.html')).createSync(recursive: true); fileSystem.file(fileSystem.path.join('lib', 'main.dart')).createSync(recursive: true); + fileSystem.file(fileSystem.path.join('lib', 'a.dart')).createSync(recursive: true); logger = BufferLogger.test(); processManager = FakeProcessManager.any(); }); @@ -155,6 +156,110 @@ void main() { }, ); + testUsingContext( + 'Infers target entrypoint correctly from --target', + () async { + // Regression test for https://github.com/flutter/flutter/issues/136830. + final BuildCommand buildCommand = BuildCommand( + androidSdk: FakeAndroidSdk(), + buildSystem: TestBuildSystem.all(BuildResult(success: true)), + fileSystem: fileSystem, + logger: logger, + osUtils: FakeOperatingSystemUtils(), + ); + final CommandRunner runner = createTestCommandRunner(buildCommand); + setupFileSystemForEndToEndTest(fileSystem); + await runner.run([ + 'build', + 'web', + '--no-pub', + '--no-web-resources-cdn', + '--target=lib/a.dart', + ]); + + final Directory buildDir = fileSystem.directory(fileSystem.path.join('build', 'web')); + expect(buildDir.existsSync(), true); + expect(testLogger.statusText, contains('Compiling lib/a.dart for the Web...')); + expect(testLogger.statusText, contains('✓ Built ${buildDir.path}')); + }, + overrides: { + Platform: () => fakePlatform, + FileSystem: () => fileSystem, + FeatureFlags: () => TestFeatureFlags(isWebEnabled: true), + ProcessManager: () => processManager, + BuildSystem: + () => TestBuildSystem.all(BuildResult(success: true), ( + Target target, + Environment environment, + ) { + expect(environment.defines, { + 'TargetFile': 'lib/a.dart', + 'HasWebPlugins': 'true', + 'ServiceWorkerStrategy': 'offline-first', + 'BuildMode': 'release', + 'DartDefines': + 'RkxVVFRFUl9WRVJTSU9OPTAuMC4w,RkxVVFRFUl9DSEFOTkVMPW1hc3Rlcg==,RkxVVFRFUl9HSVRfVVJMPWh0dHBzOi8vZ2l0aHViLmNvbS9mbHV0dGVyL2ZsdXR0ZXIuZ2l0,RkxVVFRFUl9GUkFNRVdPUktfUkVWSVNJT049MTExMTE=,RkxVVFRFUl9FTkdJTkVfUkVWSVNJT049YWJjZGU=,RkxVVFRFUl9EQVJUX1ZFUlNJT049MTI=', + 'DartObfuscation': 'false', + 'TrackWidgetCreation': 'false', + 'TreeShakeIcons': 'true', + 'UseLocalCanvasKit': 'true', + }); + }), + }, + ); + + testUsingContext( + 'Infers target entrypoint correctly from positional argument list', + () async { + // Regression test for https://github.com/flutter/flutter/issues/136830. + final BuildCommand buildCommand = BuildCommand( + androidSdk: FakeAndroidSdk(), + buildSystem: TestBuildSystem.all(BuildResult(success: true)), + fileSystem: fileSystem, + logger: logger, + osUtils: FakeOperatingSystemUtils(), + ); + final CommandRunner runner = createTestCommandRunner(buildCommand); + setupFileSystemForEndToEndTest(fileSystem); + await runner.run([ + 'build', + 'web', + '--no-pub', + '--no-web-resources-cdn', + 'lib/a.dart', + ]); + + final Directory buildDir = fileSystem.directory(fileSystem.path.join('build', 'web')); + expect(buildDir.existsSync(), true); + expect(testLogger.statusText, contains('Compiling lib/a.dart for the Web...')); + expect(testLogger.statusText, contains('✓ Built ${buildDir.path}')); + }, + overrides: { + Platform: () => fakePlatform, + FileSystem: () => fileSystem, + FeatureFlags: () => TestFeatureFlags(isWebEnabled: true), + ProcessManager: () => processManager, + BuildSystem: + () => TestBuildSystem.all(BuildResult(success: true), ( + Target target, + Environment environment, + ) { + expect(environment.defines, { + 'TargetFile': 'lib/a.dart', + 'HasWebPlugins': 'true', + 'ServiceWorkerStrategy': 'offline-first', + 'BuildMode': 'release', + 'DartDefines': + 'RkxVVFRFUl9WRVJTSU9OPTAuMC4w,RkxVVFRFUl9DSEFOTkVMPW1hc3Rlcg==,RkxVVFRFUl9HSVRfVVJMPWh0dHBzOi8vZ2l0aHViLmNvbS9mbHV0dGVyL2ZsdXR0ZXIuZ2l0,RkxVVFRFUl9GUkFNRVdPUktfUkVWSVNJT049MTExMTE=,RkxVVFRFUl9FTkdJTkVfUkVWSVNJT049YWJjZGU=,RkxVVFRFUl9EQVJUX1ZFUlNJT049MTI=', + 'DartObfuscation': 'false', + 'TrackWidgetCreation': 'false', + 'TreeShakeIcons': 'true', + 'UseLocalCanvasKit': 'true', + }); + }), + }, + ); + testUsingContext( 'Does not allow -O0 optimization level', () async {