Browse files

stop using multi-line variable declarations; put functionality in app…

…ropriately named files
  • Loading branch information...
1 parent 104567a commit 176aec887b9c31a060e7f18694db39ec8a991bc9 @marcuswestin committed Mar 30, 2013
Showing with 178 additions and 181 deletions.
  1. +12 −16 compiler.js
  2. +6 −0 lib/getCode.js
  3. +23 −0 lib/getDependencyList.js
  4. +6 −0 lib/getRequireStatements.js
  5. +102 −0 lib/resolve.js
  6. +14 −146 lib/util.js
  7. +15 −19 server.js
View
28 compiler.js
@@ -1,6 +1,8 @@
-var fs = require('fs'),
- path = require('path'),
- util = require('./lib/util')
+var fs = require('fs')
+var path = require('path')
+var util = require('./lib/util')
+var getCode = require('./lib/getCode')
+var resolve = require('./lib/resolve')
module.exports = {
compile: compileFile,
@@ -9,8 +11,7 @@ module.exports = {
dontAddClosureForModule: dontAddClosureForModule,
dontIncludeModule: dontIncludeModule,
addPath: addPath,
- addFile: addFile,
- addReplacement: addReplacement
+ addFile: addFile
}
var _ignoreClosuresFor = []
@@ -25,11 +26,6 @@ function dontIncludeModule(module) {
return module.exports
}
-function addReplacement(searchFor, replaceWith) {
- util.addReplacement.apply(util, arguments)
- return module.exports
-}
-
function addPath() {
util.addPath.apply(util, arguments)
return module.exports
@@ -45,7 +41,7 @@ function addFile() {
function compileFile(filePath, opts) {
filePath = path.resolve(filePath)
opts = util.extend(opts, { basePath:path.dirname(filePath), toplevel:true })
- var code = util.getCode(filePath)
+ var code = getCode(filePath)
return _compile(code, opts, filePath)
}
@@ -78,7 +74,7 @@ var _compile = function(code, opts, mainModule) {
var uglifyJS = require('uglify-js')
- var ast = uglifyJS.parser.parse(code, opts.strict_semicolons),
+ var ast = uglifyJS.parser.parse(code, opts.strict_semicolons)
ast = uglifyJS.uglify.ast_mangle(ast, opts)
ast = uglifyJS.uglify.ast_squeeze(ast, opts)
@@ -101,8 +97,8 @@ var _compileModule = function(code, pathBase, mainModule) {
var _minifyRequireStatements = function(code, modules) {
for (var i=0, modulePath; modulePath = modules[i]; i++) {
- var escapedPath = modulePath.replace(/\//g, '\\/').replace('(','\\(').replace(')','\\)'),
- regex = new RegExp('__require__\\["'+ escapedPath +'"\\]', 'g')
+ var escapedPath = modulePath.replace(/\//g, '\\/').replace('(','\\(').replace(')','\\)')
+ var regex = new RegExp('__require__\\["'+ escapedPath +'"\\]', 'g')
code = code.replace(regex, '__require__["_'+ i +'"]')
}
@@ -130,8 +126,8 @@ var _replaceRequireStatements = function(modulePath, code, modules, pathBase) {
if (!modules[subModulePath]) {
modules[subModulePath] = true
- var newPathBase = path.dirname(subModulePath),
- newModuleCode = util.getCode(subModulePath)
+ var newPathBase = path.dirname(subModulePath)
+ var newModuleCode = getCode(subModulePath)
_replaceRequireStatements(subModulePath, newModuleCode, modules, newPathBase)
modules.push(subModulePath)
}
View
6 lib/getCode.js
@@ -0,0 +1,6 @@
+var fs = require('fs')
+
+module.exports = function readCode(filePath) {
+ if (!filePath.match(/\.js$/)) { filePath += '.js' }
+ return fs.readFileSync(filePath).toString()
+}
View
23 lib/getDependencyList.js
@@ -0,0 +1,23 @@
+var getCode = require('./getCode')
+var getRequireStatements = require('./getRequireStatements')
+var resolve = require('./resolve')
+
+module.exports = getDependencyList
+
+function getDependencyList(path) {
+ return _findRequiredModules(path, []).concat(path)
+}
+
+var _findRequiredModules = function(absolutePath, _requiredModules) {
+ _requiredModules[absolutePath] = true
+ var code = getCode(absolutePath)
+ var requireStatements = getRequireStatements(code)
+
+ for (var i=0, requireStmnt; requireStmnt = requireStatements[i]; i++) {
+ var absPath = resolve.requireStatement(requireStmnt, absolutePath)
+ if (_requiredModules[absPath]) { continue }
+ _findRequiredModules(absPath, _requiredModules)
+ _requiredModules.push(absPath)
+ }
+ return _requiredModules
+}
View
6 lib/getRequireStatements.js
@@ -0,0 +1,6 @@
+module.exports = getRequireStatements
+
+var _globalRequireRegex = /require\s*\(['"][\w\/\.-]*['"]\)/g
+function getRequireStatements(code) {
+ return code.match(_globalRequireRegex) || []
+}
View
102 lib/resolve.js
@@ -0,0 +1,102 @@
+var path = require('path')
+var fs = require('fs')
+var existsSync = fs.existsSync || path.existsSync
+
+module.exports = {
+ path: resolvePath,
+ requireStatement: 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
+}
+
+function resolvePath(searchPath, pathBase) {
+ if (searchPath[0] == '.') {
+ // relative path, e.g. require("./foo")
+ return _findModuleMain(path.resolve(pathBase, searchPath))
+ }
+
+ var searchParts = searchPath.split('/')
+ var componentName = searchParts[searchParts.length - 1]
+ var name = searchParts.shift()
+ var 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))
+ if (modulePath) { return modulePath }
+
+ if (pathBase != '/') {
+ // not yet at the root - keep climbing!
+ return resolvePath(searchPath, path.resolve(pathBase, '..'))
+ }
+
+ return ''
+}
+
+var _nodePaths = process.env.NODE_PATH.split(':')
+var _pathnameGroupingRegex = /require\s*\(['"]([\w\/\.-]*)['"]\)/
+function resolveRequireStatement(requireStmnt, currentPath) {
+ var rawPath = requireStmnt.match(_pathnameGroupingRegex)[1]
+ var resolvedPath = resolvePath(rawPath, path.dirname(currentPath))
+
+ if (!resolvedPath && rawPath[0] != '.' && rawPath[0] != '/') {
+ for (var i=0; i<_nodePaths.length; i++) {
+ resolvedPath = _findModuleMain(path.resolve(_nodePaths[i], rawPath))
+ if (resolvedPath) { break }
+ }
+ }
+
+ if (!resolvedPath) { throw 'Could not resolve "'+rawPath+'" in "'+currentPath+'"' }
+ return resolvedPath
+}
+
+function _findModuleMain(absModulePath, tryFileName) {
+ var foundPath = ''
+ function attempt(aPath) {
+ if (foundPath) { return }
+ if (existsSync(aPath)) { foundPath = aPath }
+ }
+ attempt(absModulePath + '.js')
+ try {
+ var package = JSON.parse(fs.readFileSync(absModulePath + '/package.json').toString())
+ attempt(path.resolve(absModulePath, package.main+'.js'))
+ attempt(path.resolve(absModulePath, package.main))
+ } catch(e) {}
+ attempt(absModulePath + '/index.js')
+
+ if (tryFileName) { attempt(absModulePath + '/' + tryFileName + '.js') }
+ return foundPath
+}
+
+
View
160 lib/util.js
@@ -1,157 +1,25 @@
-var path = require('path'),
- fs = require('fs')
-
-var existsSync = fs.existsSync || path.existsSync
+var path = require('path')
+var fs = require('fs')
+var resolve = require('./resolve')
module.exports = {
extend: extend,
- getDependencyList: getDependencyList,
- getRequireStatements: getRequireStatements,
- resolve: resolve,
- resolveRequireStatement: resolveRequireStatement,
- addPath: addPath,
- addFile: addFile,
- hasAddedPath: hasAddedPath,
- addReplacement: addReplacement,
- getCode: getCode
+ getDependencyList: require('./getDependencyList'),
+ getRequireStatements: require('./getRequireStatements'),
+ getCode: require('./getCode'),
+ resolvePath: resolve.path,
+ resolveRequireStatement: resolve.requireStatement,
+ addPath: resolve.addPath,
+ addFile: resolve.addFile,
+ hasAddedPath: resolve.hasAddedPath
}
function extend(target, extendWith) {
target = target || {}
for (var key in extendWith) {
- if (typeof target[key] != 'undefined') { continue }
- target[key] = extendWith[key]
- }
- return target
-}
-
-var _replacements = []
-function addReplacement(searchFor, replaceWith) {
- _replacements.push({ searchFor:searchFor, replaceWith:replaceWith })
-}
-
-function getCode(filePath) {
- if (!filePath.match(/\.js$/)) { filePath += '.js' }
- var code = fs.readFileSync(filePath).toString()
- for (var i=0, replacement; replacement = _replacements[i]; i++) {
- code = code.replace(replacement.searchFor, replacement.replaceWith)
- }
- return code
-}
-
-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
-function getRequireStatements(code) {
- return code.match(_globalRequireRegex) || []
-}
-
-function resolve(searchPath, pathBase) {
- if (searchPath[0] == '.') {
- // 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))
- if (modulePath) { return modulePath }
-
- if (pathBase != '/') {
- // not yet at the root - keep climbing!
- return resolve(searchPath, path.resolve(pathBase, '..'))
- }
- return ''
-}
-
-function _findModuleMain(absModulePath, tryFileName) {
- var foundPath = ''
- function attempt(aPath) {
- if (foundPath) { return }
- if (existsSync(aPath)) { foundPath = aPath }
- }
- attempt(absModulePath + '.js')
- try {
- var package = JSON.parse(fs.readFileSync(absModulePath + '/package.json').toString())
- attempt(path.resolve(absModulePath, package.main+'.js'))
- attempt(path.resolve(absModulePath, package.main))
- } catch(e) {}
- attempt(absModulePath + '/index.js')
-
- if (tryFileName) { attempt(absModulePath + '/' + tryFileName + '.js') }
- return foundPath
-}
-
-var _nodePaths = process.env.NODE_PATH.split(':')
-var _pathnameGroupingRegex = /require\s*\(['"]([\w\/\.-]*)['"]\)/
-function resolveRequireStatement(requireStmnt, currentPath) {
- var rawPath = requireStmnt.match(_pathnameGroupingRegex)[1],
- resolvedPath = resolve(rawPath, path.dirname(currentPath))
-
- if (!resolvedPath && rawPath[0] != '.' && rawPath[0] != '/') {
- for (var i=0; i<_nodePaths.length; i++) {
- resolvedPath = _findModuleMain(path.resolve(_nodePaths[i], rawPath))
- if (resolvedPath) { break }
+ if (target[key] === undefined) {
+ target[key] = extendWith[key]
}
}
-
- if (!resolvedPath) { throw 'Could not resolve "'+rawPath+'" in "'+currentPath+'"' }
- return resolvedPath
-}
-
-function getDependencyList(path) {
- return _findRequiredModules(path).concat(path)
-}
-
-var _findRequiredModules = function(absolutePath, _requiredModules) {
- if (!_requiredModules) { _requiredModules = [] }
- _requiredModules[absolutePath] = true
- var code = getCode(absolutePath),
- requireStatements = getRequireStatements(code)
-
- for (var i=0, requireStmnt; requireStmnt = requireStatements[i]; i++) {
- var absPath = resolveRequireStatement(requireStmnt, absolutePath)
- if (_requiredModules[absPath]) { continue }
- _findRequiredModules(absPath, _requiredModules)
- _requiredModules.push(absPath)
- }
- return _requiredModules
-}
-
-var _readFile = function(path) {
- return fs.readFileSync(path).toString()
+ return target
}
-
View
34 server.js
@@ -1,7 +1,7 @@
-var http = require('http'),
- fs = require('fs'),
- path = require('path'),
- util = require('./lib/util')
+var http = require('http')
+var fs = require('fs')
+var path = require('path')
+var util = require('./lib/util')
module.exports = {
listen: listen,
@@ -10,16 +10,10 @@ module.exports = {
isRequireRequest: isRequireRequest,
addPath: addPath,
addFile: addFile,
- addReplacement: addReplacement,
setOpts: setOpts,
handleRequest: handleRequest
}
-function addReplacement(searchFor, replaceWith) {
- util.addReplacement(searchFor, replaceWith)
- return module.exports
-}
-
function addPath() {
util.addPath.apply(util, arguments)
return module.exports
@@ -91,10 +85,12 @@ function handleRequest(req, res) {
}
function _handleMainModuleRequest(reqPath, req, res) {
- var prefix = util.hasAddedPath(reqPath.split('/')[0]) ? '' : './',
- modulePath = util.resolve(prefix + reqPath, opts.path)
+ var prefix = util.hasAddedPath(reqPath.split('/')[0]) ? '' : './'
+ var modulePath = util.resolvePath(prefix + reqPath, opts.path)
if (!modulePath) { return _sendError(res, 'Could not find module "'+reqPath+'" from "'+opts.path+'"') }
+
+
try { var deps = util.getDependencyList(modulePath) }
catch(err) { return _sendError(res, 'in util.getDependencyList: ' + err) }
@@ -147,12 +143,12 @@ function _handleModuleRequest(reqPath, res) {
}
function _getModuleCode(res, reqPath) {
- var _closureStart = ';(function() {',
- _moduleDef = 'var module = {exports:{}}; var exports = module.exports;',
- _closureEnd = '})()'
+ var _closureStart = ';(function() {'
+ var _moduleDef = 'var module = {exports:{}}; var exports = module.exports;'
+ var _closureEnd = '})()'
- var code = util.getCode(reqPath),
- requireStatements = util.getRequireStatements(code)
+ var code = util.getCode(reqPath)
+ var requireStatements = util.getRequireStatements(code)
for (var i=0, requireStmnt; requireStmnt = requireStatements[i]; i++) {
try { var depPath = util.resolveRequireStatement(requireStmnt, reqPath) }
@@ -177,8 +173,8 @@ function _sendError(res, msg) {
}
function _getBase() {
- var host = opts.host,
- port = (!opts.usePagePort && opts.port)
+ var host = opts.host
+ var port = (!opts.usePagePort && opts.port)
if (host && port) {
return '//' + host + ':' + port + '/' + opts.root

0 comments on commit 176aec8

Please sign in to comment.