Announcement

Collapse
No announcement yet.

Creating a node.js module with exports = function(param){ }

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Creating a node.js module with exports = function(param){ }

    So I stumbled onto https://forums.bridge.net/forum/comm...ith-bridge-net which kind of got me towards where I want to be but i'm struggling to figure out how I can get the module export to be how I need it.

    Background: I'm looking into using bridge to build NodeRed nodes from C# types, node red nodes require module.exports to be a function here's a sample of a node red node below (I've removed the actual doing bits)

    module.exports = function (RED) {
    
        const fs = require('fs-extra');
        const path = require('path');
    
        function MyCustomNode(config) {
            RED.nodes.createNode(this, config);
    
            var node = this;
            node.on('input', function (msg) {
                // DO STUFF
            });        
        }
    
        RED.nodes.registerType('myCustomNode', MyCustomNode);
    }
    So I gather i can use the module attribute to specify ModuleType.CommonJS which almost generates what i'm after but in the generated JS the module.exports is set to a js object rather than a parameterised function: https://deck.net/063c1a560052dabaa3504081776081e0

    Here's the generated output from Deck
    Bridge.assembly("Demo", function ($asm, globals) {
        "use strict";
    
        (function () {
            var Module1 = { };
            Bridge.define("RedNodes.MyCustomNode", {
                $metadata : function () { return {"att":1048577,"a":2,"m":[{"a":2,"isSynthetic":true,"n":".ctor","t":1,"sn":"ctor"}]}; },
                $scope: Module1,
                $module: "Module1"
            });
            module.exports.Module1 = Module1;
        }) ();
    
    });
    I have 2 questions:

    1: how do I force the compilation to produce the export as a function rather than an object?
    2: how do I, in that generated function, ensure the call to RED.nodes.registerType('myCustomNode', MyCustomNode); ?

    The rest seems pretty trivial to create c# classes to define the external library (RED). My assumption is it's because of how node-red specifies how nodes are declared, and the fact we register a function into node-red which is called each time it wants to make an instance of a given node. Here's the node-red docs on writing nodes in case it helps: https://nodered.org/docs/creating-nodes/first-node

    Any help or tips would be much appreciated as this would enable me to make some really cool stuff for a project i'm working on!

    Thanks in advance

    #2
    Hello willsmith182, welcome to Bridge forums, and thanks for your interest in the project!

    I believe none of the current module export settings will support the exact format you need. What I can think of is, manually crafting your module.exports lines via the Template Attribute, slash-at code within static methods positioned in the code, or using Script.Write and positioning.

    I believe the best solution would though, implement a proper handling for this type of module output in Bridge Translator, within Bridge Translator's Compiler/Translator/Emitter/Emitter.OutputModule.cs.

    Hope this helps!

    Comment


      #3
      Thanks fabricio, that get me going in the right direction.. almost!

      I've raised a few issues on github related to the specific scenarios I've encountered.

      Comment

      Working...
      X