module.exports = {
	compileJSFile: compileJSFile
}

var fs = require('fs'),
	sys = require('sys')

/* Compile a javascript file
 ***************************/
function compileJSFile(filePath) {
	var modulePath = filePath.replace(/\.js$/, ''),
		modules = {}
	return _indent('var require = {}\n' + compileJSModule(modulePath, modules))
}

/* Compile a javascript module
 *****************************/
var globalRequireRegex = /require\s*\(['"][\w\/\.]*['"]\)/g,
	pathnameGroupingRegex = /require\s*\(['"]([\w\/\.]*)['"]\)/
function compileJSModule(path, modules) {
	var code = fs.readFileSync(path + '.js').toString(),
		requireStatements = code.match(globalRequireRegex)
	
	if (!requireStatements) { return code }
	
	for (var i=0, requireStatement; requireStatement = requireStatements[i]; i++) {
		var modulePath = requireStatement.match(pathnameGroupingRegex)[1]
		if (modules[modulePath]) {
			code = code.replace(requireStatement, 'require["'+modulePath+'"].exports')
		} else {
			modules[modulePath] = true
			code = code.replace(requireStatement, [
					'(function() {',
					'	var module = require["'+modulePath+'"] = {exports:{}}, exports = module.exports',
					'		',
					'		// start module code',
							compileJSModule(modulePath, modules),
					'		// end module code',
					'		',
					'		return module.exports',
					'})();'
				].join('\n'))
		}
	}
	return code
}

/* Util
 ******/
function _indent(code) {
	var lines = code.replace(/\t/g, '').split('\n'),
		result = [],
		indentation = 0
	
	for (var i=0, line; i < lines.length; i++) {
		line = lines[i]
		
		if (line.match(/^\s*\}/)) { indentation-- }
		result.push(_repeat('\t', indentation) + line)
		if (!line.match(/^\s*\/\//) && line.match(/\{\s*$/)) { indentation++ }
	}
	return result.join('\n')
}

function _repeat(str, times) {
	if (times < 0) { return '' }
	return new Array(times + 1).join(str)
}

@@ -12,16 +12,39 @@
 rts = %7B%0A
+%09compileJS: compileJS,%0A
 %09compile
@@ -113,16 +113,41 @@
 e('sys')
+,%0A%09path = require('path')
 %0A%0A/* Com
@@ -239,70 +239,109 @@
  %7B%0A%09
-var modulePath = filePath.replace(/%5C.js$/, ''),%0A%09%09modules =
+return compileJS(_readFile(filePath), path.dirname(filePath))%0A%7D%0A%0Afunction compileJS(code, basePath)
  %7B
-%7D
 %0A%09re
@@ -345,23 +345,24 @@
 %09return 
-_
 indent
+JS
 ('var re
@@ -393,35 +393,34 @@
 SModule(
-modulePath, modules
+code, %7B%7D, basePath
 ))%0A%7D%0A%0A/*
@@ -529,16 +529,17 @@
 %5D%5B%5Cw%5C/%5C.
+-
 %5D*%5B'%22%5D%5C)
@@ -592,16 +592,17 @@
 (%5B%5Cw%5C/%5C.
+-
 %5D*)%5B'%22%5D%5C
@@ -629,20 +629,20 @@
 SModule(
-path
+code
 , module
@@ -646,68 +646,27 @@
 ules
-) %7B%0A%09var code = fs.readFileSync(path + '.js').toString(),%0A%09%09
+, pathBase) %7B%0A%09var 
 requ
@@ -842,15 +842,21 @@
 ) %7B%0A
+%09%09%0A
 %09%09var 
-m
+rawM
 odul
@@ -911,16 +911,71 @@
 egex)%5B1%5D
+,%0A%09%09%09modulePath = path.join(pathBase, rawModulePath)%0A%09%09
 %0A%09%09if (m
@@ -1115,16 +1115,62 @@
  = true%0A
+%09%09%09var newPathBase = path.dirname(modulePath)%0A
 %09%09%09code 
@@ -1321,28 +1321,16 @@
 ,%0A%09%09%09%09%09'
-%09%09',%0A%09%09%09%09%09'%09
 %09// star
@@ -1351,17 +1351,16 @@
 ,%0A%09%09%09%09%09%09
-%09
 compileJ
@@ -1371,27 +1371,59 @@
 ule(
-modulePath, modules
+_readFile(modulePath + '.js'), modules, newPathBase
 ),%0A%09
@@ -1427,17 +1427,16 @@
 ,%0A%09%09%09%09%09'
-%09
 %09// end 
@@ -1459,20 +1459,8 @@
 %09%09%09'
-%09%09',%0A%09%09%09%09%09'%09
 %09ret
@@ -1490,17 +1490,16 @@
 %09%09%09'%7D)()
-;
 '%0A%09%09%09%09%5D.
@@ -1537,25 +1537,8 @@
 %0A%7D%0A%0A
-/* Util%0A ******/%0A
 func
@@ -1546,15 +1546,16 @@
 ion 
-_
 indent
+JS
 (cod
@@ -1907,24 +1907,42 @@
 in('%5Cn')%0A%7D%0A%0A
+/* Util%0A ******/%0A%0A
 function _re
@@ -2027,12 +2027,83 @@
 join(str)%0A%7D%0A
+%0Afunction _readFile(path) %7B%0A%09return fs.readFileSync(path).toString()%0A%7D%0A

Fix the compiler - the base path was not being kept track of properly, thus making nested imports from sub-folders fail