Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
fix using amd-loader and define in the same module
Browse files Browse the repository at this point in the history
  • Loading branch information
fjakobs committed Aug 1, 2013
1 parent 1fa6fdc commit 44e637a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.c9revisions/
amd-loader-*.tgz
package/
4 changes: 4 additions & 0 deletions .npmignore
@@ -0,0 +1,4 @@
.git/
.c9revisions/
package/
amd-loader-*.tgz
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -2,4 +2,5 @@ language: node_js
node_js:
- 0.4
- 0.5
- 0.6
- 0.6
- 0.10
22 changes: 11 additions & 11 deletions lib/amd-loader.js → amd-loader.js
@@ -1,4 +1,3 @@
var path = require("path");
var fs = require("fs");
var Module = require("module");

Expand All @@ -19,7 +18,7 @@ global.define = function (id, injects, factory) {

// infere the module
var currentModule = moduleStack[moduleStack.length-1];
var module = currentModule || require.main;
var mod = currentModule || module.parent || require.main;

// parse arguments
if (!factory) {
Expand All @@ -28,7 +27,7 @@ global.define = function (id, injects, factory) {
if (factory) {
// two args
if (typeof id === "string") {
if (id !== module.id) {
if (id !== mod.id) {
throw new Error("Can not assign module to a different id than the current file");
}
// default injects
Expand All @@ -53,8 +52,9 @@ global.define = function (id, injects, factory) {
}

var chunks = relativeId.split("!");
var prefix;
if (chunks.length >= 2) {
var prefix = chunks[0];
prefix = chunks[0];
relativeId = chunks.slice(1).join("!");
}

Expand All @@ -66,22 +66,22 @@ global.define = function (id, injects, factory) {
return fs.readFileSync(fileName);
} else
return require(fileName);
}.bind(this, module);
}.bind(this, mod);

injects.unshift("require", "exports", "module");

id = module.id;
id = mod.id;
if (typeof factory !== "function") {
// we can just provide a plain object
return module.exports = factory;
return mod.exports = factory;
}

var returned = factory.apply(module.exports, injects.map(function (injection) {
var returned = factory.apply(mod.exports, injects.map(function (injection) {
switch (injection) {
// check for CommonJS injection variables
case "require": return req;
case "exports": return module.exports;
case "module": return module;
case "exports": return mod.exports;
case "module": return mod;
default:
// a module dependency
return req(injection);
Expand All @@ -90,6 +90,6 @@ global.define = function (id, injects, factory) {

if (returned) {
// since AMD encapsulates a function/callback, it can allow the factory to return the exports.
module.exports = returned;
mod.exports = returned;
}
};
6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name" : "amd-loader",
"version" : "0.0.4",
"version" : "0.0.5",
"description" : "Add the capability to load AMD (Asynchronous Module Definition) modules to node.js",
"author": "ajax.org B.V. <info@ajax.org>",
"contributors": [
Expand All @@ -10,9 +10,9 @@
"type" : "git",
"url" : "http://github.com/ajaxorg/node-amd-loader.git"
},
"main": "./lib/amd-loader",
"main": "./amd-loader.js",
"scripts" : {
"test" : "node test/test.js"
"test" : "node test/test.js && node test/test2.js"
},
"engines" : {
"node" : ">= 0.4.11"
Expand Down
6 changes: 6 additions & 0 deletions test/node_modules/e.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/test.js
@@ -1,4 +1,4 @@
require("../lib/amd-loader");
require("..");
var assert = require("assert");

console.log("Running amd-loader tests");
Expand Down
4 changes: 4 additions & 0 deletions test/test2.js
@@ -0,0 +1,4 @@
// this tests the case where the AMD-loader is not loaded in the main module and
// the module using amd-loader uses define itself
console.log("require file with amd-loader and define")
require("e");

0 comments on commit 44e637a

Please sign in to comment.