pulyaevskiy / firebase-functions-interop

Firebase Functions Interop Library for Dart.
BSD 3-Clause "New" or "Revised" License
191 stars 52 forks source link

Using a JavaScript library #59

Closed gazialankus closed 4 years ago

gazialankus commented 4 years ago

How can we call JavaScript code from our Dart code? I can't see how the js code can be included here.

In Web, you need to already have the js code included in the js side and you tell Dart to call that. So, we need access to the js side. However, firebase-functions-interop way is to point node.js to the generated build/node/index.dart.js file.

I guess I could just edit the generated js file at every build, but I'm hoping for a better approach.

Thanks!

gazialankus commented 4 years ago

Ok I figured it out. There were very few examples with require() from here. Here's the jist of it in case someone else needs it:

sendmail.js

exports.sendmail = function sendmail() {
    console.log("SENT!!!");
}

sendmail.dart

@JS()
library sendmail;

import 'package:js/js.dart';

@JS()
@anonymous
abstract class Sendmail {
  external void sendmail();
}

usage

import 'sendmail.dart';

void main() {
  final sender = require("./sendmail.js");
  sender.sendmail();
}