Skip to content

Commit

Permalink
Implement addPath and addFile
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuswestin committed Jul 12, 2011
1 parent a44256a commit fced3c6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 17 deletions.
1 change: 1 addition & 0 deletions Changelog
Expand Up @@ -4,6 +4,7 @@ TODO
intended v0.4.5
+ Set the mimetype of script files correctly
+ Remove node_modules directory - use dependencies in package.json instead
+ Implement addPath and addFile for server and compiler

v0.4.4
+ Import js files directly under node_modules correctly, e.g. ./node_modules/foo.js
Expand Down
14 changes: 13 additions & 1 deletion compiler.js
Expand Up @@ -6,7 +6,19 @@ var fs = require('fs'),

module.exports = {
compile: compileFile,
compileCode: compileCode
compileCode: compileCode,
addPath: addPath,
addFile: addFile
}

function addPath() {
util.addPath.apply(util, arguments)
return module.exports
}

function addFile() {
util.addFile.apply(util, arguments)
return module.exports
}

/* api
Expand Down
45 changes: 43 additions & 2 deletions lib/util.js
Expand Up @@ -5,7 +5,30 @@ module.exports = {
getDependencyList: getDependencyList,
getRequireStatements: getRequireStatements,
resolve: resolve,
resolveRequireStatement: resolveRequireStatement
resolveRequireStatement: resolveRequireStatement,
addPath: addPath,
addFile: addFile,
hasAddedPath: hasAddedPath
}

var _paths = []
function addPath(name, namedPath) {
_paths.push({ name:name, path:path.resolve(namedPath) })
}

var _files = []
function addFile(name, namedPath) {
_files.push({ name:name, path:path.resolve(namedPath) })
}

function hasAddedPath(name) {
for (var i=0, addedPath; addedPath=_paths[i]; i++) {
if (addedPath.name == name) { return true }
}
for (var i=0, addedPath; addedPath=_files[i]; i++) {
if (addedPath.name == name) { return true }
}
return false
}

var _globalRequireRegex = /require\s*\(['"][\w\/\.-]*['"]\)/g
Expand All @@ -18,6 +41,22 @@ function resolve(searchPath, pathBase) {
// relative path, e.g. require("./foo")
return _findModuleMain(path.resolve(pathBase, searchPath))
}

var searchParts = searchPath.split('/'),
componentName = searchParts[searchParts.length - 1],
name = searchParts.shift(),
rest = searchParts.join('/')
for (var i=0, addedPath; addedPath=_paths[i]; i++) {
if (addedPath.name != name) { continue }
var modulePath = _findModuleMain(path.resolve(addedPath.path, rest), componentName)
if (modulePath) { return modulePath }
}

for (var i=0, addedFile; addedFile = _files[i]; i++) {
if (addedFile.name != searchPath) { continue }
return addedFile.path
}

// npm-style path, e.g. require("npm").
// Climb parent directories in search for "node_modules"
var modulePath = _findModuleMain(path.resolve(pathBase, 'node_modules', searchPath))
Expand All @@ -30,7 +69,7 @@ function resolve(searchPath, pathBase) {
return ''
}

function _findModuleMain(absModulePath) {
function _findModuleMain(absModulePath, tryFileName) {
var foundPath = ''
function attempt(aPath) {
if (foundPath) { return }
Expand All @@ -43,6 +82,8 @@ function _findModuleMain(absModulePath) {
attempt(path.resolve(absModulePath, package.main))
} catch(e) {}
attempt(absModulePath + '/index.js')

if (tryFileName) { attempt(absModulePath + '/' + tryFileName + '.js') }
return foundPath
}

Expand Down
48 changes: 34 additions & 14 deletions server.js
Expand Up @@ -10,16 +10,34 @@ module.exports = {
listen: listen,
mount: mount,
connect: connect,
isRequireRequest: isRequireRequest
isRequireRequest: isRequireRequest,
addPath: addPath,
addFile: addFile,
addReplacement: addReplacement
}

function addReplacement(searchFor, replaceWith) {
opts.replacements.push([searchFor, replaceWith])
return module.exports
}

function addPath() {
util.addPath.apply(util, arguments)
return module.exports
}

function addFile() {
util.addFile.apply(util, arguments)
return module.exports
}

function listen(port, _opts) {
if (!_opts) { _opts = { port:port }}
_setOpts(_opts)
port = opts.port = (port || 1234)
opts.handleAllRequests = true
var server = http.createServer()
mount(server)
server.listen(port, opts.host)
server.listen(opts.port, opts.host)
}

function mount(server, _opts, handleAllRequests) {
Expand Down Expand Up @@ -49,31 +67,33 @@ function isRequireRequest(req) {
var opts = {
path: process.cwd(),
root: 'require',
port: null,
host: null
port: 1234,
host: 'localhost',
replacements: []
}

function _setOpts(_opts) {
if (typeof _opts == 'string') {
opts = extend({ path:_opts }, opts)
} else {
opts = extend(_opts, opts)
}
opts = extend(_opts, opts)
}

function _normalizeURL(url) {
return url.replace(/\?.*/g, '').replace(/\/js$/, '.js')
}

/* request handlers
******************/
function _handleRequest(req, res) {
var reqPath = req.url.substr(opts.root.length + 2)
var reqPath = _normalizeURL(req.url).substr(opts.root.length + 2)
if (!reqPath.match(/\.js$/)) {
_handleMainModuleRequest(reqPath, res)
_handleMainModuleRequest(reqPath, req, res)
} else {
_handleModuleRequest(reqPath, res)
}
}

function _handleMainModuleRequest(reqPath, res) {
var modulePath = util.resolve('./' + reqPath, opts.path)
function _handleMainModuleRequest(reqPath, req, res) {
var prefix = util.hasAddedPath(reqPath.split('/')[0]) ? '' : './',
modulePath = util.resolve(prefix + reqPath, opts.path)
if (!modulePath) { return _sendError(res, 'Could not find module "'+reqPath+'" from "'+opts.path+'"') }

try { var deps = util.getDependencyList(modulePath) }
Expand Down

0 comments on commit fced3c6

Please sign in to comment.