Permalink
Browse files

support __dirname and __filename in node adapter.

  • Loading branch information...
1 parent 6e62c82 commit d81b12bb521215bf78726135ab24228afdff7b6b @jrburke jrburke committed Oct 11, 2010
Showing with 28 additions and 4 deletions.
  1. +11 −3 build/convert/node/r-source.js
  2. +14 −1 build/convert/node/requireAdapter.js
  3. +3 −0 tasks.txt
@@ -21,7 +21,7 @@
cwd = process.cwd(),
appFilePath = process.argv[2],
isDebug = false,
- appDir;
+ appDir, content;
if (appFilePath === "debug") {
isDebug = true;
@@ -36,7 +36,11 @@
//Now get app directory.
appDir = appFilePath.split("/");
appDir.pop();
- appDir = appDir.join("/");
+ if (appDir.length) {
+ appDir = appDir.join("/");
+ } else {
+ appDir = '.';
+ }
//Create some temporary globals that will be removed by the injected file.
global.__requireIsDebug = isDebug;
@@ -52,5 +56,9 @@
process.compile("require({baseUrl: '" + appDir + "'});", "baseUrl");
//Showtime!
- process.compile(fs.readFileSync(appFilePath), appFilePath);
+ //Try to support __dirname and __filename for node.
+ content = 'var __filename = "' + appFilePath +
+ '"; var __dirname = "' + appDir + '";\n' +
+ fs.readFileSync(appFilePath);
+ process.compile(content, appFilePath);
}());
@@ -20,7 +20,7 @@
require.load = function (moduleName, contextName) {
var url = require.nameToUrl(moduleName, null, contextName),
context = require.s.contexts[contextName],
- content;
+ content, dirName;
//isDone is used by require.ready()
require.s.isDone = false;
@@ -56,6 +56,19 @@
if (isDebug) {
logger.trace("RequireJS about to evaluate module: " + moduleName);
}
+
+ //Attempt to support __dirname and __filename in node
+ dirName = url.split('/');
+ dirName.pop();
+ if (dirName.length) {
+ dirName = dirName.join('/');
+ } else {
+ dirName = '.';
+ }
+ content = '(function () { var __dirname = "' + dirName +
+ '"; var __filename = "' + url +
+ '";\n' + content + '\n}());';
+
process.compile(content, url);
//Support anonymous modules.
View
@@ -8,6 +8,9 @@
What about pages that use that new version but some 3rd party lib wants its own?
That is what contexts are for.
+- For function(require, exports, module) form, do not pass exports if the function
+ does not ask for it, to allow the function to set the exported value by returning.
+
- Figure out why priorityOrder test does not work, and if it should be supported.
Loading a plugin while waiting for a priority load may not be worth supporting,
it also means it will be slower. A priority module can include plugins but just

0 comments on commit d81b12b

Please sign in to comment.