1

I'm using dart2js to compile dart to JavaScript on my Ubuntu server.

I'm able to use simple commands like print() but I can't seem to use the DOM. The code below is returning null.

Is there something else I need to make imports work besides including the compiled out.js?

import 'dart:html';

void main() {
    print(document.querySelector('body'));
}

HTML:

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" href="/css/f4cacce_main_1.css" />
    <script src="/js/51de0d2_main_1.js"></script>
</head>
<body>
</body>
</html>
Tek
  • 2,888
  • 5
  • 45
  • 73

1 Answers1

0

If you import out.js manually then the issue is probably in your HTML file. Please add it to your question.

You should use pub build instead of dart2js (pub build uses dart2js internally). pub build takes care of adding the script tag for the generated JavaScript.

Edit

Your HTML should look like this before you run pub build

<!DOCTYPE HTML>
<html>
<head>
  <link rel="stylesheet" href="/css/f4cacce_main_1.css" />
</head>
<body>
  <script type="application/dart" src="main1.dart"></script>
  <script src="packages/browser/dart.js"></script>
</body>
</html>

(assuming main1.dart is the file containing your Dart code).

You can open this in Dartium without building to JavaScript.
You can also serve this using pub serve and then open it from Dartium or non-Dart capable browsers and pub serve always returns the correct output (Dart or JS)

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Edited question with HTML. – Tek Jan 14 '15 at 14:21
  • This code will be run from a server, so no display means no Dartium. Plus it will be used for other people to use so I need to compile my code to javascript. – Tek Jan 14 '15 at 14:50
  • What do you mean by "This code will be run from a server"? The browser loads the code from a server and executes it? This is how it's usually done with Dart client apps. `pub build` does compile it to JavaScript. What do you mean by "will be used for other people to use"? Should some other Dart or JavaScript app load the built JavaScript (built from your code)? – Günter Zöchbauer Jan 14 '15 at 14:52
  • Right, but the main idea is that I have to compile it on the server. I'll be using this program for every day browsers so I can't use the method you described above – Tek Jan 14 '15 at 14:54
  • What do you mean by "the idea is that I have to compile it on the server"? `pub serve` is just a development tool so you don't need to call `pub build` during development (it's done on-the-fly). Usually you need to compile Dart code to JavaScript before you serve it to the client. This is what `pub build` does. – Günter Zöchbauer Jan 14 '15 at 14:57
  • How does pub build affect compiled javascript compared to dart2js alone? – Tek Jan 14 '15 at 14:59
  • `pub build` builds all files in the project and it applies transformers if there are any specified in the `pubspec.yaml` file. You can also add `dart2js` configuration settings in `pubspec.yaml` which are taken into account by `pub build` (passed to `dart2js`). `pub build` internally calls `dart2js`. – Günter Zöchbauer Jan 14 '15 at 15:01
  • Some examples how to configure `dart2js` in `pubspec.yaml` http://stackoverflow.com/a/21339347/217408, http://stackoverflow.com/a/23257605/217408 – Günter Zöchbauer Jan 14 '15 at 15:05
  • Turns out `dart2js` was compiling dart code to javascript just fine. I was missing `dart.js` that comes in `build/web/packages/browser.js`. That made it work on all browsers and the code above was able to access the DOM and worked as expected. Mind adding that to your answer so I can accept? – Tek Jan 16 '15 at 04:09