diff --git a/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart b/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart index 4aa608f4e23c9..bd626064b580c 100644 --- a/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart +++ b/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart @@ -3501,39 +3501,6 @@ extension SkPartialImageInfoExtension on SkPartialImageInfo { double get width => _width.toDart; } -/// Helper interop methods for [patchCanvasKitModule]. -@JS() -external set __flutterWebCachedModule(JSAny? module); -set _flutterWebCachedModule(Object? module) => - __flutterWebCachedModule = module?.toJSAnyShallow; - -@JS() -external JSAny? get __flutterWebCachedModule; -Object? get _flutterWebCachedModule => - __flutterWebCachedModule?.toObjectShallow; - -@JS() -external set __flutterWebCachedExports(JSAny? exports); -set _flutterWebCachedExports(Object? exports) => - __flutterWebCachedExports = exports?.toJSAnyShallow; - -@JS() -external JSAny? get __flutterWebCachedExports; -Object? get _flutterWebCachedExports => - __flutterWebCachedExports?.toObjectShallow; - -@JS('Object') -external JSAny get _objectConstructor; -Object get objectConstructor => _objectConstructor.toObjectShallow; - -@JS('exports') -external JSAny? get _exports; -Object? get exports => _exports?.toObjectShallow; - -@JS('module') -external JSAny? get _module; -Object? get module => _module?.toObjectShallow; - @JS('window.flutterCanvasKit.RuntimeEffect') @anonymous @staticInterop @@ -3558,65 +3525,6 @@ extension SkSkRuntimeEffectExtension on SkRuntimeEffect { children.toJSAnyShallow); } -/// Monkey-patch the top-level `module` and `exports` objects so that -/// CanvasKit doesn't attempt to register itself as an anonymous module. -/// -/// The idea behind making these fake `exports` and `module` objects is -/// that `canvaskit.js` contains the following lines of code: -/// -/// if (typeof exports === 'object' && typeof module === 'object') -/// module.exports = CanvasKitInit; -/// else if (typeof define === 'function' && define['amd']) -/// define([], function() { return CanvasKitInit; }); -/// -/// We need to avoid hitting the case where CanvasKit defines an anonymous -/// module, since this breaks RequireJS, which DDC and some plugins use. -/// Temporarily removing the `define` function won't work because RequireJS -/// could load in between this code running and the CanvasKit code running. -/// Also, we cannot monkey-patch the `define` function because it is -/// non-configurable (it is a top-level 'var'). -// TODO(hterkelsen): Rather than this monkey-patch hack, we should -// build CanvasKit ourselves. See: -// https://github.com/flutter/flutter/issues/52588 -void patchCanvasKitModule(DomHTMLScriptElement canvasKitScript) { - // First check if `exports` and `module` are already defined. If so, then - // CommonJS is being used, and we shouldn't have any problems. - if (exports == null) { - final Object? exportsAccessor = js_util.jsify({ - 'get': js_util.allowInterop(() { - if (domDocument.currentScript == canvasKitScript) { - return js_util.callConstructor(objectConstructor, []); - } else { - return _flutterWebCachedExports; - } - }), - 'set': js_util.allowInterop((dynamic value) { - _flutterWebCachedExports = value; - }), - 'configurable': true, - }); - js_util.callMethod(objectConstructor, - 'defineProperty', [domWindow, 'exports', exportsAccessor]); - } - if (module == null) { - final Object? moduleAccessor = js_util.jsify({ - 'get': js_util.allowInterop(() { - if (domDocument.currentScript == canvasKitScript) { - return js_util.callConstructor(objectConstructor, []); - } else { - return _flutterWebCachedModule; - } - }), - 'set': js_util.allowInterop((dynamic value) { - _flutterWebCachedModule = value; - }), - 'configurable': true, - }); - js_util.callMethod(objectConstructor, - 'defineProperty', [domWindow, 'module', moduleAccessor]); - } -} - const String _kFullCanvasKitJsFileName = 'canvaskit.js'; const String _kChromiumCanvasKitJsFileName = 'chromium/canvaskit.js'; @@ -3711,7 +3619,6 @@ Future _downloadCanvasKitJs(String url) { canvasKitScript.addEventListener('load', loadCallback); canvasKitScript.addEventListener('error', errorCallback); - patchCanvasKitModule(canvasKitScript); domDocument.head!.appendChild(canvasKitScript); return canvasKitLoadCompleter.future; diff --git a/lib/web_ui/lib/src/engine/dom.dart b/lib/web_ui/lib/src/engine/dom.dart index 26ed2f9674025..583ab2736fe0d 100644 --- a/lib/web_ui/lib/src/engine/dom.dart +++ b/lib/web_ui/lib/src/engine/dom.dart @@ -63,6 +63,10 @@ extension JSAnyToObjectExtension on JSAny { Object get toObjectDeep => js_util.dartify(this)!; } +@JS('Object') +external JSAny get _objectConstructor; +Object get objectConstructor => _objectConstructor.toObjectShallow; + @JS() @staticInterop class DomWindow extends DomEventTarget {} diff --git a/lib/web_ui/test/canvaskit/initialization/does_not_mock_module_exports_test.dart b/lib/web_ui/test/canvaskit/initialization/does_not_mock_module_exports_test.dart new file mode 100644 index 0000000000000..c06e3877db283 --- /dev/null +++ b/lib/web_ui/test/canvaskit/initialization/does_not_mock_module_exports_test.dart @@ -0,0 +1,39 @@ +// Copyright 2013 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 'package:js/js_util.dart' as js_util; +import 'package:test/bootstrap/browser.dart'; +import 'package:test/test.dart'; +import 'package:ui/src/engine.dart'; + +void main() { + internalBootstrapBrowserTest(() => testMain); +} + +void testMain() { + group('initializeEngineServices', () { + test('does not mock module loaders', () async { + // Initialize CanvasKit... + await initializeEngineServices(); + + // CanvasKitInit should be defined... + expect( + js_util.hasProperty(domWindow, 'CanvasKitInit'), + isTrue, + reason: 'CanvasKitInit should be defined on Window', + ); + + // window.exports and window.module should be undefined! + expect( + js_util.hasProperty(domWindow, 'exports'), + isFalse, + reason: '`window.exports` should not be defined.', + ); + expect( + js_util.hasProperty(domWindow, 'module'), + isFalse, + reason: '`window.module` should not be defined.', + ); + }); + }); +} diff --git a/lib/web_ui/test/canvaskit/initialization/initialization_services_vs_ui_test.dart b/lib/web_ui/test/canvaskit/initialization/services_vs_ui_test.dart similarity index 100% rename from lib/web_ui/test/canvaskit/initialization/initialization_services_vs_ui_test.dart rename to lib/web_ui/test/canvaskit/initialization/services_vs_ui_test.dart diff --git a/lib/web_ui/test/canvaskit/initialization/initialization_stores_config_test.dart b/lib/web_ui/test/canvaskit/initialization/stores_config_test.dart similarity index 100% rename from lib/web_ui/test/canvaskit/initialization/initialization_stores_config_test.dart rename to lib/web_ui/test/canvaskit/initialization/stores_config_test.dart