register-js

1.2.2 • Public • Published

register.js

A compatible layer for node projects to support require feature on both browser and node. Also, provide complex file loading features for browser.

WARNING: Since 1.2.0, "/" as separator is no longer being supported. Dot(.) is the only thing legal.

Usage

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 is equal to

 module.exports = exportObject;

On browser script, it is 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.

Get JSON

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.

License

The MIT License (MIT)

Copyright (c) 2014 Karl.M.Cauchy

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i register-js

Weekly Downloads

0

Version

1.2.2

License

MIT

Last publish

Collaborators

  • karlcauchy