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

cjs2amd

v0.0.2

Published

Simple CommonJS to AMD converter and bundler

Downloads

3

Readme

cjs2amd - Simple CommonJS to AMD converter and bundler

Converts CommonJS modules:

module.exports = {
	c: require("./b/c"),
	f: require("d")
};

into AMD modules:

require(["b/c", "node_modules/d/f"], function(cModule, fModule) {
	var module = {
		exports: {}
	};

	module.exports = {
		c: cModule,
		f: fModule
	};

	return module.exports;
})

Install

npm install cjs2amd

Command usage

Convert single file:

cjs2amd --output <output file> [--require] [--name <module name>] [--stdout] [--root <dir>] <input file>

Convert file and all it's dependencies:

cjs2amd --recursive --output <output dir> [--root <dir>] <input file>

Bundle file and all it's dependencies:

cjs2amd --bundle --output <output dir> [--root <dir>] <input file>

Print dependencies:

cjs2amd --dependencies <input file>

Options:

-h, --help              output usage information
-d, --dependencies      Print dependencies
-i, --input [path]      Input file path
-o, --output [path]     Output file path, dir if used with -R/--recursive
-r, --root [path]       Root path in resolving, default to current working dir
-n, --name [name]       Module name in define(), or global export variable
-R, --recursive [path]  Convert file and all it's dependencies
-s, --stdout            Output to STDOUT
-b, --bundle            Bundle all dependencies into single file
--require               Use require() fun in module declaration
-g, --global            Export module.exports as global variable
--silent                Don't log warnings and errors

Examples

Source tree:

src/
  a.js
  b/
    c.js
node_modules/
  d/
    f.js // main file of "d" module

Files content:

// src/a.js

module.exports = {
	c: require("./b/c"),
	f: require("d")
};
// src/b/c.js

module.exports = "c";
// node_modules/d/f.js

module.exports = "f";

Convert all

cjs2amd --recursive --output lib --root src src/a.js

Resulting lib/ dir:

lib/
  a.js
  b/
    c.js
  node_modules/
    d/
      f.js

Note! node_modules is placed inside lib. All dirs under root will be placed inside, for example ../../a become a.

Bundle into single file:

cjs2amd --bundle --output lib/bundle.js --root src src/a.js

Contents of lib/bundle.js:

(function () {
	var modules = {};

	var initialized = {};
	window.define = function (name, dependencies, factory) {
		modules[name] = {
			dependencies: dependencies,
			factory: factory
		};
	};

	window.require = function (dependencyNames, factory) {
		var dependencies = [];
		for (var i = 0; i < dependencyNames.length; i++) {
			var name = dependencyNames[i];
			if (!initialized[name]) {
				var module = modules[name];
				require(module.dependencies, function () {
					initialized[name] = module.factory.apply(null, arguments);
				});
			}
			dependencies.push(initialized[name]);
		}
		return factory.apply(null, dependencies);
	};
})();

define("b/c", [], function () {
	var module = { exports: {} };

	module.exports = "c";

	return module.exports;
});

define("node_modules/d/f", [], function () {
	var module = { exports: {} };

	module.exports = "f";

	return module.exports;
});

require(["b/c", "node_modules/d/f"], function (cModule, fModule) {
	var module = { exports: {} };

	module.exports = {
		c: cModule,
		f: fModule
	};

	return module.exports;
});

API

.convert({ input, root, name, recursive, require, global })

Convert file or files(when .recursive == true) to AMD.

Single file:

var result = cjs2amd.convert({
	input: "./src/module.js",
	name: "MyModule", // optional, by default "src/module"
	root: "./", // optional, by default current working dir
});

// result will be converted module source

File and dependencies:

var result = cjs2amd.convert({
	input: "./src/module.js",
	root: "./", // optional, by default current working dir
	recursive: true
});

// result will be hash { "module name in define": "converted source" }
.cmd({ input, dependencies, output, root, name, recursive, stdout, bundle, require, global })

The same as command-line tool, but as a function.