Comparing version 0.0.0 to 0.0.1
35
cli.js
#!/usr/bin/env node | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var argv = require('optimist').argv; | ||
@@ -8,6 +9,36 @@ var porter = require('./porter'); | ||
var outFile = argv.o; | ||
var watch = argv.w; | ||
var alias = argv.a; | ||
var debug = !!(argv.debug); | ||
var bundle = porter.bundle(entryModule, debug); | ||
fs.writeFileSync(outFile, bundle); | ||
var watchEvents; | ||
if(watch) { | ||
watchEvents = new (require('events').EventEmitter)(); | ||
console.log(watchEvents); | ||
} | ||
if(alias){ | ||
var aliasDir = path.dirname(alias); | ||
var moduleAliasContents = fs.readFileSync(alias).toString().trim(); | ||
if(moduleAliasContents){ | ||
var moduleAliases = JSON.parse(moduleAliasContents); | ||
for(var module in moduleAliases){ | ||
moduleAliases[path.resolve(aliasDir, module)] = path.resolve(aliasDir, moduleAliases[module]); | ||
delete moduleAliases[module]; | ||
} | ||
} | ||
} | ||
var bundle = porter.bundle(entryModule, debug, watchEvents, moduleAliases); | ||
writeFile(bundle); | ||
if(watchEvents){ | ||
watchEvents.on('change', writeFile); | ||
} | ||
function writeFile(bundle){ | ||
console.log(new Date().toTimeString().substring(0,8) + ' writing bundle: ' + outFile); | ||
fs.writeFileSync(outFile, bundle); | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "CommonJS to browser exporter", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"scripts": { | ||
@@ -8,0 +8,0 @@ "test": "node test/run-tests.js" |
113
porter.js
@@ -9,7 +9,7 @@ var fs = require('fs'); | ||
return node.type == 'CallExpression' && node.callee.type == 'Identifier' && node.callee.name == 'require'; | ||
} | ||
}; | ||
var maybeAddJsExtension = function(module){ | ||
return module + ( /\.js$/.test(module) ? '': '.js' ); | ||
} | ||
}; | ||
@@ -24,4 +24,4 @@ var memoize = function(fn){ | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
@@ -36,8 +36,8 @@ | ||
else{ | ||
for(var key in o){ | ||
var value = o[key]; | ||
traverseObject(key, value, condition, propertyCallback); | ||
for(var prop in o){ | ||
var value = o[prop]; | ||
traverseObject(prop, value, condition, propertyCallback); | ||
} | ||
} | ||
} | ||
}; | ||
@@ -49,3 +49,7 @@ exports.commonDir = commonDir; | ||
getChildren = memoize(getChildren); | ||
var last = null; | ||
function buildTree(node){ | ||
//console.log('last', last); | ||
last = node; | ||
//console.log('build tree', node); | ||
if(!(node in tree)){ | ||
@@ -60,10 +64,15 @@ tree[node] = getChildren(node); | ||
return tree; | ||
} | ||
}; | ||
exports.dependencyMap = function(startModule){ | ||
exports.dependencyMap = function(startModule, aliases){ | ||
return exports.buildDependencyMap(startModule, function(module){ | ||
if(aliases && aliases[module]){ | ||
var old = module; | ||
module = aliases[module]; | ||
//console.log('redirect from ' + old + ' to ' + module); | ||
} | ||
var source = fs.readFileSync( maybeAddJsExtension(module) ); | ||
return exports.directDependencies(module, source); | ||
return exports.directDependencies(module, source, aliases); | ||
}); | ||
} | ||
}; | ||
@@ -79,3 +88,3 @@ exports.dependencyArray = function(module, map){ | ||
deps.push(d); | ||
}) | ||
}); | ||
deps.push(dep); | ||
@@ -96,3 +105,3 @@ }); | ||
return deduped; | ||
} | ||
}; | ||
@@ -106,3 +115,3 @@ exports.amdify = function(moduleName, moduleSource, baseDir, map, debug){ | ||
return path.relative(baseDir, path.resolve(moduleDir, relativePath) ); | ||
} | ||
}; | ||
@@ -129,6 +138,6 @@ var dependencies = []; | ||
name: arg | ||
} | ||
}; | ||
}); | ||
var moduleFactoryNode = debug ? | ||
var moduleFactoryNode = debug ? | ||
{ | ||
@@ -179,3 +188,3 @@ "type": "CallExpression", | ||
"type": "ArrayExpression", | ||
"elements": dependencies.map(function(d){ return { type: 'Literal', value: d}}) | ||
"elements": dependencies.map(function(d){ return { type: 'Literal', value: d}; } ) | ||
}, | ||
@@ -192,10 +201,4 @@ moduleFactoryNode | ||
exports.bundle = function(entryModule, debug){ | ||
entryModule = path.resolve(entryModule); | ||
var dependencyMap = exports.dependencyMap(entryModule); | ||
var dependencyArray = exports.orderedDependencies(entryModule, dependencyMap); | ||
dependencyArray.push(entryModule); | ||
var baseDir = commonDir(dependencyArray); | ||
var moduleSources = dependencyArray.map(function(module){ | ||
exports.bundleFiles = function(files, baseDir, dependencyMap, debug){ | ||
var moduleSources = files.map(function(module){ | ||
var source = fs.readFileSync( maybeAddJsExtension(module) ); | ||
@@ -210,4 +213,43 @@ return exports.amdify(module, source, baseDir, dependencyMap, debug); | ||
exports.directDependencies = function(absoluteModule, source){ | ||
exports.bundle = function(entryModule, debug, watch, aliases){ | ||
entryModule = path.resolve(entryModule); | ||
var dependencyMap = exports.dependencyMap(entryModule, aliases); | ||
var dependencyArray = exports.orderedDependencies(entryModule, dependencyMap); | ||
dependencyArray.push(entryModule); | ||
var baseDir = commonDir(dependencyArray); | ||
if(watch){ | ||
console.log('watch dir: ' + baseDir); | ||
dependencyArray.forEach(function(){ | ||
fs.watchFile(baseDir, function(curr, prev){ | ||
if(curr.mtime!=prev.mtime){ | ||
watch.emit('change', exports.bundleFiles(dependencyArray, baseDir, dependencyMap, debug)); | ||
} | ||
}); | ||
}); | ||
/* This api is currently broken on OS X | ||
fs.watch(baseDir, function(ev, filename){ | ||
console.log('watch event' + filename); | ||
if(dependencyArray.indexOf(filename)!=-1){ | ||
watch.emit('change', exports.bundleFiles(dependencyArray, baseDir, dependencyMap, debug)); | ||
} | ||
}); | ||
*/ | ||
} | ||
return exports.bundleFiles(dependencyArray); | ||
}; | ||
exports.directDependencies = function(absoluteModule, source, aliases){ | ||
var entryModuleDir = path.dirname(absoluteModule); | ||
if(source.toString().indexOf('// moment.js')!=-1) return []; | ||
var ast = esprima.parse(source); | ||
@@ -220,11 +262,12 @@ | ||
}, function(key, node){ | ||
modules.push(node.arguments[0].value); | ||
}); | ||
var absModule = path.resolve(entryModuleDir, node.arguments[0].value); | ||
if(aliases && aliases[absModule]){ | ||
absModule = aliases[absModule]; | ||
} | ||
var absoluteModules = modules.map(function(module){ | ||
return path.resolve(entryModuleDir, module); | ||
modules.push(absModule); | ||
}); | ||
return absoluteModules; | ||
} | ||
return modules; | ||
}; | ||
@@ -239,3 +282,3 @@ exports.orderedDependencies = function(entryModule, map){ | ||
return dependencyArray; | ||
} | ||
return dependencyArray; | ||
}; |
var child = require('./sub/child'); | ||
module.exports = function(){ | ||
return ['/main'].concat(child()); | ||
} | ||
}; |
module.exports = function(){ | ||
return ['/sub/child']; | ||
} | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14513
16
430