npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

commonjsify

v0.1.4

Published

Browserify transform for non CommonJS libraries/scripts.

Downloads

11

Readme

commonjsify

npm version Build Status Coverage Status

David Status [devDependency Status](https://david-dm.org/arielschiavoni/ commonjsify#info=devDependencies)

Stories in Ready

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 = require('commonjsify');
var options = {};
var transform = commonjsify(options);

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 = require('browserify');
var commonjsify = require('commonjsify');
// key: angular --> because we require ./vendor/angular.js
// value: angular --> because angular is the global variable that the library creates
// and we must export
var options = {
  angular: 'angular'
};

browserify()
.require(require.resolve('./vendor/angular.js'), {entry: true, expose: 'angular'})
.transform(commonjsify(options))
.bundle()
.pipe(fs.createWriteStream(__dirname + '/bundle.js'))

Usage

Basic example

'use strict';

var browserify = require('browserify');
var fs = require('fs');

browserify()
.require(require.resolve('./vendor/lib.js'), {entry: true, expose: 'myLib'})
.transform(commonjsify({
  'lib': 'lib'
}))
.bundle()
.pipe(fs.createWriteStream(__dirname + '/bundle.js'))

Then in your page you can do:

<script src="bundle.js"></script>
<script>
  var myLib = require('myLib');
  /* ... */
</script>

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 = require('gulp');
var browserify = require('browserify');
var commonjsify = require('commonjsify');
var stringify = require('stringify');
var source = require('vinyl-source-stream');
var production = (process.env.NODE_ENV === 'production');

gulp.task('bundle', ['bundle-vendor', 'bundle-app']);

gulp.task('bundle-vendor', 'Bundle JavaScript vendor', function() {
  browserify({
    noParse: true,
    debug: !production
  })
  .require(require.resolve('angular'), {entry: true, expose: 'angular'})
  .require(require.resolve('angular-ui-router'), {expose: 'ui.router'})
  .transform(commonjsify({
    'angular': 'angular',
    'angular-ui-router': 'angular.module(\'ui.router\')'
  }))
  .bundle()
  .pipe(source('vendor.js'))
  .pipe(gulp.dest('./dist/'));
});

gulp.task('bundle-app', 'Bundle JavaScript modules', function() {
  browserify({
    entries: './src/app.js',
    debug: !production
  })
  .transform(stringify({
    extensions: ['.html'],
    minify: true
  }))
  .external('angular')
  .external('ui.router')
  .bundle()
  .pipe(source('app.js'))
  .pipe(gulp.dest('./dist/'));
});

Then in your page you can do:

<script src="vendor.js"></script>
<script src="app.js"></script>
<script>
  var angular = require('angular');
  var uiRouter = require('ui.router');
  console.log(uiRouter.name);

  var appVersion = angular.injector(['app']).get('version');
  console.log(appVersion);
  /* ... */
</script>

Contributing

If you would like to contribute code, please do the following:

  1. Fork this repository and make your changes.
  2. 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.
  3. Add your name to the "contributors" section in the package.json file.
  4. Squash all of your commits into a single commit via git rebase -i.
  5. Run the tests by running npm install && npm test from the source directory.
  6. 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