How can I output code that can be loaded as a node module?

294 views
Skip to first unread message

Danny Tuppeny

unread,
Jul 29, 2016, 8:10:33 AM7/29/16
to Dart Misc
Is there any way (today, either using dart2js or dev compiler) to write Dart that would effectively output this?


var vscode = require('vscode');

function activate(context) {
}

function deactivate() {
}

exports
.activate = activate;
exports
.deactivate = deactivate;

The "vscode" variable can be typed as Dynamic, but I need to get a reference to it and I need to set those two functions against the exports object.

I've been trying for the last few hours without much success (the output of dart2js doesn't make it easier either, and I can't even get the devcompiler to output anything other than a fairly empty stub function).

Here's what I've got so far (I don't know if this is right; like I say, hard to read dart2js output) but I need the output to be loadable as a node js module.

@JS("extension")
library extension
;

import "dart:js";
import "package:js/js.dart";

main
() {
  context
['exports'].activate = activate;
  context
['exports'].deactivate = deactivate;
}

activate
(dynamic context) {
   
print('Congratulations, your extension "dartvs-code" is now active!');
   
//var disposable = vscode.commands.registerCommand('extension.sayHello', function () {
   
//    vscode.window.showInformationMessage('Hello World!');
   
//});
   
//context.subscriptions.push(disposable);
}

deactivate
() {
}

Danny Tuppeny

unread,
Jul 29, 2016, 8:58:48 AM7/29/16
to Dart Misc
I managed to get further with this code:

@JS()

library extension
;

import "dart:js";
import "package:js/js.dart";

dynamic vscode;

@JS()
external
require(String module);

main
() {

  vscode
= require('vscode');


 
print(context);

  context
['exports'].activate = activate;
  context
['exports'].deactivate = deactivate;
}

activate
(dynamic context) {
   
print('Congratulations, your extension "dartvs-code" is now active!');

   
var disposable = vscode.commands.registerCommand('extension.sayHello', () {
        vscode
.window.showInformationMessage('Hello World!');
   
});
    context
.subscriptions.push(disposable);
}

deactivate
() {
}

This gave an error (self is undefined or something)... Managed to get slightly further by pre-pending the contents of the JS file in the node_preamble package to my dart2js output. Now I'm getting this:

Loading development extension at m:/Coding/TestStuff/dartvs-code
Activating extension `DanTup.dartvs-code` failed: Cannot read property 'set$activate' of undefined.
Activating extension `DanTup.dartvs-code` failed: Cannot read property 'set$activate' of undefined
Here is the error stack: TypeError: Cannot read property 'set$activate' of undefined
at _IsolateContext.dart._IsolateContext.eval$1 (m:\Coding\TestStuff\dartvs-code\dartvsjs\extension.js:1342:25)

So I'm guessing something is up with context['exports'] though I'm not sure how to fix it. I tried this:

@JS()
external
dynamic exports;

However that seems to crash the analyzer (I guess it's a bug to crash in this way, I'll file at GH):

PS M:\Coding\TestStuff\dartvs-code\dartvs> dartanalyzer . --strong
Analyzing [.]...
Unhandled exception:
AnalysisException: Cannot compute DART_ERRORS for M:\Coding\TestStuff\dartvs-code\dartvs\lib\extension.dart
Caused by Unexpected exception while performing VerifyUnitTask for source M:\Coding\TestStuff\dartvs-code\dartvs\lib\extension.dart
#0      AnalysisTask._safelyPerform (package:analyzer/task/model.dart:350)
#1      AnalysisTask.perform (package:analyzer/task/model.dart:238)
#2      AnalysisDriver.performWorkItem (package:analyzer/src/task/driver.dart:284)
#3      AnalysisDriver.computeResult (package:analyzer/src/task/driver.dart:109)
#4      AnalysisContextImpl.computeResult (package:analyzer/src/context/context.dart:703)
#5      AnalysisContextImpl.computeErrors (package:analyzer/src/context/context.dart:651)

Bob Nystrom

unread,
Jul 29, 2016, 12:18:29 PM7/29/16
to General Dart Discussion, John Messerly
+jmesserly

I don't know the details very well, but, yes, you can tell dev_compiler to output a node module and then use JS interop to talk to native node libraries. I think the command line flag to pass to dev_compiler is --modules=node.

Cheers!

– bob


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Samuel Rawlins

unread,
Jul 29, 2016, 12:21:32 PM7/29/16
to mi...@dartlang.org, John Messerly

Danny Tuppeny

unread,
Jul 29, 2016, 12:21:53 PM7/29/16
to General Dart Discussion, John Messerly
I've actually been trying with the dev compiler for a little while after posting this :)

I hit a differnt issue:


However that's more specific to what I need and John Messerly suggested a small wrapper as a workaround. Giving it a go, though I really have no idea how complete/stable the dev compiler is, so I don't know if going down this route is a good path (I'm going to be relying on calling plain JS objects heavily! ...)

Danny Tuppeny

unread,
Jul 29, 2016, 12:31:28 PM7/29/16
to Dart Misc, jmes...@google.com
The first line of generated code is:

dart_library.library(...

dart_library is undefined... am I supposed to have a dev_compiler build of the SDK (or the bits I use) in my output? I tried passing --dart-sdk and a path but that didn't change anything (though I suspect it already knew where the SDK was since it's running from it!).

John Messerly

unread,
Jul 29, 2016, 12:47:29 PM7/29/16
to Danny Tuppeny, Dart Misc
Dart SDK is at https://github.com/dart-lang/dev_compiler/blob/master/lib/runtime/dart_sdk.js

dart_library is a little fake module system for the browser (because browsers don't support ES6 module loading yet). You shouldn't need it if you're compiling with ES6 or node modules. But if you did want it, it's in dart_library.js right next to the SDK file I linked above.

Vijay Menon

unread,
Jul 29, 2016, 12:58:37 PM7/29/16
to General Dart Discussion, Danny Tuppeny
Danny,

If you follow the steps here (3rd comment):


You should not see this.

Vijay

Danny Tuppeny

unread,
Jul 29, 2016, 1:13:10 PM7/29/16
to Vijay Menon, General Dart Discussion
Oops, I had somehow lost "--modules node" from my command when I was looking for options to include the SDK. Adding that in removes dart_library :-)

The instructions you mentioned look like it'll let me build a node version of the SDK (since the version John linked also requires dart_library) but it has a path to a /gen/ folder (a patched SDK?) in the args that I don't see in the repo. Are there any instructions on this; or can I just pass the release 1.18 SDK? (or would it be simpler to just try and use dart_library despite being in node?).

Thanks!


Vijay Menon

unread,
Jul 29, 2016, 1:17:43 PM7/29/16
to Danny Tuppeny, General Dart Discussion
Ah, you may need to run:

> ./tool/build_sdk.sh

first in a ddc checkout to populate that gen folder. 

Danny Tuppeny

unread,
Jul 29, 2016, 1:18:38 PM7/29/16
to Vijay Menon, General Dart Discussion
Actually, it turned out to be trivial to just tweak dart_sdk (I found the difference here).

Slowly getting there!

Bob Nystrom

unread,
Jul 29, 2016, 1:39:11 PM7/29/16
to General Dart Discussion, John Messerly

On Fri, Jul 29, 2016 at 9:21 AM, Danny Tuppeny <da...@tuppeny.com> wrote:
Giving it a go, though I really have no idea how complete/stable the dev compiler is, so I don't know if going down this route is a good path (I'm going to be relying on calling plain JS objects heavily! ...)

It might be a bit of a rocky road today, but I personally think it's a great path to explore and it would be great to get some feedback on it. Running Dart apps on node using dev_compiler and JS interop is a subject close to my heart (but, alas, not close to my schedule...).

– bob

Danny Tuppeny

unread,
Jul 29, 2016, 1:57:13 PM7/29/16
to Dart Misc, jmes...@google.com
On Friday, 29 July 2016 18:39:11 UTC+1, Bob wrote:
Giving it a go, though I really have no idea how complete/stable the dev compiler is, so I don't know if going down this route is a good path (I'm going to be relying on calling plain JS objects heavily! ...)

It might be a bit of a rocky road today, but I personally think it's a great path to explore and it would be great to get some feedback on it. Running Dart apps on node using dev_compiler and JS interop is a subject close to my heart (but, alas, not close to my schedule...).

Schedules, shmedules!

Do you know of anyone else is trying to do this (that maybe I can "borrow" ideas from if I hit issues)?

alberto.ags

unread,
Jul 29, 2016, 1:58:30 PM7/29/16
to mi...@dartlang.org, John Messerly
now that clearly Dart focuses only to compile to js will be a first class feature add a option to compile for node.js directly.



--

Bob Nystrom

unread,
Jul 29, 2016, 4:31:11 PM7/29/16
to General Dart Discussion, John Messerly

On Fri, Jul 29, 2016 at 10:57 AM, Danny Tuppeny <da...@tuppeny.com> wrote:
Do you know of anyone else is trying to do this (that maybe I can "borrow" ideas from if I hit issues)?

I don't offhand, sorry.

– bob

Thierry Vilmart

unread,
Feb 28, 2020, 4:33:55 PM2/28/20
to Dart Misc, jmes...@google.com
This is old. But if someone has this problem today.
This works to make node modules:
dartdevc bloc_angular/web/main.dart  --modules es6 -o out.js
Reply all
Reply to author
Forward
0 new messages