Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

start using std.js, and remove lib/util.js

  • Loading branch information...
commit 57a63f904502871b8c40a8803ed11f3e012af1de 1 parent 997e00c
@marcuswestin authored
View
20 compiler.js
@@ -1,8 +1,10 @@
var fs = require('fs')
var path = require('path')
-var util = require('./lib/util')
+var extend = require('std/extend')
+var each = require('std/each')
var getCode = require('./lib/getCode')
var resolve = require('./lib/resolve')
+var getRequireStatements = require('./lib/getRequireStatements')
module.exports = {
compile: compileFile,
@@ -14,13 +16,13 @@ module.exports = {
*****/
function compileFile(filePath, opts) {
filePath = path.resolve(filePath)
- opts = util.extend(opts, { basePath:path.dirname(filePath), toplevel:true })
+ opts = extend(opts, { basePath:path.dirname(filePath), toplevel:true })
var code = getCode(filePath)
return _compile(code, opts, filePath)
}
function compileCode(code, opts) {
- opts = util.extend(opts, { basePath:process.cwd(), toplevel:true })
+ opts = extend(opts, { basePath:process.cwd(), toplevel:true })
return _compile(code, opts, '<code passed into compiler.compile()>')
}
@@ -70,25 +72,25 @@ var _compileModule = function(code, pathBase, mainModule) {
}
var _minifyRequireStatements = function(code, modules) {
- for (var i=0, modulePath; modulePath = modules[i]; i++) {
+ each(modules, function(modulePath, i) {
var escapedPath = modulePath.replace(/\//g, '\\/').replace('(','\\(').replace(')','\\)')
var regex = new RegExp('__require__\\["'+ escapedPath +'"\\]', 'g')
code = code.replace(regex, '__require__["_'+ i +'"]')
- }
+ })
return code
}
var _replaceRequireStatements = function(modulePath, code, modules, pathBase) {
- var requireStatements = util.getRequireStatements(code)
+ var requireStatements = getRequireStatements(code)
if (!requireStatements.length) {
modules[modulePath] = code
return
}
- for (var i=0, requireStatement; requireStatement = requireStatements[i]; i++) {
- var subModulePath = util.resolveRequireStatement(requireStatement, modulePath)
+ each(requireStatements, function(requireStatement) {
+ var subModulePath = resolve.requireStatement(requireStatement, modulePath)
if (!subModulePath) {
throw new Error("Require Compiler Error: Cannot find module '"+ rawModulePath +"' (in '"+ modulePath +"')")
@@ -103,7 +105,7 @@ var _replaceRequireStatements = function(modulePath, code, modules, pathBase) {
_replaceRequireStatements(subModulePath, newModuleCode, modules, newPathBase)
modules.push(subModulePath)
}
- }
+ })
modules[modulePath] = code
}
View
7 lib/getDependencyList.js
@@ -1,3 +1,4 @@
+var each = require('std/each')
var getCode = require('./getCode')
var getRequireStatements = require('./getRequireStatements')
var resolve = require('./resolve')
@@ -13,11 +14,11 @@ var _findRequiredModules = function(absolutePath, _requiredModules) {
var code = getCode(absolutePath)
var requireStatements = getRequireStatements(code)
- for (var i=0, requireStmnt; requireStmnt = requireStatements[i]; i++) {
+ each(requireStatements, function(requireStmnt) {
var absPath = resolve.requireStatement(requireStmnt, absolutePath)
- if (_requiredModules[absPath]) { continue }
+ if (_requiredModules[absPath]) { return }
_findRequiredModules(absPath, _requiredModules)
_requiredModules.push(absPath)
- }
+ })
return _requiredModules
}
View
22 lib/util.js
@@ -1,22 +0,0 @@
-var path = require('path')
-var fs = require('fs')
-var resolve = require('./resolve')
-
-module.exports = {
- extend: extend,
- getDependencyList: require('./getDependencyList'),
- getRequireStatements: require('./getRequireStatements'),
- getCode: require('./getCode'),
- resolvePath: resolve.path,
- resolveRequireStatement: resolve.requireStatement
-}
-
-function extend(target, extendWith) {
- target = target || {}
- for (var key in extendWith) {
- if (target[key] === undefined) {
- target[key] = extendWith[key]
- }
- }
- return target
-}
View
3  package.json
@@ -15,7 +15,8 @@
},
"author": "Marcus Westin <narcvs@gmail.com> (http://marcuswest.in)",
"dependencies": {
- "uglify-js": "1.2.6"
+ "uglify-js": "1.2.6",
+ "std": "0.1.39"
},
"devDependencies": {},
"directories": {}
View
42 server.js
@@ -1,7 +1,11 @@
var http = require('http')
var fs = require('fs')
var path = require('path')
-var util = require('./lib/util')
+var extend = require('std/extend')
+var getDependencyList = require('./lib/getDependencyList')
+var getRequireStatements = require('./lib/getRequireStatements')
+var getCode = require('./lib/getCode')
+var resolve = require('./lib/resolve')
module.exports = {
listen: listen,
@@ -54,7 +58,7 @@ var opts = {
}
function setOpts(_opts) {
- opts = util.extend(_opts, opts)
+ opts = extend(_opts, opts)
}
function _normalizeURL(url) {
@@ -73,13 +77,13 @@ function handleRequest(req, res) {
}
function _handleMainModuleRequest(reqPath, req, res) {
- var modulePath = util.resolvePath('./' + reqPath, opts.path)
+ var modulePath = resolve.path('./' + 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) }
+ try { var deps = getDependencyList(modulePath) }
+ catch(err) { return _sendError(res, 'in getDependencyList: ' + err) }
var response = ['__require__ = {}', 'require=function(){}']
@@ -90,9 +94,9 @@ function _handleMainModuleRequest(reqPath, req, res) {
if (isMobile) {
// mobile clients take too long per js file request. Inline all the JS into a single request
- for (var i=0, dependency; dependency = deps[i]; i++) {
+ each(deps, function(dependency) {
response.push(_getModuleCode(res, dependency) + "\n")
- }
+ })
} else {
response.push(
'__require__.__scripts = []',
@@ -105,10 +109,10 @@ function _handleMainModuleRequest(reqPath, req, res) {
isPhantom ? ' }, 20)' : '',
'}')
- for (var i=0, dependency; dependency = deps[i]; i++) {
+ each(deps, function(dependency) {
var src = _getBase() + '/' + dependency
response.push('__require__.__scripts.push("'+src+'")')
- }
+ })
response.push('__require__.__loadNext()')
}
@@ -134,15 +138,17 @@ function _getModuleCode(res, reqPath) {
var _moduleDef = 'var module = {exports:{}}; var exports = module.exports;'
var _closureEnd = '})()'
- 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) }
- catch (e) { _sendError(res, e.message || e) }
- if (!depPath) { return _sendError(res, 'Could not resolve module') }
-
- code = code.replace(requireStmnt, '__require__["'+depPath+'"]')
+ var code = getCode(reqPath)
+ var requireStatements = getRequireStatements(code)
+
+ try {
+ each(requireStatements, function(requireStmnt) {
+ var depPath = resolve.requireStatement(requireStmnt, reqPath)
+ if (!depPath) { throw 'Could not resolve module' }
+ code = code.replace(requireStmnt, '__require__["'+depPath+'"]')
+ })
+ } catch(e) {
+ _sendError(res, e.message || e)
}
return _closureStart
Please sign in to comment.
Something went wrong with that request. Please try again.