-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
JS interop support for ES6 modules #53783
Comments
Related request regarding idea 2: #52852 |
FYI - I've just landed a member to address the second request. Since the result is a promise that resolves to an object, you can use an extension type/ |
In case its useful, I wanted to make a note on usability issues found using a real example use case when implementing a @JS('fs')
library fs;
import 'dart:js_interop';
// getter used for importing the `fs` module
@JS()
Future<FS> get fs async => FS._(await importModule('fs').toDart);
// defines all of the members on `fs`
extension type FS._(JSObject _) implements JSObject {
external JSAny readFileSync(
JSAny file, [
JSAny? options,
]);
...
}
// USAGE (i.e. users of `package:node_interop`
Future<void> main() async {
final thisDir = '/Users/pattobrien/dev/pattobrien/dart3_node_interop';
final files = (await fs).readdirSync(thisDir.toJS);
for (final file in files.toDart) {
console.log(file);
}
} A couple things to point out:
|
Great feedback, I agree with all your points! I can imagine an annotation on the library that would signal to the compiler to import a module as a namespace that can then be used in lowering the interop calls. That way, top-level interop declarations would correspond to top-levels in that module, linking the Dart library to the JS module. We don't have support for interop to tell any of the web compilers to statically import a module, but it might be possible without too much effort - TBD. |
That would be amazing! Ideally inferring js semantics from how users are already using Dart would be the way to go. If I import a dart The translation from dart code to js code would then look something like the following:
I've also opened a related but separate issue regarding accessing the top-level |
It would be much harder to convert Better support for ESM is likely an intersection between interop and the compilers. We should create a meta issue linking all the relevant issues together here. |
Some sort of first-class support for ES modules in JS interop would be very useful for both JS backends and dart2wasm. Currently, there doesn't appear to be a way to do dynamic or static importing of ES modules through JS interop, so here are a few ideas:
@staticImport()
annotation with a const string url parameter, which transforms into a static import of an ES module:dart:js_interop
, expose a builtinFuture<ModuleExports> import<ModuleExports implements JSAny>(String url)
function for dynamic imports. Currently trying to callimport
via static interop doesn't actually work because it's not a real function, it's a browser builtin.The text was updated successfully, but these errors were encountered: