Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use flutter.js bootstrapping #2792

Merged
merged 12 commits into from
May 9, 2024
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can even drop this bit. This is wiring up the default behavior I believe.

Right @ditman ?

let appRunner = await engineInitializer.initializeEngine();
appRunner.runApp();
}
});
}

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