UNPKG

requisite

Version:
481 lines (450 loc) 14 kB
// Generated by CoffeeScript 1.12.5 var Module, Promise, codegen, compilers, escapeRegex, fs, isFunction, isString, parse, path, ref, resolver, walk, wrapper; fs = require('fs'); path = require('path'); escapeRegex = require('lodash.escaperegexp'); Promise = require('broken'); codegen = require('./codegen'); compilers = require('./compilers'); parse = require('./parse'); resolver = require('./resolver'); walk = require('./walk'); wrapper = require('./wrapper'); ref = require('./utils'), isFunction = ref.isFunction, isString = ref.isString; Module = (function() { function Module(requiredAs, opts) { var exclude, ext, i, k, len, mod, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, s, v; if (opts == null) { opts = {}; } this.moduleCache = (ref1 = opts.moduleCache) != null ? ref1 : {}; this.requiredAs = requiredAs; this.requiredBy = opts.requiredBy; this.resolver = (ref2 = opts.resolver) != null ? ref2 : resolver(); this.compilers = Object.assign({}, compilers, opts.compilers); 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 = (ref4 = opts.include) != null ? ref4 : {}; exclude = []; if (opts.resolved != null) { ref5 = opts.resolved; for (k in ref5) { v = ref5[k]; this.include[k] = v; mod = escapeRegex(k); exclude.push("^" + mod + "$|^" + mod + "\/"); } } if (opts.skip != null) { ref6 = opts.skip; for (i = 0, len = ref6.length; i < len; i++) { s = ref6[i]; exclude.push(escapeRegex(s)); } } if (exclude.length) { 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.naked = opts.naked; if (this.naked) { this.bare = true; } this.strict = opts.strict; this.exported = (ref7 = opts.exported) != null ? ref7 : false; this.required = (ref8 = opts.required) != null ? ref8 : true; this.urlRoot = (ref9 = opts.urlRoot) != null ? ref9 : ''; 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, ref1, results, v; ref1 = this.resolver(this.requiredAs, { basePath: this.basePath, paths: this.paths, requiredBy: this.requiredBy }); results = []; for (k in ref1) { v = ref1[k]; results.push(this[k] = v); } return results; }; Module.prototype.compile = function(cb) { var p; p = new Promise((function(_this) { return function(resolve, reject) { return fs.stat(_this.absolutePath, function(err, stat) { var extension; if (err != null) { return reject(new Error("Unable to find module '" + _this.absolutePath + "': " + err)); } if ((_this.mtime != null) && stat.mtime < _this.mtime) { return resolve(); } _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 reject(err); } _this.source = source; _this.sourceMap = sourceMap; return resolve(); }); }); }); }; })(this)); p.callback(cb); return p; }; Module.prototype.parse = function(opts, cb) { var k, p, ref1, ref2, v; if (isFunction(opts)) { ref1 = [opts, {}], cb = ref1[0], opts = ref1[1]; } if (opts.deep) { ref2 = this.moduleCache; for (k in ref2) { v = ref2[k]; delete this.moduleCache[k]; } } p = new Promise((function(_this) { return function(resolve, reject) { return _this.compile(function(err) { var dependencies, mod, ref3; if (err != null) { return reject(err); } _this.ast = parse(_this.source, { filename: _this.normalizedPath, sourceMap: _this.sourceMap }); try { dependencies = _this.transform(); } catch (error) { err = error; return reject(err); } _this.moduleCache[_this.requireAs] = _this; _this.dependencies = {}; if (_this.include != null) { ref3 = _this.include; for (k in ref3) { v = ref3[k]; mod = _this.findMod(k, v); dependencies.unshift(mod); } } return _this.traverse(dependencies, opts, function(err) { if (err != null) { return reject(err); } return resolve(_this); }); }); }; })(this)); p.callback(cb); return p; }; Module.prototype.transform = function() { var dependencies, err; dependencies = []; try { this.walkAst((function(_this) { return function(node) { var callback, mod, ref1, ref2, required; if (node.type === 'CallExpression' && node.callee.name === 'require') { ref1 = node["arguments"], required = ref1[0], callback = ref1[1]; if (required.type === 'Literal' && isString(required.value)) { node.callee.name = 'rqzt'; if ((ref2 = _this.exclude) != null ? ref2.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, cb) { var p, ref1; if (isFunction(opts)) { ref1 = [opts, {}], cb = ref1[0], opts = ref1[1]; } if (dependencies == null) { dependencies = this.dependencies.slice(0); } if (opts == null) { opts = {}; } if (cb == null) { cb = function() {}; } p = new Promise((function(_this) { return function(resolve, reject) { var cached, dep, mod; if (dependencies.length === 0) { return resolve(); } dep = dependencies.shift(); if (_this.dependencies[dep.requireAs] != null) { return _this.traverse(dependencies, opts, cb); } 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, cb); } dep.compilers = _this.compilers; dep.moduleCache = _this.moduleCache; dep.naked = _this.naked; dep.resolver = _this.resolver; dep.strict = _this.strict; dep.urlRoot = _this.urlRoot; mod = new Module(dep.requiredAs, dep); mod.exclude = _this.exclude; mod.dependents[_this.requireAs] = _this; _this.dependencies[mod.requireAs] = mod; return mod.parse(function(err) { if (err != null) { return reject(err); } return _this.traverse(dependencies, opts, cb); }); }; })(this)); p.callback(cb); return p; }; Module.prototype.append = function(mod) { var i, len, node, ref1, ref2, ref3; ref3 = (ref1 = (ref2 = mod.ast) != null ? ref2.body : void 0) != null ? ref1 : []; for (i = 0, len = ref3.length; i < len; i++) { node = ref3[i]; this.ast.body.push(node); } }; Module.prototype.find = function(query) { var k, nodeModule, ref1, relative, requireAs, v; switch (typeof query) { case 'function': ref1 = this.moduleCache; for (k in ref1) { v = ref1[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, ref1; 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 }); ref1 = this.ast.body; for (i = 0, len = ref1.length; i < len; i++) { node = ref1[i]; define.body.push(node); } return define.ast; }; Module.prototype.walkAst = function(fn) { return walk(this.ast, fn); }; Module.prototype.walkCache = function(fn) { var i, len, mod, ref1, results; ref1 = this.moduleCache; results = []; for (i = 0, len = ref1.length; i < len; i++) { mod = ref1[i]; results.push(fn(mod)); } return results; }; Module.prototype.walkDependencies = function(mod, fn) { var ref1, seen, walkDeps; if (isFunction(mod)) { ref1 = [mod, this], fn = ref1[0], mod = ref1[1]; } seen = {}; walkDeps = function(mod, fn) { var k, ref2, results, v; if (seen[mod.requireAs]) { return; } seen[mod.requireAs] = true; ref2 = mod.dependencies; results = []; for (k in ref2) { v = ref2[k]; if (seen[k]) { continue; } if ((fn(v)) !== false) { results.push(walkDeps(v, fn)); } else { results.push(void 0); } } return results; }; return walkDeps(mod, fn); }; Module.prototype.bundle = function() { var i, j, l, len, len1, len2, node, ref1, ref2, ref3, ref4, toplevel; toplevel = ((ref1 = this.toplevel) != null ? ref1 : new wrapper.Wrapper()).clone(); this.walkDependencies(function(mod) { var i, len, node, ref2, results; if (mod.async || mod.external) { return; } if (mod.ast != null) { ref2 = mod.wrapped().body; results = []; for (i = 0, len = ref2.length; i < len; i++) { node = ref2[i]; results.push(toplevel.body.push(node)); } return results; } }); ref2 = this.wrapped().body; for (i = 0, len = ref2.length; i < len; i++) { node = ref2[i]; toplevel.body.push(node); } if (!(this.async || this.bare)) { if (this.exported) { ref3 = (parse("global." + (path.basename(this.requireAs)) + " = rqzt('" + this.requireAs + "');")).body; for (j = 0, len1 = ref3.length; j < len1; j++) { node = ref3[j]; toplevel.body.push(node); } } else if (this.required) { ref4 = (parse("rqzt('" + this.requireAs + "');")).body; for (l = 0, len2 = ref4.length; l < len2; l++) { node = ref4[l]; toplevel.body.push(node); } } } return toplevel.ast; }; Module.prototype.toString = function(opts) { if (opts == null) { opts = {}; } if (opts.sourceMap == null) { opts.sourceMap = this.enableSourceMap; } if (opts.sourceMapRoot == null) { opts.sourceMapRoot = this.sourceMapRoot; } return codegen(this.bundle(), opts); }; return Module; })(); module.exports = Module; //# sourceMappingURL=module.js.map