Browse Source

Fixes #2, allow use in Node 0.4 if require is passed as second arg.

jrburke 12 years ago
parent
commit
071ae80c79
4 changed files with 41 additions and 22 deletions
  1. 9 2
      README.md
  2. 29 17
      amdefine.js
  3. 2 2
      package.json
  4. 1 1
      tests/require/sub/dynamic.js

+ 9 - 2
README.md

@@ -10,7 +10,7 @@ requiring those other programs to use AMD.
 
 ```javascript
     "dependencies": {
-        "amdefine": ">=0.0.1"
+        "amdefine": ">=0.0.2"
     }
 ```
 
@@ -22,6 +22,13 @@ Then run `npm install` to get amdefine into your project.
     if (typeof define !== 'function') { var define = require('amdefine')(module) }
 ```
 
+If you want to support Node 0.4, then add `require` as the second parameter to amdefine:
+
+```javascript
+    //Only if you want Node 0.4. If using 0.5 or later, use the above snippet.
+    if (typeof define !== 'function') { var define = require('amdefine')(module, require) }
+```
+
 **Only use this snippet** for loading amdefine. If you preserve the basic structure,
 with the braces, it will be stripped out when using the [RequireJS optimizer](#optimizer).
 
@@ -31,7 +38,7 @@ keep the rest of the structure to get the stripping behavior.
 If you want to deliver amdefine.js with your code but not use the npm/node_modules-installed
 option, then just download the latest release and refer to it using a relative path:
 
-[Version 0.0.1](https://github.com/jrburke/amdefine/raw/0.0.1/amdefine.js)
+[Version 0.0.2](https://github.com/jrburke/amdefine/raw/0.0.2/amdefine.js)
 
 ## define() usage
 

+ 29 - 17
amdefine.js

@@ -4,17 +4,14 @@
  * see: http://github.com/jrburke/amdefine for details
  */
 
-/*jslint strict: false, nomen: false, plusplus: false */
-/*global module, process, require: true */
+/*jslint node: true */
+/*global module, process */
+'use strict';
 
 var path = require('path'),
     loaderCache = {},
     makeRequire;
 
-// Null out require for this file so that it is not accidentally used
-// below, where module.require should be used instead.
-require = null;
-
 /**
  * Given a relative module name, like ./something, normalize it to
  * a real name that can be mapped to a path.
@@ -54,7 +51,7 @@ function makeLoad(id) {
     return load;
 }
 
-function stringRequire(module, id) {
+function stringRequire(module, require, id) {
     //Split the ID by a ! so that
     var index = id.indexOf('!'),
         relId = path.dirname(module.filename),
@@ -64,20 +61,20 @@ function stringRequire(module, id) {
         //Straight module lookup. If it is one of the special dependencies,
         //deal with it, otherwise, delegate to node.
         if (id === 'require') {
-            return makeRequire(module);
+            return makeRequire(module, require);
         } else if (id === 'exports') {
             return module.exports;
         } else if (id === 'module') {
             return module;
         } else {
-            return module.require(id);
+            return require(id);
         }
     } else {
         //There is a plugin in play.
         prefix = id.substring(0, index);
         id = id.substring(index + 1, id.length);
 
-        plugin = module.require(prefix);
+        plugin = require(prefix);
 
         if (plugin.normalize) {
             id = plugin.normalize(id, makeNormalize(relId));
@@ -89,24 +86,24 @@ function stringRequire(module, id) {
         if (loaderCache[id]) {
             return loaderCache[id];
         } else {
-            plugin.load(id, makeRequire(module), makeLoad(id), {});
+            plugin.load(id, makeRequire(module, require), makeLoad(id), {});
 
             return loaderCache[id];
         }
     }
 }
 
-makeRequire = function (module) {
+makeRequire = function (module, require) {
     function amdRequire(deps, callback) {
         if (typeof deps === 'string') {
             //Synchronous, single module require('')
-            return stringRequire(module, deps);
+            return stringRequire(module, require, deps);
         } else {
             //Array of dependencies with a callback.
 
             //Convert the dependencies to modules.
             deps = deps.map(function (depName) {
-                return stringRequire(module, depName);
+                return stringRequire(module, require, depName);
             });
 
             //Wait for next tick to call back the require call.
@@ -130,9 +127,24 @@ makeRequire = function (module) {
     return amdRequire;
 };
 
-function amdefine(module) {
+/**
+ * Creates a define for node.
+ * @param {Object} module the "module" object that is defined by Node for the
+ * current module.
+ * @param {Function} [require]. Node's require function for the current module.
+ * It only needs to be passed in Node versions before 0.5, when module.require
+ * did not exist.
+ * @returns {Function} a define function that is usable for the current node
+ * module.
+ */
+function amdefine(module, require) {
     var alreadyCalled = false;
 
+    //Favor explicit value, passed in if the module wants to support Node 0.4.
+    require = require || function req() {
+        return module.require.apply(module, arguments);
+    };
+
     //Create a define function specific to the module asking for amdefine.
     function define() {
 
@@ -160,11 +172,11 @@ function amdefine(module) {
         //to convert them to dependency values.
         if (deps) {
             deps = deps.map(function (depName) {
-                return stringRequire(module, depName);
+                return stringRequire(module, require, depName);
             });
         } else if (isFactoryFunction) {
             //Pass in the standard require, exports, module
-            deps = [makeRequire(module), module.exports, module];
+            deps = [makeRequire(module, require), module.exports, module];
         }
 
         if (!isFactoryFunction) {

+ 2 - 2
package.json

@@ -1,7 +1,7 @@
 {
     "name": "amdefine",
     "description": "Provide AMD's define() API for declaring modules in the AMD format",
-    "version": "0.0.1",
+    "version": "0.0.2",
     "homepage": "http://github.com/jrburke/amdefine.js",
     "author": "James Burke <jrburke@gmail.com> (http://github.com/jrburke)",
     "licenses": [
@@ -16,6 +16,6 @@
     ],
     "main": "./amdefine.js",
     "engines": {
-        "node": ">=0.6.0"
+        "node": ">=0.4.2"
     }
 }

+ 1 - 1
tests/require/sub/dynamic.js

@@ -1,4 +1,4 @@
-if (typeof define !== 'function') { var define = (require('../../../amdefine'))(module); }
+if (typeof define !== 'function') { var define = (require('../../../amdefine'))(module, require); }
 
 //Just testing a plain exports case.
 define(function (require) {