From 44e637aab70f762fa8661dcde448c0b678fe5d45 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 1 Aug 2013 09:04:41 -0400 Subject: [PATCH] fix using amd-loader and define in the same module --- .gitignore | 3 +++ .npmignore | 4 ++++ .travis.yml | 3 ++- lib/amd-loader.js => amd-loader.js | 22 +++++++++++----------- package.json | 6 +++--- test/node_modules/e.js | 6 ++++++ test/test.js | 2 +- test/test2.js | 4 ++++ 8 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 .gitignore create mode 100644 .npmignore rename lib/amd-loader.js => amd-loader.js (83%) create mode 100644 test/node_modules/e.js create mode 100644 test/test2.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..69d3962 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.c9revisions/ +amd-loader-*.tgz +package/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..a8a239c --- /dev/null +++ b/.npmignore @@ -0,0 +1,4 @@ +.git/ +.c9revisions/ +package/ +amd-loader-*.tgz diff --git a/.travis.yml b/.travis.yml index c2fd804..1cf67b7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,4 +2,5 @@ language: node_js node_js: - 0.4 - 0.5 - - 0.6 \ No newline at end of file + - 0.6 + - 0.10 \ No newline at end of file diff --git a/lib/amd-loader.js b/amd-loader.js similarity index 83% rename from lib/amd-loader.js rename to amd-loader.js index 63c7e26..05edc37 100644 --- a/lib/amd-loader.js +++ b/amd-loader.js @@ -1,4 +1,3 @@ -var path = require("path"); var fs = require("fs"); var Module = require("module"); @@ -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) { @@ -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 @@ -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("!"); } @@ -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); @@ -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; } }; \ No newline at end of file diff --git a/package.json b/package.json index 86ed35e..e70e29b 100644 --- a/package.json +++ b/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. ", "contributors": [ @@ -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" diff --git a/test/node_modules/e.js b/test/node_modules/e.js new file mode 100644 index 0000000..2fb486a --- /dev/null +++ b/test/node_modules/e.js @@ -0,0 +1,6 @@ +require("../.."); + +define(function(require, exports, module) { + exports.A = require("./a").A; + exports.E = "E"; +}); \ No newline at end of file diff --git a/test/test.js b/test/test.js index 55f26d5..79d578b 100644 --- a/test/test.js +++ b/test/test.js @@ -1,4 +1,4 @@ -require("../lib/amd-loader"); +require(".."); var assert = require("assert"); console.log("Running amd-loader tests"); diff --git a/test/test2.js b/test/test2.js new file mode 100644 index 0000000..92674de --- /dev/null +++ b/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"); \ No newline at end of file