Committed by
GitHub
Merge pull request #471 from p-mazhnik/pavel/web-debug-fix
fix(web): fix loading js scripts in debug mode in flutter 3.7
Showing
1 changed file
with
18 additions
and
31 deletions
| 1 | import 'dart:async'; | 1 | import 'dart:async'; |
| 2 | import 'dart:html' as html; | 2 | import 'dart:html' as html; |
| 3 | -import 'dart:js' show context; | 3 | +import 'dart:js' show context, JsObject; |
| 4 | 4 | ||
| 5 | -import 'package:js/js.dart'; | ||
| 6 | import 'package:mobile_scanner/src/web/base.dart'; | 5 | import 'package:mobile_scanner/src/web/base.dart'; |
| 7 | 6 | ||
| 8 | Future<void> loadScript(JsLibrary library) async { | 7 | Future<void> loadScript(JsLibrary library) async { |
| 8 | + dynamic amd; | ||
| 9 | + dynamic define; | ||
| 9 | // ignore: avoid_dynamic_calls | 10 | // ignore: avoid_dynamic_calls |
| 10 | if (library.usesRequireJs && context['define']?['amd'] != null) { | 11 | if (library.usesRequireJs && context['define']?['amd'] != null) { |
| 12 | + // In dev, requireJs is loaded in. Disable it here. | ||
| 11 | // see https://github.com/dart-lang/sdk/issues/33979 | 13 | // see https://github.com/dart-lang/sdk/issues/33979 |
| 12 | - return loadScriptUsingRequireJS(library.contextName, library.url); | ||
| 13 | - } else { | ||
| 14 | - return loadScriptUsingScriptTag(library.url); | 14 | + define = JsObject.fromBrowserObject(context['define'] as Object); |
| 15 | + // ignore: avoid_dynamic_calls | ||
| 16 | + amd = define['amd']; | ||
| 17 | + // ignore: avoid_dynamic_calls | ||
| 18 | + define['amd'] = false; | ||
| 19 | + } | ||
| 20 | + try { | ||
| 21 | + await loadScriptUsingScriptTag(library.url); | ||
| 22 | + } finally { | ||
| 23 | + if (amd != null) { | ||
| 24 | + // Re-enable requireJs | ||
| 25 | + // ignore: avoid_dynamic_calls | ||
| 26 | + define['amd'] = amd; | ||
| 27 | + } | ||
| 15 | } | 28 | } |
| 16 | } | 29 | } |
| 17 | 30 | ||
| @@ -29,32 +42,6 @@ Future<void> loadScriptUsingScriptTag(String url) { | @@ -29,32 +42,6 @@ Future<void> loadScriptUsingScriptTag(String url) { | ||
| 29 | return script.onLoad.first; | 42 | return script.onLoad.first; |
| 30 | } | 43 | } |
| 31 | 44 | ||
| 32 | -Future<void> loadScriptUsingRequireJS(String packageName, String url) { | ||
| 33 | - final Completer completer = Completer(); | ||
| 34 | - final String eventName = '_${packageName}Loaded'; | ||
| 35 | - | ||
| 36 | - context.callMethod( | ||
| 37 | - 'addEventListener', | ||
| 38 | - [eventName, allowInterop((_) => completer.complete())], | ||
| 39 | - ); | ||
| 40 | - | ||
| 41 | - final script = html.ScriptElement() | ||
| 42 | - ..type = 'text/javascript' | ||
| 43 | - ..async = false | ||
| 44 | - ..defer = false | ||
| 45 | - ..text = ''' | ||
| 46 | - require(["$url"], (package) => { | ||
| 47 | - window.$packageName = package; | ||
| 48 | - const event = new Event("$eventName"); | ||
| 49 | - dispatchEvent(event); | ||
| 50 | - }) | ||
| 51 | - '''; | ||
| 52 | - | ||
| 53 | - html.document.head!.append(script); | ||
| 54 | - | ||
| 55 | - return completer.future; | ||
| 56 | -} | ||
| 57 | - | ||
| 58 | /// Injects JS [libraries] | 45 | /// Injects JS [libraries] |
| 59 | /// | 46 | /// |
| 60 | /// Returns a [Future] that resolves when all of the `script` tags `onLoad` events trigger. | 47 | /// Returns a [Future] that resolves when all of the `script` tags `onLoad` events trigger. |
-
Please register or login to post a comment