Discussion:
[dart-misc] Is it possible to import a (node) module that is not known to Dart (as a JSObject/dynamic/etc)?
Danny Tuppeny
2016-07-29 17:36:08 UTC
Permalink
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
<https://pub.dartlang.org/packages/js> is up-to-date and the correct way to
do JS interop; docs seem to be quite thin on the ground.
--
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+***@dartlang.org.
'John Messerly' via Dart Misc
2016-08-01 16:51:34 UTC
Permalink
+someone who might know
Post by Danny Tuppeny
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
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.
@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
<https://pub.dartlang.org/packages/js> is up-to-date and the correct way
to do JS interop; docs seem to be quite thin on the ground.
--
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
--
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+***@dartlang.org.
'Jacob Richman' via Dart Misc
2016-08-01 17:12:18 UTC
Permalink
https://github.com/mbullington/node_preamble.dart
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);
}
Post by 'John Messerly' via Dart Misc
+someone who might know
Post by Danny Tuppeny
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
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.
@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
<https://pub.dartlang.org/packages/js> is up-to-date and the correct way
to do JS interop; docs seem to be quite thin on the ground.
--
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
--
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+***@dartlang.org.
Danny Tuppeny
2016-08-01 17:17:10 UTC
Permalink
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
Post by 'John Messerly' via Dart Misc
+someone who might know
Post by Danny Tuppeny
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
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.
@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
<https://pub.dartlang.org/packages/js> is up-to-date and the correct way
to do JS interop; docs seem to be quite thin on the ground.
--
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
--
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
--
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+***@dartlang.org.
Loading...