schultek / jaspr

Modern web framework for building websites in Dart. Supports SPAs, SSR and SSG.
https://jasprpad.schultek.de
MIT License
1.23k stars 76 forks source link

Dart Library 'dart:js' is not allowed on this platform #149

Closed cybertheory closed 8 months ago

cybertheory commented 9 months ago

Description

Trying to interoperate with JS files in order to get Material Web Components working with Caspr: https://github.com/cybertheory/caspr/tree/main/packages/caspr-material

Screenshot 2024-01-07 at 4 19 42 PM

Steps To Reproduce

  1. Activate jaspr through dart pub global activate jaspr_cli
  2. Create a new project with jaspr create -t classic my_app
  3. Run dart pub add caspr-material
  4. In lib\app.dart replace the code with
import 'package:caspr_material/components/buttons/button.dart';
import 'package:jaspr/jaspr.dart';

class App extends StatelessComponent {
  @override
  Iterable<Component> build(BuildContext context) sync* {
    yield FilledButton("hi");
  }
}
  1. Run jaspr serve
  2. See error

Expected Behavior

It should run and show a MDC button. If jaspr targets web it should allow for js interop?

Additional Context Caspr extends Jaspr's component library. So this is why I am posting an issue here.

schultek commented 9 months ago

I think this error comes from the server. When using server-side-rendering your component is also executed on the server and there dart:js is not available. You can fix this by using darts conditional imports: https://dart.dev/guides/libraries/create-packages#conditionally-importing-and-exporting-library-files

There is also https://docs.page/schultek/jaspr/advanced/imports to make working with them a bit easier.

schultek commented 9 months ago

Also I have used mdc also in jaspr_pad, so check out e.g. https://github.com/schultek/jaspr/blob/main/apps/jaspr_pad/lib/components/elements/button.dart and related files on how I deal with this there.

schultek commented 9 months ago

@cybertheory Can this be closed then?