unimod

A small npm package for spawning a JS module pre-built for exporting to multiple environments.

npm install unimod
4 downloads in the last month

Unimod

A small npm package for spawning a JS module pre-built for exporting to multiple environments.

Unimod is short for "universal module". The idea is that it's a common occurrence to need to write a module that can be exported via Node, the browser, or AMD.

Unimod does one little thing: it creates that module for you so you don't have to write the boilerplate. In doing so, it ensures that your boilerplate module won't throw errors in JSLint (with possible exceptions, both of which are ok) and gives you a couple of options as well.

You can call Unimod via the command line or require it as a Node module.

Installation

Make sure you've already got Node.js and npm. Then do one of these:

~$ npm install unimod

Or, if you want to install it globally:

~$ npm install unimod -g

Usage

Here are your usage options (you can view these at any time with unimod -h):

Usage: unimod [options]

Options:

  -v, --version          Display the version of unimod.
  -h, --help             Display help information.
  -n, --name [name]      The module name. Defaults to 'default'.
  -f, --file [name]      The name of the file to put the module in.
  -i, --indent [number]  Indentation space length. Defaults to 2.

With the command line, you can set the name of your module's namespace with -n, determine a path to a file where you want the code to be written with -f, and choose the amount of spaces to be used in an indentation with -i.

For example:

~$ unimod -n myModule -f ./myModule.js -i 4

The above code will create a file called myModule.js containing the following code:

/*
 * Create universal exports...
 */
(function (moduleName, global, closure) {
    "use strict";
    var output = closure(global);

    // AMD
    if (global.define && typeof global.define === "function" && global.define.amd) {
        global.define(moduleName, [], output);

    // Node
    } else if (global.module && global.module.exports) {
        global.module.exports = output;

    // Browser
    } else {
        global[moduleName] = output;
    }

/*
 * Define module code...
 */
}("myModule", this, function (global) {
    "use strict";

    /*
     * Specify functionality to be exported...
     */
    return {};

}));

If you don't include an argument for -f in the command line, Unimod will output the module text to the console.

The two reasons JSLint complains are as follows:

  1. Out of the box, the function where you will write your code gives you the global parameter but doesn't use it anywhere.
  2. JSLint will get mad if you don't use 4 spaces for indentation.

Using it with JavaScript

If you require Unimod as a Node module, you have the same options but you'll call it like this:

var unimod = require('unimod');

unimod({
  name: 'myModule',
  fileName: './myModule.js',
  indent: 4
});

In this case, if you don't include a fileName property, Unimod will synchronously return the module text. If you don't mind using all the defaults, you cal also call Unimod like this:

unimod();
npm loves you