How can I generate code to export functions for node in Dart?

Issue

(I failed at getting this working in dart2js so I’m trying in dart dev compiler; but I’d happily take an answer for dart2j!).

If I have test.dart:

void activate() {
  print("activating...");
}

and run dartdevc --modules node -o test.js test.dart the output is:

(function() {
  'use strict';
  const dart_sdk = require('dart_sdk');
  const core = dart_sdk.core;
  const dart = dart_sdk.dart;
  const dartx = dart_sdk.dartx;
  const __test = Object.create(null);
  let VoidTovoid = () => (VoidTovoid = dart.constFn(dart.definiteFunctionType(dart.void, [])))();
  __test.activate = function() {
    core.print("activating...");
  };
  dart.fn(__test.activate, VoidTovoid());
  // Exports:
  exports.__test = __test;
})();

This means my function is exported as __test.activate but what I need is for it just to be activate.

How can I control this? The JS I’m aiming for the equivilent of this:

exports.activate = function() { core.print("activating"); }

Solution

This isn’t currently possible but I worked around it with a wrapper:

var extension = require('./dartvsjs/extension.js');

exports.activate = extension.__lib__extension.activate;
exports.deactivate = extension.__lib__extension.deactivate;

Answered By – Danny Tuppeny

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *