amitayh / amd-compiler

Compiles JS source written as AMD modules to a single file without the need for a loader (RequireJS / almond / etc.)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

amd-compiler Build Status codecov

Compiles JS source written as AMD modules to a single file without the need for a loader (RequireJS / almond / etc.)

Installation

$ git clone https://github.com/amitayh/amd-compiler.git
$ cd amd-compiler
$ npm install

Usage

Invoke the bin/compile script to compile source:

$ node bin/compile <path/to/main> >> compiled.js

Example

Say the given modules are defined in the path /foo/bar:

// modA.js
define(["modB", "modC"], function(b, c) {
  return b + c("A");
});
// modB.js
define(["modC"], function(c) {
  return c("B");
});
// modC.js
define(function() {
  return function(name) {
    return "Hello, " + name + "!";
  };
});
// main.js
require(["modA", "modB"], function(a, b) {
  console.log(a, b);
});

Then invoking the comiler script like so:

$ node bin/compile /foo/bar/main

Will generate the following output:

(function () {
  // Source: /foo/bar/modC.js
  var modC = function () {
      return function (name) {
        return "Hello, " + name + "!";
      };
    }();
  // Source: /foo/bar/modB.js
  var modB = function (c) {
      return c("B");
    }(modC);
  // Source: /foo/bar/modA.js
  var modA = function (b, c) {
      return b + c("A");
    }(modB, modC);
  // Source: /foo/bar/main.js
  (function (a, b) {
    console.log(a, b);
  }(modA, modB));
}());

Running tests

Install mocha:

$ npm install -g mocha

Run tests:

$ mocha specs/

Internals

  1. Dependency graph is resolved, using esprima for parsing the JS sources:
    Dependency graph
  2. Graph is topologically sorted, making sure that the dependencies are in the correct order:
    Dependencies in topological order
  3. Using escodegen, the compiler constructs a single source from all needed dependencies

About

Compiles JS source written as AMD modules to a single file without the need for a loader (RequireJS / almond / etc.)


Languages

Language:JavaScript 98.4%Language:Shell 1.6%