Skip to content
Browse files

Code cleanup, more readable now

  • Loading branch information...
1 parent 98ef87d commit 603fa8f79d54e46af555d82e9a4573b0424bb53a @davglass davglass committed
Showing with 31 additions and 2 deletions.
  1. +17 −0 examples/external-module.js
  2. +14 −2 lib/node-yui3.js
View
17 examples/external-module.js
@@ -1,5 +1,22 @@
YUI.add('external-foo', function(Y) {
+ Y.log(__dirname, 'info', '__dirname');
+ Y.log(__filename, 'info', '__filename');
+
+ //console.log(require);
+ //console.log(process);
+
+ var sys = require('sys');
+ sys.puts('PRINTED FROM INTERNAL REQUIRE WITH sys.puts');
+
+ var config = require('./config');
+ Y.log(config);
+ config.foo('#1 Relative');
+
+ var config = require(__dirname + '/config');
+ Y.log(config);
+ config.foo('#2 Full Dir Require');
+
Y.log('EXTERNAL FOO LOADED');
}, '1.0.0', { requires: ['node'] });
View
16 lib/node-yui3.js
@@ -203,6 +203,8 @@ YUI.require = function(str) {
return require(str);
};
+YUI.process = process;
+
YUI.prototype.__init = YUI.prototype._init;
YUI.prototype._init = function() {
var self = this;
@@ -339,8 +341,18 @@ YUI.include = function(file, cb) {
/*
* This is the fancy stepping required to get the module to eval into the local scope
*/
- var mod = "(function(YUI) { " + data + " return YUI; })",
- fn = process.compile(mod, file);
+ var dirName = path.dirname(file);
+ require.paths.unshift(dirName);
+ var mod = "(function(YUI) { var __dirname = '" + dirName + "'; "+
+ "var __filename = '" + file + "'; " +
+ "var process = YUI.process;" +
+ "var require = function(file) {" +
+ " if (file.indexOf('./') === 0) {" +
+ " file = __dirname + file.replace('./', '/'); }" +
+ " return YUI.require(file); }; " +
+ data + " return YUI; })";
+
+ var fn = process.compile(mod, file);
YUI = fn(YUI);
//cb(null, YUI);

0 comments on commit 603fa8f

Please sign in to comment.
Something went wrong with that request. Please try again.