This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Reland (x2) "Output .js files as ES6 modules. (flutter#52023)" #53718
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
01e806b
Reland (x2) "Output .js files as ES6 modules. (#52023)"
eyebrowsoffire 2adb472
Pass the main script url explicitly to the worker so it can load prop…
eyebrowsoffire de62389
Deal with relative URIs.
eyebrowsoffire 1ba2721
Use an absolute path for canvasKitBaseUrl in tests.
eyebrowsoffire 17ab8dc
Fix DomURL constructor.
eyebrowsoffire 1d6e74e
Merge branch 'main' into reland_es6_2
eyebrowsoffire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,12 +259,13 @@ extension CanvasKitExtension on CanvasKit { | |
); | ||
} | ||
|
||
@JS('window.CanvasKitInit') | ||
external JSAny _CanvasKitInit(CanvasKitInitOptions options); | ||
@JS() | ||
@staticInterop | ||
class CanvasKitModule {} | ||
|
||
Future<CanvasKit> CanvasKitInit(CanvasKitInitOptions options) { | ||
return js_util.promiseToFuture<CanvasKit>( | ||
_CanvasKitInit(options).toObjectShallow); | ||
extension CanvasKitModuleExtension on CanvasKitModule { | ||
@JS('default') | ||
external JSPromise<JSAny> defaultExport(CanvasKitInitOptions options); | ||
} | ||
|
||
typedef LocateFileCallback = String Function(String file, String unusedBase); | ||
|
@@ -3661,11 +3662,11 @@ String canvasKitWasmModuleUrl(String file, String canvasKitBase) => | |
/// Downloads the CanvasKit JavaScript, then calls `CanvasKitInit` to download | ||
/// and intialize the CanvasKit wasm. | ||
Future<CanvasKit> downloadCanvasKit() async { | ||
await _downloadOneOf(_canvasKitJsUrls); | ||
final CanvasKitModule canvasKitModule = await _downloadOneOf(_canvasKitJsUrls); | ||
|
||
final CanvasKit canvasKit = await CanvasKitInit(CanvasKitInitOptions( | ||
final CanvasKit canvasKit = (await canvasKitModule.defaultExport(CanvasKitInitOptions( | ||
locateFile: createLocateFileCallback(canvasKitWasmModuleUrl), | ||
)); | ||
)).toDart) as CanvasKit; | ||
|
||
if (canvasKit.ParagraphBuilder.RequiresClientICU() && !browserSupportsCanvaskitChromium) { | ||
throw Exception( | ||
|
@@ -3681,10 +3682,12 @@ Future<CanvasKit> downloadCanvasKit() async { | |
/// downloads it. | ||
/// | ||
/// If none of the URLs can be downloaded, throws an [Exception]. | ||
Future<void> _downloadOneOf(Iterable<String> urls) async { | ||
Future<CanvasKitModule> _downloadOneOf(Iterable<String> urls) async { | ||
for (final String url in urls) { | ||
if (await _downloadCanvasKitJs(url)) { | ||
return; | ||
try { | ||
return await _downloadCanvasKitJs(url); | ||
} catch (_) { | ||
continue; | ||
} | ||
} | ||
|
||
|
@@ -3694,36 +3697,15 @@ Future<void> _downloadOneOf(Iterable<String> urls) async { | |
); | ||
} | ||
|
||
String _resolveUrl(String url) { | ||
return createDomURL(url, domWindow.document.baseUri).toJSString().toDart; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could wrap the |
||
} | ||
|
||
/// Downloads the CanvasKit JavaScript file at [url]. | ||
/// | ||
/// Returns a [Future] that completes with `true` if the CanvasKit JavaScript | ||
/// file was successfully downloaded, or `false` if it failed. | ||
Future<bool> _downloadCanvasKitJs(String url) { | ||
final DomHTMLScriptElement canvasKitScript = | ||
createDomHTMLScriptElement(configuration.nonce); | ||
canvasKitScript.src = createTrustedScriptUrl(url); | ||
|
||
final Completer<bool> canvasKitLoadCompleter = Completer<bool>(); | ||
|
||
late final DomEventListener loadCallback; | ||
late final DomEventListener errorCallback; | ||
|
||
void loadEventHandler(DomEvent _) { | ||
canvasKitScript.remove(); | ||
canvasKitLoadCompleter.complete(true); | ||
} | ||
void errorEventHandler(DomEvent errorEvent) { | ||
canvasKitScript.remove(); | ||
canvasKitLoadCompleter.complete(false); | ||
} | ||
|
||
loadCallback = createDomEventListener(loadEventHandler); | ||
errorCallback = createDomEventListener(errorEventHandler); | ||
|
||
canvasKitScript.addEventListener('load', loadCallback); | ||
canvasKitScript.addEventListener('error', errorCallback); | ||
|
||
domDocument.head!.appendChild(canvasKitScript); | ||
|
||
return canvasKitLoadCompleter.future; | ||
Future<CanvasKitModule> _downloadCanvasKitJs(String url) async { | ||
final JSAny scriptUrl = createTrustedScriptUrl(_resolveUrl(url)); | ||
return (await importModule(scriptUrl).toDart) as CanvasKitModule; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(The gstatic.com/flutter-canvaskit URL should come from buildConfig as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, I agree. Although I'm not going to tackle that with this change (and would require other changes in the framework first).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100% agreed