commonjsify
[](https://david-dm.org/arielschiavoni/ commonjsify#info=devDependencies)
Browserify transform for non CommonJS libraries/scripts.
Table of Contents generated with DocToc
Install
With npm do:
$ npm install commonjsify --save-dev
Options
commonjsify accepts a configuration object and returns the transform function to be applied with browserify.
var commonjsify = ;var options = {};var transform = ;
options
is an object that you have to fill with some information about the
non CommonJS modules that you want to convert to CommonJS.
The format for this object is:
- key - must be the same as the filename that you are requiring
with
browserify.require(filename, opts)
without extension (it is a convention) - value - defines how you want to export the module (depends on the library that you are trying to transform to CommonJS)
Example: If you want to transform angular to CommonJS you can configure it like this:
var browserify = ;var commonjsify = ;// key: angular --> because we require ./vendor/angular.js// value: angular --> because angular is the global variable that the library creates// and we must exportvar options =angular: 'angular';
Usage
Basic example
'use strict'; var browserify = ;var fs = ;
Then in your page you can do:
Advanced and more practical example
The following example shows how you can use commonjsify
to transform angular
and angular-ui-router
which are non CommonJS modules. This scenario is particularly useful when you
want to get rid of bower
dependencies and just use npm
(see Front-end Tooling Workflows by Addy Osmani)
as the only source of true for your packages.
The code below shows a gulp task to create multiple bundles, one for vendor and another for the application itself.
'use strict'; var gulp = ;var browserify = ;var commonjsify = ;var stringify = ;var source = ;var production = processenvNODE_ENV === 'production'; gulp; gulp; gulp;
Then in your page you can do:
Contributing
If you would like to contribute code, please do the following:
- Fork this repository and make your changes.
- Write tests for any new functionality. If you are fixing a bug that tests did not cover, please make a test that reproduces the bug.
- Add your name to the "contributors" section in the
package.json
file. - Squash all of your commits into a single commit via
git rebase -i
. - Run the tests by running
npm install && npm test
from the source directory. - Assuming those pass, send the Pull Request off to me for review!
Please do not iterate the package.json version number – I will do that myself when I publish it to NPM.
Style-Guide
Please follow google style-guide for all code contributions.
License
MIT