Skip to content

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

Notifications You must be signed in to change notification settings

amitayh/amd-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published