Skip to content

Commit

Permalink
Support folder modules, i.e. if you don't find a module at location, …
Browse files Browse the repository at this point in the history
…look for it at location/index.js
  • Loading branch information
marcuswestin committed Feb 16, 2011
1 parent 38ea724 commit 22bff0b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
69 changes: 37 additions & 32 deletions require.js
Expand Up @@ -26,57 +26,62 @@ if (typeof require == 'undefined') (function() {
}
}

return pathParts.join('/') + '.js'
var possiblesBase = pathParts.join('/')
return [possiblesBase + '.js', possiblesBase + '/index.js']
}

// IE6 won't return an anonymous function from eval, so use the function constructor instead
var evaluate = typeof eval('(function(){})') == 'undefined'
? function(src, path) { return (new Function('return ' + src))() }
: function(src, path) { var src = src + '\n//@ sourceURL=' + path; return eval(src) }

var fetchFile = function(path) {
path = location.protocol + '//' + location.host + path
var xhr = new XHR()
try {
xhr.open('GET', path, false)
xhr.send(null)
} catch(e) {
log(path, 'fetchFile open/send error:', e)
return null // firefox file://
}

if (xhr.status == 404 || // all browsers, http://
xhr.status == -1100) { // safari file://
// XXX: We have no way to tell in opera if a file exists and is empty, or is 404
log(path, 'fetchFile xhr.status error:', xhr.status)
return null
var fetchFile = function(possiblePaths) {
while (possiblePaths.length) {
var possiblePath = possiblePaths[0],
url = location.protocol + '//' + location.host + possiblePath,
xhr = new XHR()
try {
xhr.open('GET', url, false)
xhr.send(null)
} catch(e) {
log(url, 'fetchFile open/send error:', e)
}

if (xhr.status == -1100) { // safari file://
// XXX: We have no way to tell in opera if a file exists and is empty, or is 404
log(url, 'fetchFile xhr.status error:', xhr.status)
} else if (xhr.status < 400) {
return xhr.responseText;
}
possiblePaths.shift()
}

return xhr.responseText;
return null
}

var pageBasePath = location.pathname.replace(/\/[^\/]*$/, '/')
window.require = function(modulePath) {
var baseStack = require._base,
currentBase = baseStack[baseStack.length - 1] || require._root || pageBasePath,
path = resolvePath(currentBase, modulePath)
possiblePaths = resolvePath(currentBase, modulePath)

if (require._modules[path]) { return require._modules[path] }
require._base.push(getDir(path))
for (var i=0, path; path = possiblePaths[i]; i++) {
if (require._modules[path]) { return require._modules[path] }
}

var moduleCode = fetchFile(path),
moduleFriendlyName = path.replace(/[\/.]/g, '_')
var moduleCode = fetchFile(possiblePaths),
foundPath = possiblePaths[0] // fetchFile will remove bad paths from possiblePaths

if (!moduleCode) {
if (!foundPath) {
log(path, 'failed to load module', modulePath)
return
}

require._base.push(getDir(foundPath))
var moduleFriendlyName = foundPath.replace(/[\/.]/g, '_')
try {
var inContextCode = '(function(module){ var exports = module.exports;\n'+ moduleCode +'\n})',
importerFunction = evaluate(inContextCode, path)
importerFunction = evaluate(inContextCode, foundPath)
} catch(e) {
if(e instanceof SyntaxError) { log(path, 'a syntax error prevented execution') }
if(e instanceof SyntaxError) { log(foundPath, 'a syntax error prevented execution') }
throw e
}

Expand All @@ -86,20 +91,20 @@ if (typeof require == 'undefined') (function() {
importerFunction(moduleObject)
} catch(e) {
if(e.type == 'syntax_error') {
log(path, 'Syntax error while importing module', e)
log(foundPath, 'Syntax error while importing module', e)
throw e
} else if (!e.requireLogged) {
e.requireLogged = true
if (e.type == 'stack_overflow') {
log(path, 'Stack overflow while importing module', path, e)
log(foundPath, 'Stack overflow while importing module', e)
} else {
log(path, 'enable "break on error" in your debugger to debug', e)
log(foundPath, 'enable "break on error" in your debugger to debug', e)
}
throw e
}
}

require._modules[path] = moduleObject.exports
require._modules[foundPath] = moduleObject.exports
require._base.pop()
return moduleObject.exports
}
Expand Down
2 changes: 2 additions & 0 deletions test/subfolder/moduleB.js
Expand Up @@ -4,3 +4,5 @@ var moduleC = require('./moduleC'),
exports.somethingFromModuleC = moduleC.something
exports.somethingFromModuleD = moduleD.something
exports.something = 'exports in moduleB'

exports.folderModule = require('./folder-module')

0 comments on commit 22bff0b

Please sign in to comment.