Comparing version 0.6.1 to 0.7.0
@@ -19,3 +19,2 @@ // modulr.sync.js (c) 2010 Tobie Langel | ||
if (!mod) { | ||
_modules[key] = mod = { id: id, exports: {} }; | ||
@@ -32,2 +31,3 @@ var fn = _factories[key]; | ||
_modules[key] = mod = { id: id, exports: {} }; | ||
// Create an instance of `require` per module. Each instance has a | ||
@@ -65,3 +65,7 @@ // reference to the path it was called from to be able to properly | ||
case '..': | ||
path.pop(); | ||
if (path.length) { | ||
path.pop(); | ||
} else { | ||
throw new RangeError('Out of bounds identifier: ' + identifier); | ||
} | ||
break; | ||
@@ -68,0 +72,0 @@ default: |
@@ -6,17 +6,12 @@ // modulr.sync.js (c) 2010 Tobie Langel | ||
PREFIX = '__module__'; // Poor man's hasOwnProperty | ||
function require(id) { | ||
var key = PREFIX + id, | ||
mod = _modules[key]; | ||
if (mod) { return mod.exports; } | ||
_modules[key] = mod = { | ||
id: id, | ||
exports: {} | ||
}; | ||
var fn = _factories[key]; | ||
delete _factories[key]; | ||
if (!fn) { throw 'Can\'t find module "' + id + '".'; } | ||
@@ -28,2 +23,4 @@ | ||
} | ||
_modules[key] = mod = { id: id, exports: {} }; | ||
// require.main isn't defined until we actually require the program's | ||
@@ -39,5 +36,5 @@ // entry point. | ||
} | ||
exports.define = define; | ||
exports.require = require; | ||
})(this); |
var util = require('util'), | ||
SuperClass = require('./collector').Collector, | ||
abstractCollector = require('./abstract-collector'), | ||
SuperClass = abstractCollector.AbstractCollector, | ||
_super = SuperClass.prototype, | ||
uglify = require('uglify-js'), | ||
processor = uglify.uglify, | ||
parser = uglify.parser, | ||
parse = parser.parse, | ||
walker = processor.ast_walker(), | ||
identifier = require('module-grapher/lib/identifier'); | ||
parser = uglify.parser; | ||
exports.createASTCollector = createASTCollector; | ||
exports.create = createASTCollector; | ||
function createASTCollector(config) { | ||
return new ASTCollector(config); | ||
var RUNTIME = parser.parse(abstractCollector.getRuntimeSrcCode('modulr.sync.js')); | ||
exports.createAstCollector = createAstCollector; | ||
exports.create = createAstCollector; | ||
function createAstCollector(config) { | ||
return new AstCollector(config); | ||
} | ||
exports.ASTCollector = ASTCollector; | ||
function ASTCollector(config) { | ||
exports.AstCollector = AstCollector; | ||
function AstCollector(config) { | ||
SuperClass.call(this, config); | ||
this.resolveIdentifiers = !!config.resolveIdentifiers; | ||
} | ||
util.inherits(ASTCollector, SuperClass); | ||
util.inherits(AstCollector, SuperClass); | ||
(function(p) { | ||
p.getModuleId = getModuleId; | ||
function getModuleId(m) { | ||
return m.id; | ||
} | ||
p.getModuleSubtree = getModuleSubtree; | ||
function getModuleSubtree(m) { | ||
var ast = this.resolveIdentifiers ? this.getResolvedAST(m) : m.ast, | ||
subtree = ast[1]; | ||
subtree = ["function", null, ["require", "exports", "module"], subtree]; | ||
return this.makeDefine(m.id, subtree); | ||
p.encloseModule = encloseModule; | ||
function encloseModule(m) { | ||
var ast = this.getModuleAst(m); | ||
return ["function", null, ["require", "exports", "module"], this.getSubtree(ast)]; | ||
} | ||
p.getLazyEvaledModuleSubtree = getLazyEvaledModuleSubtree; | ||
function getLazyEvaledModuleSubtree(m) { | ||
var ast = this.resolveIdentifiers ? this.getResolvedAST(m) : m.ast, | ||
src = this.generateCode(ast); | ||
return this.makeDefine(m.id, ["string", src]); | ||
p.escapeModule = escapeModule; | ||
function escapeModule(m) { | ||
var ast = this.getModuleAst(m); | ||
return ["string", this.generateCode(ast)]; | ||
} | ||
p.makeDefine = makeDefine; | ||
function makeDefine(id, subtree) { | ||
return ["call", ["name", "define"], [["string", id], subtree]]; | ||
function makeDefine(m, subtree) { | ||
return ["call", ["name", "define"], [["string", this.getModuleId(m)], subtree]]; | ||
} | ||
p.getMainModuleSubtree = getMainModuleSubtree; | ||
function getMainModuleSubtree() { | ||
return ["call", ["name", "require"], [["string", this._main.id]]]; | ||
p.renderRequireCall = renderRequireCall; | ||
function renderRequireCall(m) { | ||
return ["call", ["name", "require"], [["string", this.getModuleId(m)]]]; | ||
} | ||
p.getRuntimeAST = getRuntimeAST; | ||
function getRuntimeAST() { | ||
return parse(this._runtime); | ||
p.renderRuntime = renderRuntime; | ||
function renderRuntime() { | ||
return RUNTIME; | ||
} | ||
p.toAST = toAST; | ||
function toAST() { | ||
var ast = parse(''), | ||
subTree = ast[1]; | ||
subTree.push(this.getRuntimeAST()); | ||
this._modules.forEach(function(m) { | ||
subTree.push(this.getModuleSubtree(m)); | ||
}, this); | ||
this._lazyEvaledModules.map(function(m) { | ||
subTree.push(this.getLazyEvaledModuleSubtree(m)); | ||
}, this); | ||
subTree.push(this.getMainModuleSubtree()); | ||
return ast; | ||
} | ||
p.generateCode = generateCode; | ||
function generateCode(ast) { | ||
return processor.gen_code(ast, {inline_script: this.inlineSafe}); | ||
return processor.gen_code(ast, { inline_script: this.config.inlineSafe }); | ||
} | ||
p.getResolvedAST = getResolvedAST; | ||
function getResolvedAST(m) { | ||
function handleExpr(expr, args) { | ||
var firstArg = args[0]; | ||
if (expr[0] == "name" && expr[1] == "require" && firstArg[0] == 'string') { | ||
var ident = identifier.create(firstArg[1]); | ||
ident = ident.resolve(m.identifier); | ||
args = args.slice(0); | ||
args[0] = ['string', ident.toString()]; | ||
return [this[0], expr, args]; | ||
} | ||
p.getModuleAst = getModuleAst; | ||
function getModuleAst(m) { | ||
var ast; | ||
if (m.duplicateOf) { | ||
ast = ["toplevel", | ||
[ | ||
["stat", | ||
["assign", true, | ||
["dot", ["name", "module"], "exports"], | ||
["call", ["name", "require"], [["string", this.getModuleId(m.duplicateOf)]]] | ||
] | ||
] | ||
] | ||
]; | ||
} else { | ||
ast = m.ast; | ||
} | ||
return ast; | ||
} | ||
return walker.with_walkers({ | ||
"new": handleExpr, | ||
"call": handleExpr | ||
}, function() { return walker.walk(m.ast); }); | ||
p.getSubtree = getSubtree; | ||
function getSubtree(ast) { | ||
return ast[1]; | ||
} | ||
@@ -102,5 +89,6 @@ | ||
function toString() { | ||
return this.generateCode(this.toAST()); | ||
var ast = parser.parse(''); | ||
this.render(this.getSubtree(ast)); | ||
return this.generateCode(ast); | ||
} | ||
})(ASTCollector.prototype); | ||
})(AstCollector.prototype); |
@@ -1,6 +0,1 @@ | ||
var fs = require('fs'), | ||
path = require('path'); | ||
var RUNTIME_PATH = path.join(__dirname, '..', 'assets', 'modulr.sync.js'); | ||
exports.createBuilder = createBuilder; | ||
@@ -14,4 +9,2 @@ exports.create = createBuilder; | ||
function Builder(config) { | ||
this.lazyEval = config.lazyEval || false; | ||
this.inlineSafe = !!config.inlineSafe; | ||
this.config = config; | ||
@@ -22,3 +15,3 @@ } | ||
p.build = build; | ||
function build(result, callback) { | ||
function build(result) { | ||
var deps = result.dependencies, | ||
@@ -28,10 +21,10 @@ lazyEval = null, | ||
if (this.lazyEval === true) { | ||
if (this.config.lazyEval === true) { | ||
// if lazy-eval is true, all modules are lazy-evaled. | ||
lazyEval = deps; | ||
} else if (this.lazyEval) { | ||
// Else `this.lazyEval` is an array of modules ids that | ||
} else if (this.config.lazyEval) { | ||
// Else `this.config.lazyEval` is an array of modules ids that | ||
// are to be lazy-evaled. Convert it to id/module object | ||
// pairs. | ||
var ids = this.lazyEval, | ||
var ids = this.config.lazyEval, | ||
modules = {}; | ||
@@ -46,5 +39,5 @@ | ||
} else { | ||
var err = new TypeError('LazyEval config option only accepts modules which are dependencies of "' + result.main + '". "' + id + '" is not.'); | ||
callback(err); | ||
return; | ||
var msg = 'LazyEval config option only accepts modules which are dependencies of "'; | ||
msg += result.main + '". "' + id + '" is not.'; | ||
throw new TypeError(msg); | ||
} | ||
@@ -58,31 +51,12 @@ } | ||
result.lazyEval = lazyEval; | ||
collector.setLazyEvaluatedModules(lazyEval); | ||
collector.setModules(deps); | ||
collector.addMainModule(result.main); | ||
for (var id in deps) { | ||
if (lazyEval && (id in lazyEval)) { | ||
collector.addLazyEvaledModule(deps[id]); | ||
} else { | ||
collector.addModule(deps[id]); | ||
} | ||
} | ||
return collector.toString(); | ||
} | ||
if (result.main) { | ||
collector.addMainModule(result.main); | ||
} | ||
fs.readFile(RUNTIME_PATH, 'utf8', function(err, src) { | ||
if (err) { | ||
callback(err); | ||
} else { | ||
collector.addRuntime(src); | ||
result.output = collector.toString(); | ||
callback(null, result); | ||
} | ||
}); | ||
} | ||
p.createCollector = createCollector; | ||
function createCollector(config) { | ||
// Use a sorted collector as a temporary fix until | ||
// a real dev env is implemented. | ||
return require('./sorted-collector').create(config); | ||
return require('./collector').create(config); | ||
} | ||
@@ -94,2 +68,4 @@ | ||
// Collect all the modules which are selected for | ||
// lazy evaluation along with their dependencies. | ||
for (var id in modules) { | ||
@@ -96,0 +72,0 @@ var module = modules[id], |
@@ -0,1 +1,6 @@ | ||
var util = require('util'), | ||
abstractCollector = require('./abstract-collector'), | ||
SuperClass = abstractCollector.AbstractCollector, | ||
_super = SuperClass.prototype; | ||
var JS_ESCAPE_REGEXP = /\\|\r?\n|"/g, | ||
@@ -9,3 +14,4 @@ INLINE_SCRIPT_SAFE_JS_ESCAPE_REGEXP = /\\|\r?\n|"|<\//g, | ||
'</': '<\/' | ||
}; | ||
}, | ||
RUNTIME = abstractCollector.getRuntimeSrcCode('modulr.sync.js'); | ||
@@ -20,38 +26,18 @@ exports.createCollector = createCollector; | ||
function Collector(config) { | ||
this.inlineSafe = !!config.inlineSafe; | ||
this._modules = []; | ||
this._lazyEvaledModules = []; | ||
SuperClass.call(this, config); | ||
} | ||
util.inherits(Collector, SuperClass); | ||
(function(p) { | ||
p._main = null; | ||
p._modules = null; | ||
p._lazyEvaledModules = null; | ||
p._runtime = null; | ||
p.addModule = addModule; | ||
function addModule(m) { | ||
this._modules.push(m); | ||
} | ||
p.addLazyEvaledModule = addLazyEvaledModule; | ||
function addLazyEvaledModule(m) { | ||
this._lazyEvaledModules.push(m); | ||
} | ||
p.makeDefine = makeDefine; | ||
function makeDefine(id, src) { | ||
return '\ndefine("' + id + '", ' + src + ');'; | ||
function makeDefine(m, src) { | ||
var output = '', | ||
identifier = m.indexModule ? m.indexModule.id : m.id; | ||
output += '\n// module: ' + m.id; | ||
output += '\n// file: ' + m.searchPath + '/' + identifier + m.ext; | ||
output += '\ndefine("' + m.id + '", ' + src + ');'; | ||
return output; | ||
} | ||
p.addMainModule = addMainModule; | ||
function addMainModule(m) { | ||
this._main = m; | ||
} | ||
p.addRuntime = addRuntime; | ||
function addRuntime(src) { | ||
this._runtime = src; | ||
} | ||
p.escape = escape; | ||
@@ -67,21 +53,40 @@ function escape(str, inlineSafe) { | ||
function toString() { | ||
var output = []; | ||
var buffer = []; | ||
this.render(buffer); | ||
return buffer.join('\n'); | ||
} | ||
output.push(this._runtime); | ||
p.encloseModule = encloseModule; | ||
function encloseModule(m) { | ||
return 'function(require, exports, module) {\n' + this.getModuleSrc(m) + '\n}'; | ||
} | ||
this._modules.forEach(function(m) { | ||
var src = 'function(require, exports, module) {\n' + m.src + '\n}'; | ||
output.push(this.makeDefine(m, src)); | ||
}, this); | ||
p.escapeModule = escapeModule; | ||
function escapeModule(m) { | ||
return '"' + this.escape(this.getModuleSrc(m), this.config.inlineSafe) + '"'; | ||
} | ||
this._lazyEvaledModules.forEach(function(m) { | ||
var src = this.escape(m.src, this.inlineSafe); | ||
output.push(this.makeDefine(m, '"' + src + '"')); | ||
}, this); | ||
p.getModuleSrc = getModuleSrc; | ||
function getModuleSrc(m) { | ||
if (m.duplicateOf) { | ||
return 'module.exports = require("' + this.getModuleId(m.duplicateOf) + '");' | ||
} | ||
return m.getSrc(); | ||
} | ||
output.push('require("' + this._main.id + '");'); | ||
p.getModuleId = getModuleId; | ||
function getModuleId(m) { | ||
return m.id; | ||
} | ||
return output.join('\n'); | ||
p.renderRequireCall = renderRequireCall; | ||
function renderRequireCall(m) { | ||
return 'require("' + this.getModuleId(m) + '");' | ||
} | ||
p.renderRuntime = renderRuntime; | ||
function renderRuntime() { | ||
return RUNTIME; | ||
} | ||
})(Collector.prototype); | ||
13
main.js
@@ -17,8 +17,5 @@ var fs = require('fs'), | ||
} else { | ||
builder.create(config).build(result, function(err, result) { | ||
if (config.verbose) { | ||
log(result); | ||
} | ||
callback(err, result); | ||
}); | ||
result.output = builder.create(config).build(result); | ||
if (config.verbose) { log(result); } | ||
callback(null, result); | ||
} | ||
@@ -50,6 +47,2 @@ }); | ||
var config = json.modulr || {}; | ||
config.paths = config.paths || []; | ||
if (config.paths.indexOf('.') < 0) { | ||
config.paths.push('.'); | ||
} | ||
config.isPackageAware = true; | ||
@@ -56,0 +49,0 @@ config.root = root; |
@@ -5,5 +5,5 @@ { | ||
"main": "./main", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"dependencies": { | ||
"module-grapher": "0.7.x", | ||
"module-grapher": "0.10.x", | ||
"uglify-js": "~1.0.7" | ||
@@ -16,3 +16,4 @@ }, | ||
"contributors": [ | ||
"Tobie Langel <tobie.langel@gmail.com> (http://tobielangel.com)" | ||
"Tobie Langel <tobie.langel@gmail.com> (http://tobielangel.com)", | ||
"Chris Tice <chris.tice@gmail.com>" | ||
], | ||
@@ -19,0 +20,0 @@ "repository": { |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
23591
17
604
0
4
+ Addedmodule-grapher@0.10.4(transitive)
- Removedmodule-grapher@0.7.0(transitive)
Updatedmodule-grapher@0.10.x