@@ -16,96 +16,112 @@ import '../globals.dart' as globals;
16
16
import '../platform_plugins.dart' ;
17
17
import '../plugins.dart' ;
18
18
import '../project.dart' ;
19
+ import '../reporting/reporting.dart' ;
20
+ import '../version.dart' ;
19
21
import 'migrations/scrub_generated_plugin_registrant.dart' ;
20
22
21
23
export '../build_system/targets/web.dart' show kDart2jsDefaultOptimizationLevel;
22
24
23
- Future <void > buildWeb (
24
- FlutterProject flutterProject,
25
- String target,
26
- BuildInfo buildInfo,
27
- bool csp,
28
- String serviceWorkerStrategy,
29
- bool sourceMaps,
30
- bool nativeNullAssertions,
31
- bool isWasm, {
32
- String dart2jsOptimization = kDart2jsDefaultOptimizationLevel,
33
- String ? baseHref,
34
- bool dumpInfo = false ,
35
- bool noFrequencyBasedMinification = false ,
36
- String ? outputDirectoryPath,
37
- }) async {
38
- final bool hasWebPlugins = (await findPlugins (flutterProject))
39
- .any ((Plugin p) => p.platforms.containsKey (WebPlugin .kConfigKey));
40
- final Directory outputDirectory = outputDirectoryPath == null
41
- ? globals.fs.directory (getWebBuildDirectory (isWasm))
42
- : globals.fs.directory (outputDirectoryPath);
43
- outputDirectory.createSync (recursive: true );
25
+ class WebBuilder {
26
+ WebBuilder ({
27
+ required Logger logger,
28
+ required BuildSystem buildSystem,
29
+ required Usage usage,
30
+ required FlutterVersion flutterVersion,
31
+ required FileSystem fileSystem,
32
+ }) : _logger = logger,
33
+ _buildSystem = buildSystem,
34
+ _flutterUsage = usage,
35
+ _flutterVersion = flutterVersion,
36
+ _fileSystem = fileSystem;
44
37
45
- // The migrators to apply to a Web project.
46
- final List <ProjectMigrator > migrators = < ProjectMigrator > [
47
- ScrubGeneratedPluginRegistrant (flutterProject.web, globals.logger),
48
- ];
38
+ final Logger _logger;
39
+ final BuildSystem _buildSystem;
40
+ final Usage _flutterUsage;
41
+ final FlutterVersion _flutterVersion;
42
+ final FileSystem _fileSystem;
49
43
50
- final ProjectMigration migration = ProjectMigration (migrators);
51
- migration.run ();
44
+ Future <void > buildWeb (
45
+ FlutterProject flutterProject,
46
+ String target,
47
+ BuildInfo buildInfo,
48
+ bool csp,
49
+ String serviceWorkerStrategy,
50
+ bool sourceMaps,
51
+ bool nativeNullAssertions,
52
+ bool isWasm, {
53
+ String dart2jsOptimization = kDart2jsDefaultOptimizationLevel,
54
+ String ? baseHref,
55
+ bool dumpInfo = false ,
56
+ bool noFrequencyBasedMinification = false ,
57
+ String ? outputDirectoryPath,
58
+ }) async {
59
+ final bool hasWebPlugins =
60
+ (await findPlugins (flutterProject)).any ((Plugin p) => p.platforms.containsKey (WebPlugin .kConfigKey));
61
+ final Directory outputDirectory = outputDirectoryPath == null
62
+ ? _fileSystem.directory (getWebBuildDirectory (isWasm))
63
+ : _fileSystem.directory (outputDirectoryPath);
64
+ outputDirectory.createSync (recursive: true );
52
65
53
- final Status status = globals.logger.startProgress ('Compiling $target for the Web...' );
54
- final Stopwatch sw = Stopwatch ()..start ();
55
- try {
56
- final BuildResult result = await globals.buildSystem.build (
57
- WebServiceWorker (globals.fs, buildInfo.webRenderer, isWasm: isWasm),
58
- Environment (
59
- projectDir: globals.fs.currentDirectory,
60
- outputDir: outputDirectory,
61
- buildDir: flutterProject.directory
62
- .childDirectory ('.dart_tool' )
63
- .childDirectory ('flutter_build' ),
64
- defines: < String , String > {
65
- kTargetFile: target,
66
- kHasWebPlugins: hasWebPlugins.toString (),
67
- kCspMode: csp.toString (),
68
- if (baseHref != null )
69
- kBaseHref : baseHref,
70
- kSourceMapsEnabled: sourceMaps.toString (),
71
- kNativeNullAssertions: nativeNullAssertions.toString (),
72
- kServiceWorkerStrategy: serviceWorkerStrategy,
73
- kDart2jsOptimization: dart2jsOptimization,
74
- kDart2jsDumpInfo: dumpInfo.toString (),
75
- kDart2jsNoFrequencyBasedMinification: noFrequencyBasedMinification.toString (),
76
- ...buildInfo.toBuildSystemEnvironment (),
77
- },
78
- artifacts: globals.artifacts! ,
79
- fileSystem: globals.fs,
80
- logger: globals.logger,
81
- processManager: globals.processManager,
82
- platform: globals.platform,
83
- usage: globals.flutterUsage,
84
- cacheDir: globals.cache.getRoot (),
85
- engineVersion: globals.artifacts! .isLocalEngine
86
- ? null
87
- : globals.flutterVersion.engineRevision,
88
- flutterRootDir: globals.fs.directory (Cache .flutterRoot),
89
- // Web uses a different Dart plugin registry.
90
- // https://github.com/flutter/flutter/issues/80406
91
- generateDartPluginRegistry: false ,
92
- ));
93
- if (! result.success) {
94
- for (final ExceptionMeasurement measurement in result.exceptions.values) {
95
- globals.printError ('Target ${measurement .target } failed: ${measurement .exception }' ,
96
- stackTrace: measurement.fatal
97
- ? measurement.stackTrace
98
- : null ,
99
- );
66
+ // The migrators to apply to a Web project.
67
+ final List <ProjectMigrator > migrators = < ProjectMigrator > [
68
+ ScrubGeneratedPluginRegistrant (flutterProject.web, _logger),
69
+ ];
70
+
71
+ final ProjectMigration migration = ProjectMigration (migrators);
72
+ migration.run ();
73
+
74
+ final Status status = _logger.startProgress ('Compiling $target for the Web...' );
75
+ final Stopwatch sw = Stopwatch ()..start ();
76
+ try {
77
+ final BuildResult result = await _buildSystem.build (
78
+ WebServiceWorker (_fileSystem, buildInfo.webRenderer, isWasm: isWasm),
79
+ Environment (
80
+ projectDir: _fileSystem.currentDirectory,
81
+ outputDir: outputDirectory,
82
+ buildDir: flutterProject.directory.childDirectory ('.dart_tool' ).childDirectory ('flutter_build' ),
83
+ defines: < String , String > {
84
+ kTargetFile: target,
85
+ kHasWebPlugins: hasWebPlugins.toString (),
86
+ kCspMode: csp.toString (),
87
+ if (baseHref != null ) kBaseHref: baseHref,
88
+ kSourceMapsEnabled: sourceMaps.toString (),
89
+ kNativeNullAssertions: nativeNullAssertions.toString (),
90
+ kServiceWorkerStrategy: serviceWorkerStrategy,
91
+ kDart2jsOptimization: dart2jsOptimization,
92
+ kDart2jsDumpInfo: dumpInfo.toString (),
93
+ kDart2jsNoFrequencyBasedMinification: noFrequencyBasedMinification.toString (),
94
+ ...buildInfo.toBuildSystemEnvironment (),
95
+ },
96
+ artifacts: globals.artifacts! ,
97
+ fileSystem: _fileSystem,
98
+ logger: _logger,
99
+ processManager: globals.processManager,
100
+ platform: globals.platform,
101
+ usage: _flutterUsage,
102
+ cacheDir: globals.cache.getRoot (),
103
+ engineVersion: globals.artifacts! .isLocalEngine ? null : _flutterVersion.engineRevision,
104
+ flutterRootDir: _fileSystem.directory (Cache .flutterRoot),
105
+ // Web uses a different Dart plugin registry.
106
+ // https://github.com/flutter/flutter/issues/80406
107
+ generateDartPluginRegistry: false ,
108
+ ));
109
+ if (! result.success) {
110
+ for (final ExceptionMeasurement measurement in result.exceptions.values) {
111
+ _logger.printError (
112
+ 'Target ${measurement .target } failed: ${measurement .exception }' ,
113
+ stackTrace: measurement.fatal ? measurement.stackTrace : null ,
114
+ );
115
+ }
116
+ throwToolExit ('Failed to compile application for the Web.' );
100
117
}
101
- throwToolExit ('Failed to compile application for the Web.' );
118
+ } on Exception catch (err) {
119
+ throwToolExit (err.toString ());
120
+ } finally {
121
+ status.stop ();
102
122
}
103
- } on Exception catch (err) {
104
- throwToolExit (err.toString ());
105
- } finally {
106
- status.stop ();
123
+ _flutterUsage.sendTiming ('build' , 'dart2js' , Duration (milliseconds: sw.elapsedMilliseconds));
107
124
}
108
- globals.flutterUsage.sendTiming ('build' , 'dart2js' , Duration (milliseconds: sw.elapsedMilliseconds));
109
125
}
110
126
111
127
/// Web rendering backend mode.
0 commit comments