简体   繁体   中英

how to provide both node.js & browser support for (sub)modules

Lets' say I've got a sample library split into 2 files: base library and additional module. The base module resides in module.js :

var Calculator = {
    add: function(a, b) { return a + b; },
    sub: function(a, b) { return a - b; }
};

The additional module resides in submodule.js :

if (typeof Calculator == "undefined") {                                                                                                                                                                                                                                        
        var Calculator = {};                                                                                                                                                                                                                                                   
}                                                                                                                                                                                                                                                                              

Calculator.mul = function(a, b) { return a * b; };                                                                                                                                                                                                                             

Calculator.div = function(a, b) { return a / b; };

This is how many js libraries are built (main module + submodules), though probably they're configured a lot better. I've prepared a basic index.html file:

<html>
<head>
        <script type="text/javascript" src="module.js"></script>
        <script type="text/javascript" src="submodule.js"></script>
</head>
<body>
</body>
</html>

that loads the library and enables me to execute it inside the browser (console output below):

Calculator.add(3,4)
7
Calculator.sub(3,4)
-1
Calculator.mul(3,4)
12
Calculator.div(3,4)
0.75

This is all what the library does. Now I want to provide support for node.js (not breaking support for browsers). I've enclosed the definition within immediately-invoked-fun-expr with a root parameter, which is calculated during runtime: node's module.exports if it exists or this==window otherwise (browser). The code looks like this:

(function(root) {

        root.Calculator = {
                add: function(a, b) { return a + b; },
                sub: function(a, b) { return a - b; }
        };

}(typeof module == 'undefined' ? this : module.exports));

When I run node, I can import the module:

> var c = require('./module.js')
undefined
> c
{ Calculator: { add: [Function], sub: [Function] } }

but what can I do to import submodule?

socket.io is good example maybe. You can see structure from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM