register-js
v1.2.2
Published
A compatible layer for node projects to support require feature on both browser and node. Also, provide complex file loading features for browser. And it also support to load JSON files.
Downloads
18
Maintainers
Readme
Register.js
Register js is a compatible level for Node.js programs to run under browser environment. To do this, you should add a single line at the end of your javascript file:
var register_js = require("regsiter-js") //Call only once for your entire project.
//Then you can use it anywhere.
register(exportObject, "TestNamespace.TestObject", module);
Or if you're not 100% sure register.js has loaded correctly, the following statement will do 100% correctly under both browser and Node.js:
var module = require("register-js")(exportObject, "TestNamespace.TestObject", module);
On node.js it will equal to
module.exports = exportObject;
On browser script, it will equal to
window.TestNamespace.TestObject = exportObject;
Also, register.js will provide you a require function for browser, which is almost as the same as node.
var X = require("TestObject", "TestNamespace.TestObject");
And this tool will try to find a resource named as TestNamespace/TestObject.js.
The second parameter will be regarded as the better browser option. However, you can write no 2nd parameter as well:
var X = require("TestObject");
And this calling will find a javascript file named as TestObject.js.
You can set a configuration object as registerConfig:
registerConfig.root = "http://example.com/js/"
Then the searching file will be
http://example.com/js/TestNamespace/TestObject.js
and
http://example.com/js/TestObject.js
Auto-fetch can be useful only when you're running simple websites with limited visitors or testing. Do not use it for large websites, because performance will be a serious problem. Use grunt to compile your javascript code into a single file to avoid multiply loading.
Browser support:
IE 6.0 +
Firefox 1.0 +
Chrome 1.0 +
Safari 5.0 +
Opera 9.0 +
WARNING: Since there has been already a npm package named register, so we used "register-js" instead. For browser, global scope register is also available.
RequireJSON
Since 1.1.0, you can use requireJSON for both node and browser.
requireJSON("test.jsonPackage");
This method will look for test/jsonPackage.json. Also, you can use
requireJSON.fileType = "js";
To make it work with js files.