Requisite.prototype.process = function (source, pathname) { var self = this, files = findRequires(source, { raw: true }), o, p; while (files.length) { o = files.shift(); if (!o.value) { return new Error("Cannot handle non-string required path: " + o.raw); } try { p = require.resolve(path.resolve(path.dirname(pathname.toString()), o.value)); } catch (err) { throw new Error(err.message + ' (require: ' + o.value + ') (in: ' + pathname + ')'); } if (undefined === this.requires[p]) { this.requires[p] = { idx: this.idx++, source: null }; // prevent from "cyclic" loops this.requires[p].source = this.process(fs.readFileSync(p, 'utf8'), p); } source = source.replace(o.value, this.requires[p].idx); } return source; };
var uniqDeps = uniq(flatten(srcFiles.map(function (srcFile) { var code = fs.readFileSync(srcFile, 'utf8'); try { return findRequires(code); } catch (e) { return []; // happens if this is an es6 module, parsing fails } }))).filter(function (dep) {
parseDependencies = function (text) { return findRequires(text, { raw: true }).map(function (node) { var path = node.value; if (!path) { throw new TypeError("Not supported require call: '" + node.raw + "'"); } return (path.slice(-3) === '.js') ? path.slice(0, -3) : path; }); };
parseDependencies = function (text, filename, ignoreErrors) { return findRequires(text, { raw: true }).map(function (node) { var path = node.value; if (!path) { if (!ignoreErrors) { throw new CustomError("Not parsable require call: `" + node.raw + "` at " + filename + ":" + node.line + "\n You may" + " ignore such errors with ignoreErrors option ('ignore-errors'" + " when running from command line)", 'DYNAMIC_REQUIRE'); } console.warn("Not parsable require call (ignored): `" + node.raw + "` at " + filename + ":" + node.line); } return path; }); };
Requisite.prototype.process = function (source, pathname) { var self = this, base = path.dirname(pathname.toString()), files = findRequires(source, { raw: true }), o, p; while (files.length) { o = files.shift(); if (!o.value) { return new Error("Cannot handle non-string required path: " + o.raw); } try { p = String(o.value) // replace ^ with application root .replace(/^\^\/*/, (pathname.appRoot || '^') + '/') // replace @ with package root .replace(/^@\/*/, (pathname.pkgRoot || '@') + '/'); // try to resolve path relatively to pathname p = new Pathname(require.resolve(path.resolve(base, p)), { appRoot: pathname.appRoot, pkgRoot: pathname.pkgRoot }); } catch (err) { throw new Error(err.message + ' (require: ' + o.value + ') (in: ' + pathname + ')'); } if (undefined === this.requires[p]) { this.requires[p] = { idx: this.idx++, apiPath: pathname.apiPath, source: null }; // prevent from "cyclic" loops this.requires[p].source = this.process(p.readSync(), p); } source = source.replace(o.value, this.requires[p].idx); } return source; };
parseDependencies = function (text, filename, ignoreErrors) { var deps; try { deps = findRequires(text, { raw: true }); } catch (e) { throw customError(e.message + " in " + filename, 'AST_ERROR', { origin: e }); } return deps.filter(function (node) { if (node.value != null) return true; if (!ignoreErrors) { throw customError("Not parsable require call: `" + node.raw + "` at " + filename + ":" + node.line + "\n You may" + " ignore such errors with ignoreErrors option ('ignore-errors'" + " when running from command line)", 'DYNAMIC_REQUIRE'); } console.warn("Not parsable require call (ignored): `" + node.raw + "` at " + filename + ":" + node.line); }); };
/* * Wraps the given source code string as a module definition for the client. * Recursively browserifies and embeds all of unbundled dependencies. * * options: * * - wrapper - src wrapper (client/require), or empty (for main.js) * - apiPath - api path, for flient only * - fsPath - current file name * - allowEJS - used only for `main.js`, to inject some variables in <file>.js.ejs */ function browserifySingle(sandbox, pkgName, source, options) { var result = [] , N = sandbox.N , wrapper = options.wrapper , apiPath = options.apiPath || null , filePath = options.fsPath , allowEJS = options.allowEJS , directory = path.dirname(filePath) , commentMatch = HEADER_COMMENT_PATTERN.exec(source); if (!filePath) { throw new Error('Missed required `fsPath` argument.'); } // Embedded modules must be placed *after* Mincer's comment directives. if (commentMatch) { result.push(source.slice(0, commentMatch[0].length)); source = source.slice(commentMatch[0].length); } // If target file is an EJS template, render it before any processing. if (allowEJS && '.ejs' === path.extname(filePath)) { source = ejs.render(source, { N: N, jetson: jetson.serialize }); } // Find & reqursuvely process `require` directives // findRequires(source, { raw: true }).forEach(function (match) { var resolvedPath, dependencySource; // `require` path cannot be determinated - throw error. if (!match.value) { throw new Error('Error in \'require\': file \'' + filePath + '\', string ' + match.line + '.'); } if (vendorVirtualModules[match.value]) { // Get path to a virtual module. resolvedPath = vendorVirtualModules[match.value]; } else { // Resolve absolute, relative, or node-module path. resolvedPath = resolveModulePath(directory, match.value); } if (!resolvedPath) { throw 'Bundler cannot find required file "' + match.value + '" ' + 'at ' + filePath + ':' + match.point + ':' + match.line; } if (_.has(clientModules, resolvedPath)) { throw 'Require of client block "' + match.value + '" is prohibited ' + 'at ' + filePath + ':' + match.point + ':' + match.line; } // Note: This is not actually safe way to replace `require` paths, but // alternative ways seem be too complicated. In real live that should // work without problems. source = source.replace(match.raw, JSON.stringify(resolvedPath)); // Embed private local modules. (not described in the bundle config and // not embedded yet) if (!_.has(vendorModules, resolvedPath) && !_.has(embeddedModulesPaths[pkgName], resolvedPath)) { embeddedModulesPaths[pkgName] = embeddedModulesPaths[pkgName] || {}; embeddedModulesPaths[pkgName][resolvedPath] = true; // `required` files in vendor modules must be marked as vendor's too if (_.has(vendorModules, filePath)) { vendorModules[resolvedPath] = true; } dependencySource = fs.readFileSync(resolvedPath, 'utf8'); // Recursively browserify result.push(browserifySingle(sandbox, pkgName, dependencySource, { wrapper: WRAPPER_REQUIRE_TEMPLATE , fsPath: resolvedPath , allowEJS: allowEJS })); } }); if (wrapper) { source = wrapper({ path: filePath, apiPath: apiPath, source: source }); } result.push(source); return result.join('\n'); }
function getReqs(thisPath) { var fullPath = path.join('./packages/node_modules', thisPath); return findRequires(fs.readFileSync(fullPath, 'utf8')); }