Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where is 'dart_sdk.js'? #46376

Closed
shinayser opened this issue Jun 15, 2021 · 7 comments
Closed

Where is 'dart_sdk.js'? #46376

shinayser opened this issue Jun 15, 2021 · 7 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop.

Comments

@shinayser
Copy link

shinayser commented Jun 15, 2021

Hello everyone! I am trying to create a POC on my company showing that Dart superpowers to javascript are reall awesome.

But I am really stuck =(
This is the sample class I want to create a library for usage on javascript evironment:

class Awesome {
  bool get isAwesome => true;

  String hello() => 'Hello cruel world !';
}

Then, I run the following command on it:

dartdevc -o lib/dart_js_poc.js lib/src/awesome.dart --modules es6

I am using es6 because I want this library to run on our React server.

Here comes the JS generated by the dart compiler:

var awesome = Object.create(dart.library);
export { awesome as lib__src__awesome };
import { core, dart, dartx } from 'dart_sdk.js';
dart._checkModuleNullSafetyMode(false);
const CT = Object.create({
  _: () => (C, CT)
});
var I = ["org-dartlang-app:/lib/src/awesome.dart"];
awesome.Awesome = class Awesome extends core.Object {
  get isAwesome() {
    return true;
  }
  hello() {
    return "Hello cruel world !";
  }
};
(awesome.Awesome.new = function() {
  ;
}).prototype = awesome.Awesome.prototype;
dart.addTypeTests(awesome.Awesome);
dart.addTypeCaches(awesome.Awesome);
dart.setMethodSignature(awesome.Awesome, () => ({
  __proto__: dart.getMethods(awesome.Awesome.__proto__),
  hello: dart.fnType(core.String, [])
}));
dart.setGetterSignature(awesome.Awesome, () => ({
  __proto__: dart.getGetters(awesome.Awesome.__proto__),
  isAwesome: core.bool
}));
dart.setLibraryUri(awesome.Awesome, I[0]);
dart.trackLibraries("dart_js_poc", {
  "org-dartlang-app:/lib/src/awesome.dart": awesome
}, {
}, null);

//# sourceMappingURL=dart_js_poc.js.map

The problem is on this line: import { core, dart, dartx } from 'dart_sdk.js';. Where is this file? Without it I can't use this library.

Is this a bug on the compiler?

@mraleph
Copy link
Member

mraleph commented Jun 15, 2021

Look for this file in <dart-sdk>/lib/dev_compiler/kernel/*.

FWIW, I don't think dev compiler is intended for production usage. It is a development compiler after all, but I let @sigmundch comment on that.

@mraleph mraleph added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Jun 15, 2021
@jakemac53
Copy link
Contributor

I believe the goal (if not already done) is to remove these files and have you compile them yourself, in order to make the shipped SDK smaller.

In general the process for compiling and bootstrapping a JS app with dartdevc is undocumented, and not stable. It is recommended you use webdev to develop web apps using dartdevc.

Note that if you use the dart2js compiler (or I think now dart compile js <file>?) that produces a single file that doesn't require any extra bootstrapping.

@shinayser
Copy link
Author

Thanks for the answer folks!

Note that if you use the dart2js compiler (or I think now dart compile js <file>?) that produces a single file that doesn't require any extra bootstrapping.

The problem with dart2js is realy only one: it requireds a main dart file to work. My poc is about creating a library, thus, not having a main, entrypoint =(

Do you think that what I am trying to achieve is not possible with Dart? That is the answer I am trying to find on various websites and I can't find. Yes, there is a lot about dart and javascript but only as web apps, but for libraries.

@jakemac53
Copy link
Contributor

The short answer is Dart does not generally support creating shared libraries for the web, no.

Technically you can kind of sort of work your way around that with JS interop for some use cases, but it probably isn't a good solution for what you are trying to actually achieve.

@shinayser
Copy link
Author

shinayser commented Jun 15, 2021

Thanks a lot @jakemac53, I think I will stop trying them. But would be amazing if dart could provide a way of doing it, right?
Nowadays, flutter community is getting huge we are tryign to share those amazing dart codes we write veryday to our frontend web colleagues 😁.

@shinayser
Copy link
Author

shinayser commented Jun 15, 2021

I created another issue for that feature request: #46378

@jakemac53
Copy link
Contributor

jakemac53 commented Jun 15, 2021

This has definitely come up before but I don't have a link handy. Ultimately, my personal opinion is that Dart is just simply not a good fit for this task, and I think that is likely why it hasn't been done yet.

There are a lot of reasons it isn't a good fit, but mostly it comes down to the overhead of the Dart SDK itself, and the difficulties that you would face trying to overcome that. If you simply included the dart_sdk.js library (compiled by dartdevc), that is several megabytes in size on its own.

Generally we rely on tree shaking, minification, and other mechanisms to get rid of some of this overhead, and also when you are talking about a full app the cost tends to get amortized. But none of those mechanisms really work well when you are talking about a library, for a few reasons:

  • you can't do whole world analysis in order to tree shake (particularly the sdk libs, but also library dependencies)
  • you can't easily share dependency code across several libraries (they would at a minimum have to agree on where shared deps come from)
  • libraries tend to be small, so having a large dependency doesn't lend itself well to that purpose
  • dartdevc - the only compiler that today outputs anything that is recognizable as a normal library from other JS code - is not meant for production, it is optimized instead for debugging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop.
Projects
None yet
Development

No branches or pull requests

3 participants