Skip to content

Commit

Permalink
Initial check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-steele-idem committed Jan 2, 2014
0 parents commit a45bb12
Show file tree
Hide file tree
Showing 9 changed files with 564 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/work
/build
/.idea/
/npm-debug.log
/node_modules
/*.sublime-workspace
*.orig
.DS_Store
coverage
39 changes: 39 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"predef": [

],

"globals": {
"require": true
},

"node" : true,
"es5" : false,
"browser" : true,
"boss" : false,
"curly": false,
"debug": false,
"devel": false,
"eqeqeq": true,
"evil": true,
"forin": false,
"immed": true,
"laxbreak": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": true,
"nomen": false,
"onevar": false,
"plusplus": false,
"regexp": false,
"undef": true,
"sub": false,
"white": false,
"eqeqeq": false,
"latedef": true,
"unused": "vars",

/* Relaxing options: */
"eqnull": true
}
154 changes: 154 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
(function(global) {

var cache = {};
var separator = "/";
var defs = {}; //Registered module definitions are added to this object

function Module(id, require, exports) {
this.id = exports;
this.require = require;
this.exports = exports;
}

function resolve(id, baseName) {
if (id.charAt(0) == separator) {
id = id.substring(1);
}

if (!baseName) {
return id;
}

if (id.charAt(0) == '.') {
if (!baseName) {
return id;
}

var baseNameParts = baseName.split(separator).slice(0, -1);

id.split(separator).forEach(function(part) {
if (part == '..') {
baseNameParts.splice(baseNameParts.length-1, 1); //Remove the last element
}
else if (part != '.') {
baseNameParts.push(part);
}
});

return baseNameParts.join(separator);
}

return id;
}

function build(id, deps, factory) {
var exports = {};
if (typeof factory === 'function') {
var require = createRequire(id);
var module = new Module(id, require, exports);

var local = { //Map local functions and objects to names so that the names can be explicitly used. For example: define(['require', 'exports', 'module'], function(require, exports, module) {})
require: require,
exports: exports,
module: module
};


cache[id] = exports;

if (!deps) {
deps = [];
}

for (var i=0, len=deps.length, depId; i<len; i++) {
depId = deps[i];

if (typeof depId == 'string') {
deps[i] = local[depId] || requireSync(resolve(depId));
}
}

deps = deps.concat(require, exports, module);

var failed = 1;
var result;

try {
result = factory.apply(global, deps);
if (result != null) {
exports = result;
}
else {
exports = module.exports;
}

cache[id] = exports;
failed = 0;
}
finally {
if (failed) {
delete cache[id];
}
}
}
else {
cache[id] = factory;
}

return exports;
}

function requireSync(id) {
if (cache.hasOwnProperty(id)) {
return cache[id];
}

var def = defs[id];

if (def) {
return build.call(global, id, def[0], def[1]);
}
else {
throw new Error(id + ' not found');
}
}

function createRequire(baseName) {
function require(id) {
if (arguments.length > 1) {
throw new Error('Invalid arguments');
}

return requireSync(resolve(id, baseName));
}

// Mirror the require API for Node.js
require.cache = cache;
require.resolve = function(id) {
return resolve(id, baseName);
};

return require;
}

function define(id, deps, factory) {
if (arguments.length === 2) {
factory = deps;
deps = null;
}
if (typeof factory === 'function') {
defs[id] = [deps, factory];
}
else {
cache[id] = factory;
}

}

define.amd = {};

global.define = define;
global.require = createRequire();
global.Module = Module;

}( typeof module === "object" && module && typeof module.exports === "object" ? exports : window));
7 changes: 7 additions & 0 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"raptor": {
"browser-dependencies": [
"index.js"
]
}
}
54 changes: 54 additions & 0 deletions note.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
define('hello-world') -->
raptorAMD.resolveDefine('hello-world') --> 'raptor/raptor-amd/hello-world' -->
define('raptor/raptor-amd/hello-world');

require('hello-world') -->
raptorAMD.findModule('hello-world', module) --> 'raptor/raptor-amd/hello-world'

require('raptor/raptor-amd/hello-world.amd');

var path = require('path');

raptorAMD._resolveDefine = function(id, define) {
var module = define._module;
var moduleRoot = findRootDir(module);
return moduleRoot + '|' + id;
};

raptorAMD._findModule = function(id, define) {
var module = define._module;

for (var i = 0, PL = paths.length; i < PL; i++) {
var basePath = path.resolve(paths[i], request);
var filename;

if (!trailingSlash) {
// try to join the request to the path
filename = tryFile(basePath);

if (!filename && !trailingSlash) {
// try it with each of the extensions
filename = tryExtensions(basePath, exts);
}
}

if (!filename) {
filename = tryPackage(basePath, exts);
}

if (!filename) {
// try it with each of the extensions at "index"
filename = tryExtensions(path.resolve(basePath, 'index'), exts);
}

if (filename) {
Module._pathCache[cacheKey] = filename;
return filename;
}
}

var curDir = path.dirname(options.source.filename)

var moduleRoot = findRootDir(module);
return moduleRoot + '|' + id;
};
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "raptor-amd",
"version": "0.1.0-SNAPSHOT",
"description": "RaptorJS provides an AMD module loader that works in Node, Rhino and the web browser. It also includes various sub-modules to support building optimized web applications.",
"keywords": [
"AMD",
"modules",
"loader"
],
"repository": {
"type": "git",
"url": "https://github.com/raptorjs/raptor-amd.git"
},
"scripts": {
"test": "node_modules/.bin/mocha --ui bdd --reporter spec ./test"
},
"author": "Patrick Steele-Idem <psteeleidem@ebay.com>",
"contributors": [
"Patrick Steele-Idem <psteeleidem@ebay.com>",
"Phillip Gates-Idem <phillip.idem@gmail.com>"
],
"maintainers": "Patrick Steele-Idem <psteeleidem@ebay.com>",
"dependencies": {
},
"devDependencies": {
"mocha": "~1.15.1",
"chai": "~1.8.1"
},
"license": "Apache License v2.0",
"bin": {},
"main": "lib/index.js",
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"ebay": {},
"raptor": {
"browser-dependencies": [
{ "package": "./lib/package.json" }
]
}
}
60 changes: 60 additions & 0 deletions test/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"predef": [
"jasmine",
"spyOn",
"it",
"xit",
"console",
"describe",
"xdescribe",
"expect",
"beforeEach",
"before",
"after",
"waits",
"waitsFor",
"runs",
"raptor",
"$",
"dust",
"__rhinoHelpers",
"Packages",
"JavaAdapter",
"unescape"
],

"globals": {
"define": true,
"require": true
},

"node" : true,
"es5" : false,
"browser" : true,
"boss" : false,
"curly": false,
"debug": false,
"devel": false,
"eqeqeq": true,
"evil": true,
"forin": false,
"immed": true,
"laxbreak": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": true,
"nomen": false,
"onevar": false,
"plusplus": false,
"regexp": false,
"undef": true,
"sub": false,
"white": false,
"eqeqeq": false,
"latedef": true,
"unused": "vars",

/* Relaxing options: */
"eqnull": true
}
Loading

0 comments on commit a45bb12

Please sign in to comment.