dart-json-mapper icon indicating copy to clipboard operation
dart-json-mapper copied to clipboard

Can`t use JsonMapper in production build for web platform

Open fidlip opened this issue 5 years ago • 20 comments

Given this code

import 'package:dart_json_mapper/json_mapper.dart';
import 'package:dart_json_mapper/annotations.dart';

import 'main.reflectable.dart';

@jsonSerializable
class TridaA {
  TridaA() {
    print("TridaA constructed");
  }
  String prop;
}

void main() async {
  // inicializace reflektoru
  initializeReflectable();

  print(".main");
  jsonSerializable.annotatedClasses.forEach((cls) {
    print("Annotated class ${cls.qualifiedName}");
  });

  print("allClasses ${jsonSerializable.annotatedClasses}");
  var mirror = jsonSerializable.annotatedClasses.firstWhere((cm) {
    print("   class ${cm.simpleName} ~ ${cm.reflectedType.toString()}");
    return cm.simpleName == "TridaA";
  });

  print("Instance by reflection: ${mirror.newInstance("", [], {})}");

  var map = Map<String, dynamic>.from({"b": Map<String, dynamic>.from({"prop": "test"})});

  var instance = JsonMapper.instance
    .deserializeObject(map, TridaA);

  print("Instance by JsonMapper: ${instance}");

}

Everything is OK if it is built with dartdevc, but when compiled with dart2js by: webdev serve web:43751 --hot-reload --release with minification, it raise error:

Uncaught It seems your class 'minified:a6' has not been annotated with @jsonSerializable at Object.c (localhost:43751/main.dart.js:2719:3) at bX.a2 (localhost:43751/main.dart.js:5437:20) at bX.bB (localhost:43751/main.dart.js:5442:30) at localhost:43751/main.dart.js:6122:49 at ef.a (localhost:43751/main.dart.js:3678:72) at ef.$2 (localhost:43751/main.dart.js:3804:23) at Object.jI (localhost:43751/main.dart.js:3659:3) at Object.fD (localhost:43751/main.dart.js:6124:10) at localhost:43751/main.dart.js:6532:8 at localhost:43751/main.dart.js:6527:55

fidlip avatar Mar 13 '19 23:03 fidlip

@fidlip Thank you for the fix. It's absolutely acceptable.

k-paxian avatar Mar 15 '19 21:03 k-paxian

Thanks a lot!

fidlip avatar Mar 16 '19 20:03 fidlip

Hi @k-paxian I have the same problem when using generic types (https://github.com/k-paxian/dart-json-mapper/issues/101#issuecomment-1007344542) flutter web image

masoodmrx avatar Jan 15 '22 09:01 masoodmrx

Hello, please solve this problem I need it

masoodmrx avatar Jan 16 '22 08:01 masoodmrx

Hello @k-paxian , Hope you are fine :).

When i went into web production , the console output this one too : Uncaught It seems your class 'minified:x<dynamic>' has not been annotated with @jsonSerializable. Then can't load and use the library .

Do you get any ideas ?

jeansebastienZ avatar Jan 23 '23 18:01 jeansebastienZ

@jeansebastienZ sure, this issue needs some more love & attention. I'd like to mitigate it at least and this time I'm struggling to focus myself on other topics. (including latest tech layoffs, I couldn't donate extra more personal time on OSS, occasion by occasion now, small issues etc.)

k-paxian avatar Jan 23 '23 19:01 k-paxian

Re @k-paxian , ✌🏽,

I tried avoiding minification with --profile however it still produce the error ( Uncaught It seems your class 'JSArray<dynamic>' has not been annotated with @jsonSerializable ) . --profile mode still doing tree-shaking and there is no way to deactivate it right now.

As it is not possible to build web in Debug , right now it's not possible for us to use JSON MAPPER for web. If you get any solution you are welcome 👍🏾

jeansebastienZ avatar Jan 24 '23 10:01 jeansebastienZ

Hello @k-paxian, Can you send me a mail please 😉 ?

jeansebastienZ avatar Jan 27 '23 11:01 jeansebastienZ

@jeansebastienZ please try the latest version 2.2.9 and give more feedback

k-paxian avatar Jan 29 '23 17:01 k-paxian

Hello @k-paxian ,

First thanks you for digging on this problem ✌🏽, as my team need this feature for going in production mode.

I tried with the last version : Capture d’écran 2023-01-29 à 19 40 15 I still get this in release mode ( by the way i did re-generate the main.mapper.g to be sure ) : Capture d’écran 2023-01-29 à 20 09 19

jeansebastienZ avatar Jan 29 '23 19:01 jeansebastienZ

Minification is a separate issue, the goal of the release was green unit tests first.

k-paxian avatar Jan 29 '23 19:01 k-paxian

I need this web platform support myself, particularly for the dart 3.0 capable to build web components for easy embedding. So eventually this issue would be resolved, but I cannot promise any timelines :shrug:

k-paxian avatar Jan 29 '23 19:01 k-paxian

Re @k-paxian ,

Thanks you for the info ! I've just ran it in --profile mode and uploaded to a repository and it is working well ! Thanks you for your constant works on this repository .

jeansebastienZ avatar Jan 29 '23 20:01 jeansebastienZ

Hi @k-paxian I used --profile mode but it has error like below, could you tell me how to fix?

image

likeconan avatar Jan 30 '23 03:01 likeconan

Hello @likeconan , Did you update the version , clean your cache and regenerate the main.mapper.g ? Moreover are your sure every mdls you use are effectively used on a adapter before running your app ?

jeansebastienZ avatar Jan 30 '23 09:01 jeansebastienZ

@jeansebastienZ Hi, I used dart_json_mapper with 2.2.9 version. And finally got the reason, it's because I uploaded into aws s3 and using cloudfront to host. When using s3 static website, everything works fine

likeconan avatar Feb 01 '23 13:02 likeconan

Thank you for the workaround.

I'd also be interested in a fix for the release mode, since now my JavaScript File weighs around 6Mb which takes quite long to load. Thank you!

ma-pe avatar Apr 16 '23 17:04 ma-pe

I'd be interested in taking a look into it. Yet I have no idea where to start, since there is no explicit web code. @k-paxian do you have a hint for me? Thank you :)

ma-pe avatar Apr 19 '23 20:04 ma-pe

You can start from https://pub.dev/packages/build_web_compilers And making a failing unit test, after minification process.

k-paxian avatar Apr 19 '23 21:04 k-paxian

I tried. But actually don't have a clue what I am doing 😬

ma-pe avatar Apr 27 '23 17:04 ma-pe