Module Attribute

Geoffrey McGill edited this page Jun 15, 2017 · 2 revisions

The following demonstrates a Bridge C# sample configured as a [Module].

Example (C#)

using System;
using Bridge;

[assembly:Module("Module1")]

namespace Demo2
{
    public class Md1
    {
        public static void M1()
        {
            M2();
        }

        public static void M2()
        {
            Console.WriteLine("Hello");
        }
    }
}

AMD (JavaScript)

define("Module1", function () {
    var Module1 = { };
    Bridge.define("Demo2.Md1", {
        $scope: Module1,
        statics: {
            methods: {
                M1: function () {
                    Module1.Demo2.Md1.M2();
                },
                M2: function () {
                    System.Console.WriteLine("Hello");
                }
            }
        }
    });
    return Module1;
});

CommonJS (JavaScript)

[assembly:Module(ModuleType.CommonJS, "Module1")]
(function () {
    var Module1 = { };
    Bridge.define("Demo2.Md1", {
        $scope: Module1,
        statics: {
            methods: {
                M1: function () {
                    Module1.Demo2.Md1.M2();
                },
                M2: function () {
                    System.Console.WriteLine("Hello");
                }
            }
        }
    });
    module.exports.Module1 = Module1;
}) ();

UMD (JavaScript)

[assembly:Module(ModuleType.UMD, "Module1")]
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define("Module1", factory);
    } else if (typeof module === 'object' && module.exports) {
        module.exports = factory();
    } else {
        root.Module1 = factory();
    }
}(this, function () {
    var Module1 = { };
    Bridge.define("Demo2.Md1", {
        $scope: Module1,
        statics: {
            methods: {
                M1: function () {
                    Module1.Demo2.Md1.M2();
                },
                M2: function () {
                    System.Console.WriteLine("Hello");
                }
            }
        }
    });
    return Module1;
}));

ES6 (javascript)

[assembly:Module(ModuleType.ES6, "Module1")]
(function () {
    var Module1 = { };
    Bridge.define("Demo2.Md1", {
        $scope: Module1,
        statics: {
            methods: {
                M1: function () {
                    Module1.Demo2.Md1.M2();
                },
                M2: function () {
                    System.Console.WriteLine("Hello");
                }
            }
        }
    });
    export {Module1};
}) ();

Using The Module In Another Project (C#)

namespace Demo
{
    public class Program
    {
        public static void Main()
        {
            Demo2.Md1.M1();
        }
    }
}

AMD Module

By default, Bridge uses require function to load AMD modules. If you want to use different library then function name can be changed in bridge.json (loader.loaderFunction property)

Bridge.assembly("Bridge.TestLibrary", function ($asm, globals) {
    "use strict";

    require(["Module1"], function (Module1) {
        Bridge.define("Demo.Program", {
            $main: function () {
                Module1.Demo2.Md1.M1();
            }
        });
    });
});

CommonJS module

Bridge.assembly("Bridge.TestLibrary", function ($asm, globals) {
    "use strict";

    var Module1 = require('Module1');

    Bridge.define("Demo.Program", {
        $main: function () {
            Module1.Demo2.Md1.M1();
        }
    });
});

UMD module

Code will depends from loader.type option in bridge.json, default is AMD (generated code will be the same as in AMD module section). You can change loader type to CommonJS.

ES6 module

Bridge.assembly("Bridge.TestLibrary", function ($asm, globals) {
    "use strict";

    import Module1 from "Module1";

    Bridge.define("Demo.Program", {
        $main: function () {
            Module1.Demo2.Md1.M1();
        }
    });
});

Any module can be loaded manually.

You need to disable autoloading using loader.disableAutoLoading property for all modules or just specific module using loader.disabledModulesMask

bridge.json
"loader": {
    "disableAutoLoading":  true
  }

C#

namespace Demo
{
    public class Program
    {
        public static async void Main()
        {
            Console.WriteLine("Start");
            await Script.LoadModule(typeof (Demo2.Md1));
            Demo2.Md1.M1();
            Console.WriteLine("End");
        }
    }
}

JavaScript

Bridge.assembly("Bridge.TestLibrary", function ($asm, globals) {
    "use strict";

    var Module1;

    Bridge.define("Demo.Program", {
        $main: function () {
            var $step = 0,
                $task1, 
                $jumpFromFinally, 
                $asyncBody = Bridge.fn.bind(this, function () {
                    for (;;) {
                        $step = System.Array.min([0,1], $step);
                        switch ($step) {
                            case 0: {
                                Bridge.Console.log("Start");
                                $task1 = Bridge.loadModule({amd: ["Module1"]}, function () { Module1 = arguments[0]; });
                                $step = 1;
                                $task1.continueWith($asyncBody, true);
                                return;
                            }
                            case 1: {
                                $task1.getAwaitedResult();
                                Module1.Demo2.Md1.M1();
                                Bridge.Console.log("End");
                                return;
                            }
                            default: {
                                return;
                            }
                        }
                    }
                }, arguments);

            $asyncBody();
        }
    });
});
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.