brackets-userland / brackets-electron

An open source code editor for the web, written in JavaScript, HTML and CSS.
http://brackets.io
MIT License
81 stars 8 forks source link

how/where would you load node modules in extension code? #56

Closed mackenza closed 8 years ago

mackenza commented 8 years ago
const fs = require('fs');

define(function (require, exports, module) {
    "use strict";

    var CommandManager = brackets.getModule("command/CommandManager"),
        EditorManager  = brackets.getModule("editor/EditorManager"),
        Menus          = brackets.getModule("command/Menus");

    var theText = fs.readFileSync('hello-world.txt');

    // Function to run when the menu item is clicked
    function handleHelloWorld() {
        var editor = EditorManager.getFocusedEditor();
        if (editor) {
            var insertionPos = editor.getCursorPos();
            editor.document.replaceRange(theText, insertionPos);
        }
    }

    // First, register a command - a UI-less object associating an id to a handler
    var MY_COMMAND_ID = "helloworld.writehello";   // package-style naming to avoid collisions
    CommandManager.register("Hello World 2", MY_COMMAND_ID, handleHelloWorld);

    // Then create a menu item bound to the command
    // The label of the menu item is the name we gave the command (see above)
    var menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
    menu.addMenuItem(MY_COMMAND_ID);
});

So that totally fails.

So where should I be loading my NodeJS modules? Won't the NodeJS require conflict with the RequireJS require.

Am I missing something obvious?

zaggino commented 8 years ago
const fs = node.require('fs');

should do the trick

mackenza commented 8 years ago

nope. I tried it above and inside the define() and I get a bunch of errors that it can't load menus and other Brackets modules.

mackenza commented 8 years ago

node looks to be a global. I get hints on it when I go to type it.

zaggino commented 8 years ago

This seems to work fine: https://github.com/zaggino/brackets-electron/blob/master/src/filesystem/impls/appshell/AppshellFileSystem.js#L124

mackenza commented 8 years ago

maybe because I am in an extension?

I will play with it some more I guess.

mackenza commented 8 years ago

ok it was something else. There were 147 errors and I was missing a file that caused the rest of the extension not to load.

mackenza commented 8 years ago

I spoke too soon. The extension loads now but it doesn't actually work and I get 142 errors now.

zaggino commented 8 years ago

If you link the extension repo here, I can look at it later (when more important stuff is resolved). Unless you want to put up a PR with fix :-)

ingorichter commented 8 years ago

@mackenza where is your repo?

mackenza commented 8 years ago

https://github.com/mackenza/behw I recreated it and can't test just now, but last time I tried this, it gave me all kinds of errors about loading modules.

zaggino commented 8 years ago

@mackenza see https://github.com/mackenza/behw/pull/1/files