Skip to content

Commit

Permalink
Use flutter.js bootstrapping (#2960)
Browse files Browse the repository at this point in the history
* Revert "Revert "Use flutter.js bootstrapping (#2792)" (#2959)"

This reverts commit 9183b0b.

* Loosen CORS header for canvaskit.wasm
  • Loading branch information
johnpryan committed May 9, 2024
1 parent 9183b0b commit 578bd4e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ build/
# Or the files created by dart2js.
*.dart.js
*.js.deps
*.js.map

# Python generated files
*.pyc
Expand Down
7 changes: 5 additions & 2 deletions pkgs/dart_services/lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import 'main.dart' as entrypoint;
Future<void> main() async {
registerPlugins(webPluginRegistrar);
await ui_web.bootstrapEngine();
entrypoint.main();
await ui_web.bootstrapEngine(
runApp: () {
return entrypoint.main();
},
);
}
''';

Expand Down
9 changes: 9 additions & 0 deletions pkgs/dartpad_ui/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@
}
]
},
{
"source": "/canvaskit/chromium/canvaskit.wasm",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
},
{
"source": "**",
"headers": [
Expand Down
4 changes: 2 additions & 2 deletions pkgs/dartpad_ui/web/frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
margin-top: 0;
}
</style>
<script src="frame.js"></script>
<script src="require.js"></script>
<script id="compiled-script"></script>
<script src="flutter.js"></script>
<script src="frame.js"></script>
</head>

<body>
Expand Down
20 changes: 13 additions & 7 deletions pkgs/dartpad_ui/web/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ function messageHandler(e) {
var obj = e.data;

if (obj.command === 'execute') {
// TODO: Switch to using engineInitializer.initializeEngine(config). See
// https://docs.flutter.dev/development/platform-integration/web/initialization.
window.flutterConfiguration = {
canvasKitBaseUrl: obj.canvasKitBaseUrl
};

replaceJavaScript(obj.js);
runFlutterApp(obj.js);
}
};

function runFlutterApp(compiledScript) {
var blob = new Blob([compiledScript], {type: 'text/javascript'});
var url = URL.createObjectURL(blob);
_flutter.loader.loadEntrypoint({
entrypointUrl: url,
onEntrypointLoaded: async function(engineInitializer) {
let appRunner = await engineInitializer.initializeEngine();
appRunner.runApp();
}
});
}

window.addEventListener('load', function () {
window.addEventListener('message', messageHandler, false);
parent.postMessage({ 'sender': 'frame', 'type': 'ready' }, '*');
Expand Down

0 comments on commit 578bd4e

Please sign in to comment.