Add client-side entry point script.
Problem
Currently there is no option to run code on the client before mounting a @client component, since the client-side entrypoint is transparently auto-generated. When using multiple client components, there is no single global main function.
Additionally, developers cannot wrap their client components in some other component, like a ProviderScope or similar. This makes sharing state between client components impossible without using global state.
Proposed Solution
Allow the definition of a client-side entrypoint file, e.g. main.client.dart, where the user manually calls runApp() on the client to start the hydration. Similar to defaultJasprOptions for the server, there would have to be a generated configuration or component that encapsulates the @client component registration. An API could look like this:
// This file is 'lib/main.client.dart'
import 'package:jaspr/browser.dart';
// Generated client options
import 'client_options.g.dart';
void main() {
// Whatever code here.
{ }
runApp(
// Wrap in arbitrary component
MyWrappingComponent(
child: ClientApp( // This would be a new core framework component
options: defaultClientOptions, // This is imported from the generated file.
),
),
);
}
This would replace the auto-generated web/main.clients.dart file.
When implementing this feature, the natural thing would also be to align the definition of server entrypoints to use a similar schema:
lib/<...>.server.dartas the filename conventionlib/server_options.g.dartas the generated options file.
This would also allow the framework and tooling to auto-detect entrypoint files without needing to configure them in pubspec.yaml.
This all makes sense to me and seems like a good idea.
Would manual hydration also be able to go in this file?
This should also work for manual hydration and even client mode. But it's not meant to be mandatory in these cases.
I've opted for the following:
- main.server.dart (prev main.dart)
- main.server.g.dart (prev jaspr_options.dart)
- main.client.dart (new)
- main.client.g.dart (new)
This keeps the files in nice order and allows for file nesting inside the editor.
I'm also planning to rename ' package:jaspr/browser.dart' to 'package:jaspr/client.dart' to keep everything consistent.