55/// JavaScript API a Flutter Web application can use to configure the Web
66/// Engine.
77///
8- /// The configuration is a plain JavaScript object set as the
9- /// `flutterConfiguration` property of the top-level `window` object.
8+ /// The configuration is passed from JavaScript to the engine as part of the
9+ /// bootstrap process, through the `FlutterEngineInitializer.initializeEngine`
10+ /// JS method, with an (optional) object of type [JsFlutterConfiguration] .
11+ ///
12+ /// This library also supports the legacy method of setting a plain JavaScript
13+ /// object set as the `flutterConfiguration` property of the top-level `window`
14+ /// object, but that approach is now deprecated and will warn users.
15+ ///
16+ /// Both methods are **disallowed** to be used at the same time.
1017///
1118/// Example:
1219///
13- /// <head>
14- /// <script>
15- /// window.flutterConfiguration = {
16- /// canvasKitBaseUrl: "https://example.com/my-custom-canvaskit/"
17- /// };
18- /// </script>
19- /// </head>
20+ /// _flutter.loader.loadEntrypoint({
21+ /// // ...
22+ /// onEntrypointLoaded: async function(engineInitializer) {
23+ /// let appRunner = await engineInitializer.initializeEngine({
24+ /// // JsFlutterConfiguration goes here...
25+ /// canvasKitBaseUrl: "https://example.com/my-custom-canvaskit/",
26+ /// });
27+ /// appRunner.runApp();
28+ /// }
29+ /// });
30+ ///
31+ /// Example of the **deprecated** style (this will issue a JS console warning!):
32+ ///
33+ /// <script>
34+ /// window.flutterConfiguration = {
35+ /// canvasKitBaseUrl: "https://example.com/my-custom-canvaskit/"
36+ /// };
37+ /// </script>
2038///
21- /// Configuration properties supplied via `window.flutterConfiguration`
22- /// override those supplied using the corresponding environment variables. For
23- /// example, if both `window.flutterConfiguration.canvasKitBaseUrl` and the
24- /// `FLUTTER_WEB_CANVASKIT_URL` environment variables are provided,
25- /// `window.flutterConfiguration.canvasKitBaseUrl` is used.
39+ /// Configuration properties supplied via this object override those supplied
40+ /// using the corresponding environment variables. For example, if both the
41+ /// `canvasKitBaseUrl` config entry and the `FLUTTER_WEB_CANVASKIT_URL`
42+ /// environment variables are provided, the `canvasKitBaseUrl` entry is used.
2643
2744@JS ()
2845library configuration;
2946
3047import 'package:js/js.dart' ;
48+ import 'package:meta/meta.dart' ;
49+ import 'dom.dart' ;
3150
3251/// The version of CanvasKit used by the web engine by default.
3352// DO NOT EDIT THE NEXT LINE OF CODE MANUALLY
3453// See `lib/web_ui/README.md` for how to roll CanvasKit to a new version.
3554const String _canvaskitVersion = '0.37.0' ;
3655
3756/// The Web Engine configuration for the current application.
38- FlutterConfiguration get configuration => _configuration ?? = FlutterConfiguration (_jsConfiguration);
57+ FlutterConfiguration get configuration =>
58+ _configuration ?? = FlutterConfiguration .legacy (_jsConfiguration);
3959FlutterConfiguration ? _configuration;
4060
4161/// Sets the given configuration as the current one.
4262///
4363/// This must be called before the engine is initialized. Calling it after the
4464/// engine is initialized will result in some of the properties not taking
4565/// effect because they are consumed during initialization.
66+ @visibleForTesting
4667void debugSetConfiguration (FlutterConfiguration configuration) {
4768 _configuration = configuration;
4869}
4970
5071/// Supplies Web Engine configuration properties.
5172class FlutterConfiguration {
52- /// Constructs a configuration from a JavaScript object containing
53- /// runtime-supplied properties.
54- FlutterConfiguration (this ._js );
73+ /// Constructs an unitialized configuration object.
74+ @visibleForTesting
75+ FlutterConfiguration ();
5576
56- final JsFlutterConfiguration ? _js;
77+ /// Constucts a "tainted by JS globals" configuration object.
78+ ///
79+ /// This configuration style is deprecated. It will warn the user about the
80+ /// new API (if used)
81+ FlutterConfiguration .legacy (JsFlutterConfiguration ? config) {
82+ if (config != null ) {
83+ _usedLegacyConfigStyle = true ;
84+ _configuration = config;
85+ }
86+ // Warn the user of the deprecated behavior.
87+ assert (() {
88+ if (config != null ) {
89+ domWindow.console.warn ('window.flutterConfiguration is now deprecated.\n '
90+ 'Use engineInitializer.initializeEngine(config) instead.\n '
91+ 'See: https://docs.flutter.dev/development/platform-integration/web/initialization' );
92+ }
93+ if (_requestedRendererType != null ) {
94+ domWindow.console.warn ('window.flutterWebRenderer is now deprecated.\n '
95+ 'Use engineInitializer.initializeEngine(config) instead.\n '
96+ 'See: https://docs.flutter.dev/development/platform-integration/web/initialization' );
97+ }
98+ return true ;
99+ }());
100+ }
101+
102+ bool _usedLegacyConfigStyle = false ;
103+ JsFlutterConfiguration ? _configuration;
104+
105+ /// Sets a value for [_configuration] .
106+ ///
107+ /// This method is called by the engine initialization process, through the
108+ /// [initEngineServices] method.
109+ ///
110+ /// This method throws an AssertionError, if the _configuration object has
111+ /// been set to anything non-null through the [FlutterConfiguration.legacy]
112+ /// constructor.
113+ void setUserConfiguration (JsFlutterConfiguration ? configuration) {
114+ if (configuration != null ) {
115+ assert (! _usedLegacyConfigStyle,
116+ 'Use engineInitializer.initializeEngine(config) only. '
117+ 'Using the (deprecated) window.flutterConfiguration and initializeEngine '
118+ 'configuration simultaneously is not supported.' );
119+ assert (_requestedRendererType == null || configuration.renderer == null ,
120+ 'Use engineInitializer.initializeEngine(config) only. '
121+ 'Using the (deprecated) window.flutterWebRenderer and initializeEngine '
122+ 'configuration simultaneously is not supported.' );
123+ _configuration = configuration;
124+ }
125+ }
57126
58127 // Static constant parameters.
59128 //
60129 // These properties affect tree shaking and therefore cannot be supplied at
61- // runtime. They must be static constants for the compiler to remove dead
130+ // runtime. They must be static constants for the compiler to remove dead code
62131 // effectively.
63132
64133 /// Auto detect which rendering backend to use.
@@ -110,7 +179,7 @@ class FlutterConfiguration {
110179 /// --web-renderer=canvaskit \
111180 /// --dart-define=FLUTTER_WEB_CANVASKIT_URL=https://example.com/custom-canvaskit-build/
112181 /// ```
113- String get canvasKitBaseUrl => _js ? .canvasKitBaseUrl ?? _defaultCanvasKitBaseUrl;
182+ String get canvasKitBaseUrl => _configuration ? .canvasKitBaseUrl ?? _defaultCanvasKitBaseUrl;
114183 static const String _defaultCanvasKitBaseUrl = String .fromEnvironment (
115184 'FLUTTER_WEB_CANVASKIT_URL' ,
116185 defaultValue: 'https://unpkg.com/canvaskit-wasm@$_canvaskitVersion /bin/' ,
@@ -121,7 +190,7 @@ class FlutterConfiguration {
121190 ///
122191 /// This is mainly used for testing or for apps that want to ensure they
123192 /// run on devices which don't support WebGL.
124- bool get canvasKitForceCpuOnly => _js ? .canvasKitForceCpuOnly ?? _defaultCanvasKitForceCpuOnly;
193+ bool get canvasKitForceCpuOnly => _configuration ? .canvasKitForceCpuOnly ?? _defaultCanvasKitForceCpuOnly;
125194 static const bool _defaultCanvasKitForceCpuOnly = bool .fromEnvironment (
126195 'FLUTTER_WEB_CANVASKIT_FORCE_CPU_ONLY' ,
127196 );
@@ -135,7 +204,7 @@ class FlutterConfiguration {
135204 ///
136205 /// This value can be specified using either the `FLUTTER_WEB_MAXIMUM_SURFACES`
137206 /// environment variable, or using the runtime configuration.
138- int get canvasKitMaximumSurfaces => _js ? .canvasKitMaximumSurfaces ?? _defaultCanvasKitMaximumSurfaces;
207+ int get canvasKitMaximumSurfaces => _configuration ? .canvasKitMaximumSurfaces ?? _defaultCanvasKitMaximumSurfaces;
139208 static const int _defaultCanvasKitMaximumSurfaces = int .fromEnvironment (
140209 'FLUTTER_WEB_MAXIMUM_SURFACES' ,
141210 defaultValue: 8 ,
@@ -152,10 +221,23 @@ class FlutterConfiguration {
152221 /// ```
153222 /// flutter run -d chrome --profile --dart-define=FLUTTER_WEB_DEBUG_SHOW_SEMANTICS=true
154223 /// ```
155- bool get debugShowSemanticsNodes => _js ? .debugShowSemanticsNodes ?? _defaultDebugShowSemanticsNodes;
224+ bool get debugShowSemanticsNodes => _configuration ? .debugShowSemanticsNodes ?? _defaultDebugShowSemanticsNodes;
156225 static const bool _defaultDebugShowSemanticsNodes = bool .fromEnvironment (
157226 'FLUTTER_WEB_DEBUG_SHOW_SEMANTICS' ,
158227 );
228+
229+ /// Returns the [hostElement] in which the Flutter Application is supposed
230+ /// to render, or `null` if the user hasn't specified anything.
231+ DomElement ? get hostElement => _configuration? .hostElement;
232+
233+ /// Returns the [requestedRendererType] to be used with the current Flutter
234+ /// application, normally 'canvaskit' or 'auto'.
235+ ///
236+ /// This value may come from the JS configuration, but also a specific JS value:
237+ /// `window.flutterWebRenderer` .
238+ ///
239+ /// This is used by the Renderer class to decide how to initialize the engine.
240+ String ? get requestedRendererType => _configuration? .renderer ?? _requestedRendererType;
159241}
160242
161243@JS ('window.flutterConfiguration' )
@@ -169,13 +251,13 @@ class JsFlutterConfiguration {}
169251extension JsFlutterConfigurationExtension on JsFlutterConfiguration {
170252 external String ? get canvasKitBaseUrl;
171253 external bool ? get canvasKitForceCpuOnly;
172- external bool ? get debugShowSemanticsNodes;
173-
174254 external int ? get canvasKitMaximumSurfaces;
175- external set canvasKitMaximumSurfaces (int ? maxSurfaces);
255+ external bool ? get debugShowSemanticsNodes;
256+ external DomElement ? get hostElement;
257+ external String ? get renderer;
176258}
177259
178260/// A JavaScript entrypoint that allows developer to set rendering backend
179261/// at runtime before launching the application.
180262@JS ('window.flutterWebRenderer' )
181- external String ? get requestedRendererType ;
263+ external String ? get _requestedRendererType ;
0 commit comments