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

amdize

v0.0.3

Published

AMD-ize JavaScript source files

Downloads

5

Readme

AMD-ize

Concatenate source code files into one file and wrap it to fit some module system, e.g. amd or cmd.

Motivation

  1. Manage JS source file freely.

    AMD modules can be built into one file, but the built modules are still separated modules and still can be invoked publicly. Sometimes it makes sense to split a big AMD module into several smaller pieces for easier maintanence, while still keep these smaller pieces private.

  2. Avoid Duplicated Declarations of Dependencies.

    Some common dependencies are almost used in every AMD module, and they need to be declared again and again in every file. And note that even after JS compression, these duplicated declarations are still there. It makes perfect sense not to repeat yourself (DRY).

  3. Become Independent of Module Frameworks.

    Want to use your AMD-style lib in a non AMD environment, or vice versa? It is actually nothing more than some special wrapping logic. So why not writing code inpendent of any module frameworks and pick one on demand?

Installation

Install globally:

	npm install -g amdize

Usage

Declare Dependencies as String Literals

Given source files:

	|-a.js
	`~lib/
	  `-b.js

a.js:

	"dojo/_base/lang";	// external dependency
	"lib/b";			// internal dependency
	
	function a(arg){
		var obj = {
			arg: arg
		};
		lang.hitch(obj, b)();
	}

lib/b.js:

	function b(){
		console.log(this.arg);
	}

Build to AMD Module (default behavior)

Run amdize to build them into one single AMD module, using default dojo module names:

	amdize --dojo a.js > out.js

Then the out.js will be:

	define([
		"dojo/_base/lang"
	], function(lang){
	
	function b(){
		console.log(this.arg);
	}
	
	function a(arg){
		var obj = {
			arg: arg
		};
		lang.hitch(obj, b)();
	}
	
	return a;
	});

Build to CMD Module

Use --mode to specify which module system to use:

	amdize --dojo --mode cmd a.js > out.js

The out.js will be:

	define(function(require, exports, module){
		var lang = require("dojo/_base/lang");
		
	function b(){
		console.log(this.arg);
	}
	
	function a(arg){
		var obj = {
			arg: arg
		};
		lang.hitch(obj, b)();
	}
	
	return a;
	});

Build to Plain JS File

Also list dependencies at top so that the output can still be amdized with other files:

	amdize --dojo --mode plain a.js > out.js

The out.js will be:

	"dojo/_base/lang";

	var a = (function(){
	
	function b(){
		console.log(this.arg);
	}
	
	function a(arg){
		var obj = {
			arg: arg
		};
		lang.hitch(obj, b)();
	}
	
	return a;
	})();

Specify Name Mappings

Provide custom name mappings in file custom.amdize.js:

	|-custom.amdize.js
	|-a.js
	`~lib/
	  `-b.js

custom.amdize.js:

	exports.map = {
		"dojo/_base/lang": "mylang" // use mylang instead of lang in source code
	};

Specify in the command line:

	amdize --dojo --name-maps custom a.js > out.js

Specify Return name

Specify the exposed name by --return. (If omitted, the main file name is used.)

	amdize --dojo --return b a.js > out.js

The the out.js will be:

	define([
		"dojo/_base/lang"
	], function(lang){
	
	function b(){
		console.log(this.arg);
	}
	
	function a(arg){
		var obj = {
			arg: arg
		};
		lang.hitch(obj, b)();
	}
	
	return b; // Expose b instead of a
	});

Exclude files

Exclude some files (separated by comma) so they and their dependencies won't be in the final output:

	amdize --dojo --exclude lib/b,lib/c a.js > out.js