dev_compiler icon indicating copy to clipboard operation
dev_compiler copied to clipboard

Support exporting functions directly at the top level (exports.blah = function() { /* ... */ })

Open DanTup opened this issue 9 years ago • 4 comments

Sorry if this is a noob question, and I appreciate this isn't ready for production, but I can't get things working with dart2js so figured I'd have a go with this...

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"); }

DanTup avatar Jul 29 '16 16:07 DanTup

Right now we don't have anything that would control that, but it's a good request. Definitely it should be possible to export something at module level for interop purposes. CC @jacob314 who may be able to take a look.

jmesserly avatar Jul 29 '16 16:07 jmesserly

p.s. good catch & thanks for filing this!

jmesserly avatar Jul 29 '16 16:07 jmesserly

A workaround until we fix this, might be to write a little wrapper JS module that re-exports the API you want.

jmesserly avatar Jul 29 '16 16:07 jmesserly

Thanks; I don't know how common this sort of thing is but it's a requirement for Visual Studio Code extensions:

Note: An extension must export an activate() function from its main module and it will be invoked only once by VS Code when any of the specified activation events is emitted.

Though it does also seem reasonable to want full control over the exported object.

I'll create a wrapper for now and see how far I can get. Thanks!

DanTup avatar Jul 29 '16 16:07 DanTup