Implementing plugin system for my project based on node-webkit

4 views
Skip to first unread message

Boyan O

unread,
Nov 17, 2013, 12:11:02 PM11/17/13
to node-...@googlegroups.com
Migrated from here:

Hello everyone, I am working on project called HIDE(IDE for Haxe programming language)
It uses node-webkit. 
I am writing it in Haxe language, then Haxe compiler generates corresponding JS code on each build.

And I'm trying to implement plugin system for my project.

Why do you need this global scope? Even if your app is using multiple windows each of them should be completely autonomous (use Law Of Demeter like everywhe else).

Maybe I said it in wrong way, sorry.
First plugin loads jQuery, second one loads Bootstrap(which requires jQuery). Each .js script is running in it's own scope, using 

(function() {})()

jQuery defines some global vars for use across different .js files, this is why I need it.

It seems like I can do some things in "background" by running script as "node-main". But it seems like I can't easily debug this code(which runs in node.js context) like I do with node-webkit.

If I get it right, then each require("path/to/script.js") runs script in it's own context, and I can only use exports to create global vars there.

So it seems like the only option to do this is to just run scripts(with node-webkit debugger) in WebKit context, like I do now.

Also can I add my project to list of apps using node-webkit?

Thanks for help.

Jakub Szwacz

unread,
Nov 17, 2013, 1:11:19 PM11/17/13
to node-...@googlegroups.com

First plugin loads jQuery, second one loads Bootstrap(which requires jQuery). Each .js script is running in it's own scope, using
 
(function() {})()

jQuery defines some global vars for use across different .js files, this is why I need it.

Yes, and they are all in global browser scope :), so you can access them easily. Are you newcomer to javascript/browser world? If so make sure you are familiar how javascript inside browser and inside node.js works (they are different in way you have to organize code), in node-webkit you have to use them both, but he best practice for most projects will be to do application structure in browser context, and pin to it node.js modules wherever necessary.

The enclosed function you was mentioning is mainly browser context thing, and is used there to have substitute for module (which node.js have as first class citizen). I feel like you are confusing this ideas.
 
If I get it right, then each require("path/to/script.js") runs script in it's own context, and I can only use exports to create global vars there.

Module could be seen as kind of class. exports object is public API of this class, so saying that things in exports are global is not quite right.
 
Also can I add my project to list of apps using node-webkit?

I think only beta or stable projects should be listed there, because it is some kind of showcase. So if your project is that mature go ahead :)

Boyan O

unread,
Nov 17, 2013, 2:04:37 PM11/17/13
to node-...@googlegroups.com
Thanks for reply.

I haven't worked with node.js much, this is why some things is confusing to me. It seems like I can't use webkit debugger to debug node.js context, so the only solution here is to do everything in browser context.
 
I think only beta or stable projects should be listed there, because it is some kind of showcase. So if your project is that mature go ahead :) 
I think I saw projects in development there too, anyway you right, it's better to add it there, when I will make stable version of my project.

I want to provide relative path to plugin directory in plugins code, instead of providing path relative to page specified in package.json

Currently I do this in plugins:
HIDE.loadJS("../plugins/boyan/jquery/bin/includes/js/jquery/jquery-2.0.3.min.js");

This makes plugin dependent on directory where it is located.

I think it's better to make it to understand path like this:
HIDE.loadJS("includes/js/jquery/jquery-2.0.3.min.js");

HIDE is a global var, which is defined in HIDE.js which is executed when index.html loads.

So for plugins located at folder "plugins", I can do something like this:
function loadJS(name, url)
{
    var nameArray = name.split(.); //pass something like "boyan.jquery"
    var path = require("path").join("plugins", nameArray[0], nameArray[1], "bin", url);
    //loadJSByPath(path) something like that
}

But how to make it when plugin is located somewhere not in "plugins" directory...
Maybe core should remember path to each loaded plugin and then get it from some map:
HIDE.pathToPlugins["boyan.jquery"] = "path/to/plugin";

And then on loadJS from plugin do:
function loadJS(name, url)
{
    var path = require("path").join(HIDE.pathToPlugins[name], "bin", url);
    //loadJSByPath(path) something like that
}

If you guys think that there is exist better way to do this, please let me know. I feel like I'm doing it in not really good way.

воскресенье, 17 ноября 2013 г., 20:11:19 UTC+2 пользователь Jakub Szwacz написал:

Jakub Szwacz

unread,
Nov 17, 2013, 2:54:53 PM11/17/13
to node-...@googlegroups.com
So just pass to plugin code absolute path to itself :)

Boyan O

unread,
Nov 17, 2013, 3:33:13 PM11/17/13
to node-...@googlegroups.com
Plugins can call functions using global var HIDE to load additional .js and .css scripts.

Currently I can just save path to each loaded plugin to core(global var HIDE).
HIDE.pathToPlugins["boyan.jquery"] = "path/to/plugin";

and get by plugin's name like so:
HIDE.loadJS(name, url);

I don't know how to make it work in another way. It does this:
core -> loads plugin(using core.loadJS(url)) -> plugin code -> core.loadJS(name, url)

So core knows path to plugin and does all loading... Then plugin tells it's name and url to core, and core gets path to plugin and loads... Something like that...
I don't know how to pass absolute path to plugin at run-time.

воскресенье, 17 ноября 2013 г., 21:54:53 UTC+2 пользователь Jakub Szwacz написал:
Reply all
Reply to author
Forward
0 new messages