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
Output .js files as ES6 modules. #52023
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2adf221
Output .js files as ES6 modules.
eyebrowsoffire 8d2232f
Change loading logic to use dynamic imports instead.
eyebrowsoffire 8e21445
`CanvasKitInit` isn't in the global scope anymore.
eyebrowsoffire 691ca5b
Merge branch 'main' into wasm_es6
eyebrowsoffire fb88f55
Fix buildroot hash.
eyebrowsoffire e68289e
Merge branch 'main' into wasm_es6
eyebrowsoffire b6e339d
Use trusted script URLs when dynamically importing.
eyebrowsoffire 911e738
Fix createTrustedScriptUrl.
eyebrowsoffire 777d955
Remove unintentionally added newline.
eyebrowsoffire 312e676
Change as per David's comment.
eyebrowsoffire 32aa1d3
Merge branch 'main' into wasm_es6
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
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') | ||
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. 🔪 💔 |
||
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; | ||
} | ||
} | ||
|
||
|
@@ -3698,32 +3701,7 @@ Future<void> _downloadOneOf(Iterable<String> urls) async { | |
/// | ||
/// 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(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
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.
This is beautiful!