5

The Dart app runs fine in Dartium, but I would like to convert it to JS.

C:\dart-sdk\bin\dart2js --out=test.js main.dart generates JS from:

import 'dart:html';

void main() {
  querySelector('#myid').text = 'Wake up, sleepy head!';
}

Then I add it to the <head>:

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript" src="test.js"></script>
</head>
<body>
  <p id="myid">hello</p>
</body>
</html>

But when I run it in Firefox, it doesn't work. It shows only "Hello" on the page. What am I doing wrong?

1 Answer 1

5

dart2js is usually not the straight-forward way to generated JS from Dart. Normally you just run pub build in your project directory. Ensure you have your pubspec.yaml configured properly csp: true (for Chrome apps Using Polymer in a Dart Chrome App, Chrome App CSP violation after Dart/Polymer transform/compile to JavaScript)

Sign up to request clarification or add additional context in comments.

9 Comments

How to use pub on Windows? Should I add Dart SDK folder to PATH? And then run pub in myAppFolder? How I can generate empty pubspec.yaml?
Yup, add dart-sdk/bin to the path. Also DartEditor contains context menus, I think on pubspec.yaml or in tools menu (It's a while I used it).
Just open your pubspec.yaml and on the right there is run pub build.
And what next? Which js file I should include?
If you have an xxx.html page (index.html or whatever name you choose) with a Dart script tag in web you get the same xxx.html output to build/web/xxx.html with the JS script tags already properly set.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.