UNPKG

requisite

Version:

A cosmic javascript bundler.

458 lines (430 loc) 13.2 kB
// Generated by CoffeeScript 1.10.0 var Module, compilers, escapeRegex, fs, path, resolver, stripDebug, utils, wrapper; fs = require('fs'); path = require('path'); stripDebug = require('strip-debug'); escapeRegex = require('lodash.escaperegexp'); compilers = require('./compilers'); resolver = require('./resolver'); utils = require('./utils'); wrapper = require('./wrapper'); Module = (function() { function Module(requiredAs, opts) { var exclude, ext, k, mod, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, v; if (opts == null) { opts = {}; } this.moduleCache = (ref = opts.moduleCache) != null ? ref : {}; this.requiredAs = requiredAs; this.requiredBy = opts.requiredBy; this.resolver = (ref1 = opts.resolver) != null ? ref1 : resolver(); this.compilers = compilers; ref2 = opts.compilers; for (k in ref2) { v = ref2[k]; this.compilers[k] = v; } this.extensions = (function() { var results; results = []; for (ext in this.compilers) { results.push('.' + ext); } return results; }).call(this); this.async = (ref3 = opts.async) != null ? ref3 : false; this.exclude = opts.exclude; this.include = opts.include; if (opts.resolveAs != null) { if (this.include == null) { this.include = {}; } exclude = []; ref4 = opts.resolveAs; for (k in ref4) { v = ref4[k]; this.include[k] = v; mod = escapeRegex(k); exclude.push("^" + mod + "$|^" + mod + "\/"); } exclude = exclude.join('|'); if (this.exclude != null) { exclude = this.exclude + '|' + exclude; } this.exclude = new RegExp(exclude); } this.paths = opts.paths; this.bare = opts.bare; this.strict = opts.strict; this["export"] = (ref5 = opts["export"]) != null ? ref5 : false; this.require = (ref6 = opts.require) != null ? ref6 : true; this.urlRoot = (ref7 = opts.urlRoot) != null ? ref7 : ''; this.ast = null; this.source = null; this.absolutePath = opts.absolutePath; this.basePath = opts.basePath; this.normalizedPath = opts.normalizedPath; this.relativePath = opts.relativePath; this.requireAs = opts.requireAs; if (!((this.absolutePath != null) && (this.normalizedPath != null))) { this.resolve(); } this.dependencies = {}; this.dependents = {}; this.enableSourceMap = opts.sourceMap; this.sourceMapRoot = opts.sourceMapRoot; } Module.prototype.findMod = function(requireAs, modulePath) { var mod; mod = this.resolver(modulePath, { paths: this.paths, basePath: this.basePath, extensions: this.extensions, requiredAs: requireAs }); mod.requireAs = requireAs; mod.basePath = this.basePath; mod.requiredBy = this.absolutePath; return mod; }; Module.prototype.resolve = function() { var k, ref, results, v; ref = this.resolver(this.requiredAs, { basePath: this.basePath, paths: this.paths, requiredBy: this.requiredBy }); results = []; for (k in ref) { v = ref[k]; results.push(this[k] = v); } return results; }; Module.prototype.compile = function(callback) { return fs.stat(this.absolutePath, (function(_this) { return function(err, stat) { var extension; if (err != null) { return callback(new Error("Unable to find module '" + _this.absolutePath + "'")); } if ((_this.mtime != null) && stat.mtime < _this.mtime) { return callback(); } _this.mtime = stat.mtime; extension = (path.extname(_this.absolutePath)).substr(1); return fs.readFile(_this.absolutePath, 'utf8', function(err, source) { var compiler, opts; if ((compiler = _this.compilers[extension]) == null) { throw new Error("No suitable compiler found for " + _this.absolutePath); } opts = { source: source, filename: _this.normalizedPath, absolutePath: _this.absolutePath, sourceMap: _this.enableSourceMap, sourceMapRoot: _this.sourceMapRoot }; return compiler.call(_this, opts, function(err, source, sourceMap) { if (err != null) { return callback(err); } _this.source = source; _this.sourceMap = sourceMap; return callback(); }); }); }; })(this)); }; Module.prototype.parse = function(opts, callback) { var k, ref, ref1, v; if (typeof opts === 'function') { ref = [opts, {}], callback = ref[0], opts = ref[1]; } if (opts.deep) { ref1 = this.moduleCache; for (k in ref1) { v = ref1[k]; delete this.moduleCache[k]; } } return this.compile((function(_this) { return function(err) { var dependencies, error, mod, ref2; if (err != null) { return callback(err); } _this.ast = utils.parse(_this.source, { filename: _this.normalizedPath }); if (_this.sourceMap != null) { utils.sourceMapToAst(_this.ast, _this.sourceMap); } try { dependencies = _this.transform(); } catch (error) { err = error; return callback(err); } _this.moduleCache[_this.requireAs] = _this; _this.dependencies = {}; if (_this.include != null) { ref2 = _this.include; for (k in ref2) { v = ref2[k]; mod = _this.findMod(k, v); dependencies.unshift(mod); } } return _this.traverse(dependencies, opts, function(err) { return callback(err, _this); }); }; })(this)); }; Module.prototype.transform = function() { var dependencies, err, error; dependencies = []; try { this.walkAst((function(_this) { return function(node) { var callback, mod, ref, ref1, required; if (node.type === 'CallExpression' && node.callee.name === 'require') { ref = node["arguments"], required = ref[0], callback = ref[1]; if (required.type === 'Literal' && typeof required.value === 'string') { if ((ref1 = _this.exclude) != null ? ref1.test(required.value) : void 0) { return true; } mod = _this.resolver(required.value, { basePath: _this.basePath, extensions: _this.extensions, requiredAs: required.value, requiredBy: _this.absolutePath }); if (mod.async = callback != null) { required.value = path.join(_this.urlRoot, mod.requireAs); } else { required.value = mod.requireAs; } dependencies.push(mod); } return true; } }; })(this)); } catch (error) { err = error; console.warn(err.toString().replace(/^Error:/, 'Warning:')); } return dependencies; }; Module.prototype.traverse = function(dependencies, opts, callback) { var cached, dep, mod, ref; if (typeof opts === 'function') { ref = [opts, {}], callback = ref[0], opts = ref[1]; } if (dependencies == null) { dependencies = this.dependencies.slice(0); } if (opts == null) { opts = {}; } if (callback == null) { callback = function() {}; } if (dependencies.length === 0) { return callback(); } dep = dependencies.shift(); if (this.dependencies[dep.requireAs] != null) { return this.traverse(dependencies, opts, callback); } if ((cached = this.find(dep.requireAs)) != null) { if (!cached.external) { this.dependencies[cached.requireAs] = cached; cached.dependents[this.requireAs] = this; } return this.traverse(dependencies, opts, callback); } dep.moduleCache = this.moduleCache; dep.resolver = this.resolver; dep.urlRoot = this.urlRoot; dep.strict = this.strict; mod = new Module(dep.requiredAs, dep); mod.exclude = this.exclude; mod.dependents[this.requireAs] = this; this.dependencies[mod.requireAs] = mod; return mod.parse((function(_this) { return function(err) { if (err != null) { return callback(err); } return _this.traverse(dependencies, opts, callback); }; })(this)); }; Module.prototype.append = function(mod) { var i, len, node, ref, ref1, ref2; ref2 = (ref = (ref1 = mod.ast) != null ? ref1.body : void 0) != null ? ref : []; for (i = 0, len = ref2.length; i < len; i++) { node = ref2[i]; this.ast.body.push(node); } }; Module.prototype.find = function(query) { var k, nodeModule, ref, relative, requireAs, v; switch (typeof query) { case 'function': ref = this.moduleCache; for (k in ref) { v = ref[k]; if (query(v)) { return v; } } break; case 'string': requireAs = query.replace(/^\.?\/+/, ''); if (requireAs === '') { return this.moduleCache[this.requireAs]; } relative = this.moduleCache['./' + requireAs]; if (relative != null) { return relative; } nodeModule = this.moduleCache[requireAs]; if (nodeModule != null) { return nodeModule; } break; default: throw new Error('Invalid query for find'); } }; Module.prototype.wrapped = function() { var define, i, len, node, ref; if (this.bare) { return this.ast; } define = new wrapper.Define({ absolutePath: this.absolutePath, async: this.async, mtime: this.mtime, normalizedPath: this.normalizedPath, relativePath: this.relativePath, requireAs: this.requireAs, strict: this.strict, urlRoot: this.urlRoot }); ref = this.ast.body; for (i = 0, len = ref.length; i < len; i++) { node = ref[i]; define.body.push(node); } return define.ast; }; Module.prototype.walkAst = function(fn) { return utils.walk(this.ast, fn); }; Module.prototype.walkCache = function(fn) { var i, len, mod, ref, results; ref = this.moduleCache; results = []; for (i = 0, len = ref.length; i < len; i++) { mod = ref[i]; results.push(fn(mod)); } return results; }; Module.prototype.walkDependencies = function(mod, fn) { var ref, seen, walk; if (typeof mod === 'function') { ref = [mod, this], fn = ref[0], mod = ref[1]; } seen = {}; walk = function(mod, fn) { var k, ref1, results, v; if (seen[mod.requireAs]) { return; } seen[mod.requireAs] = true; ref1 = mod.dependencies; results = []; for (k in ref1) { v = ref1[k]; if (seen[k]) { continue; } if ((fn(v)) !== false) { results.push(walk(v, fn)); } else { results.push(void 0); } } return results; }; return walk(mod, fn); }; Module.prototype.bundle = function() { var i, j, l, len, len1, len2, node, ref, ref1, ref2, ref3, toplevel; toplevel = ((ref = this.toplevel) != null ? ref : new wrapper.Wrapper()).clone(); this.walkDependencies(function(mod) { var i, len, node, ref1, results; if (mod.async || mod.external) { return; } if (mod.ast != null) { ref1 = mod.wrapped().body; results = []; for (i = 0, len = ref1.length; i < len; i++) { node = ref1[i]; results.push(toplevel.body.push(node)); } return results; } }); ref1 = this.wrapped().body; for (i = 0, len = ref1.length; i < len; i++) { node = ref1[i]; toplevel.body.push(node); } if (!(this.async || this.bare)) { if (this["export"]) { ref2 = (utils.parse("global." + this["export"] + " = require('" + this.requireAs + "');")).body; for (j = 0, len1 = ref2.length; j < len1; j++) { node = ref2[j]; toplevel.body.push(node); } } else if (this.require) { ref3 = (utils.parse("require('" + this.requireAs + "');")).body; for (l = 0, len2 = ref3.length; l < len2; l++) { node = ref3[l]; toplevel.body.push(node); } } } return toplevel.ast; }; Module.prototype.toString = function(opts) { var str; if (opts == null) { opts = {}; } if (opts.sourceMap == null) { opts.sourceMap = this.enableSourceMap; } if (opts.sourceMapRoot == null) { opts.sourceMapRoot = this.sourceMapRoot; } str = utils.codegen(this.bundle(), opts); if (opts.stripDebug) { return stripDebug(str).toString(); } else { return str; } }; return Module; })(); module.exports = Module; //# sourceMappingURL=module.js.map