dart vm works but dart2js fails

我的未来我决定 提交于 2019-12-06 09:46:29

问题


I am doing polymer dart. Everything works fine with dart vm but when I try to deploy it, it fails (compilation was okay.) When I run the built js version. It gave me the error

Uncaught TypeError: Cannot call method 'shL' of null

I tried to trace the compiled js code, and it seems like due to query an element that is not yet in the document (it is in the html file but somehow only the head of the document is loaded at that time.) Since it's the compiled version, it's really hard to trace which part went wrong. Also, there's no error in dart vm and dart2js.

Does anyone know why this is happening or I did something wrong?

PS. I think to make dart more popular, at least dart2js compiler has to be as stable as whenever the code runs fine on vm is fine in the js version. Having developer try to debug on the compiled js code is really annoying. Thanks, Yi

==UPDATE==

Here is the html file (before being built)

<html>
  <head>
    <meta charset="utf-8">
    <title>Sample app</title>
    <link rel="stylesheet" href="myHtml.css">

    <link rel="import" href="template/my-element.html">
    <script type="application/dart">export 'main.dart';</script>
    <script src="packages/browser/dart.js"></script>
  </head>
  <body>

        <my-element id="myElement" class="myElement" numOfRow="3"></my-element>

  </body>
</html>

回答1:


Usually this problem is caused by polymer expressions accessing properties which are dropped by pub builds tree-shaking. Tree-shaking retains all code that is referenced somewhere but polymer expressions are not considered for this yet. If your project works when you run build with option debug pub build --mode=debug then it's very probably that this is the cause.

If the dropped field/method is in code you control then you can solve this by just adding one of the annotations @reflectable, @observable, or @published. If its third party code (SDK or some 3rd-party library) you can import 'dart:mirrors' and annotate it with@MirrorsUsed(options)` where the options list the members tree-shaking should retain.




回答2:


I found that there were two issues in my original code. 1. I shouldn't load the main script before the body is loaded which I think it's also wrong in some of the sample codes in the dart page. 2. I think initPolymer() doesn't init the elements synchronously. Therefore, if I call a method of the element right after the initPolymer, it cannot find the method.

I fixed 1. However, I don't know how to fix 2. I tried initPolymer().run(), it doesn't work either. Please let me know if someone knows how to fix it.

Thanks, Yi



来源:https://stackoverflow.com/questions/20914791/dart-vm-works-but-dart2js-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!