Is it possible to import a (node) module that is not known to Dart (as a JSObject/dynamic/etc)?

148 views
Skip to first unread message

Danny Tuppeny

unread,
Jul 29, 2016, 1:36:09 PM7/29/16
to Dart Misc
I've managed to get VS Code to load/activate an extension written in Dart but I'm struggling to register commands. The JavaScript I need to generate is:

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

The "vscode" module is an existing node JS module with no Dart code. I'd like to load it so that I can interact with it (as if it was a plain JS object) but I can't find a way to do it. The dev_compiler has output requires() calls for other code, but it knows about them.

I tried using the js package to make a fake require method:

@JS()
library extension
;

import "package:js/js.dart";

@JS()
external
dynamic require(String name);

activate
(dynamic context) {
 
var vscode = require("vscode");
}

However this failed with "dart.global.require is not a function".

It's not clear to me if the stuff on the js package page is up-to-date and the correct way to do JS interop; docs seem to be quite thin on the ground.

John Messerly

unread,
Aug 1, 2016, 12:52:18 PM8/1/16
to General Dart Discussion, Jacob Richman
+someone who might know

--
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.

Jacob Richman

unread,
Aug 1, 2016, 1:12:50 PM8/1/16
to John Messerly, General Dart Discussion
Will fix your issue and will define a global object that looks like what Dart is expecting.

After you import that preamble script you will be able to write the following. 

@JS()
library extension;

import "package:js/js.dart";

@JS()
external dynamic require(String name);

@JS()
class VsCodeWindow {
  external showInformationMessage(String msg);
}

@JS()
class VsCode {
  external Commands get commands;
  external VsCodeWindow get window;
}

@JS()
class Commands {
  external registerCommand(String command, Function);
}

// You might have to tweak node_preamble.dart to expose context. Not sure if you already have that object.

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

Danny Tuppeny

unread,
Aug 1, 2016, 1:17:27 PM8/1/16
to General Dart Discussion, Jacob Richman
I've kinda bailed on using Dart for the extension for now.. I think there are too many complications (my first VSCode extension, DDC unfinished, no good Dart language support in VSCode (yet!), no node bindings for Dart, no VSCode bindings for Dart, etc.) so I've started in TypeScript.

I'm obviously not giving up on Dart (this code is for a Dart extension for VSCode because I want to use it!) and plan to switch to Dart as soon as I think it won't be frustrating and I've got some reasonable editor support working :-)

Thanks for the info; this'll come in handy when I start changing over!

Danny
Reply all
Reply to author
Forward
0 new messages