LoaderRunner.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var fs = require("fs");
  6. var readFile = fs.readFile.bind(fs);
  7. var loadLoader = require("./loadLoader");
  8. function utf8BufferToString(buf) {
  9. var str = buf.toString("utf-8");
  10. if(str.charCodeAt(0) === 0xFEFF) {
  11. return str.substr(1);
  12. } else {
  13. return str;
  14. }
  15. }
  16. function splitQuery(req) {
  17. var i = req.indexOf("?");
  18. if(i < 0) return [req, ""];
  19. return [req.substr(0, i), req.substr(i)];
  20. }
  21. function dirname(path) {
  22. if(path === "/") return "/";
  23. var i = path.lastIndexOf("/");
  24. var j = path.lastIndexOf("\\");
  25. var i2 = path.indexOf("/");
  26. var j2 = path.indexOf("\\");
  27. var idx = i > j ? i : j;
  28. var idx2 = i > j ? i2 : j2;
  29. if(idx < 0) return path;
  30. if(idx === idx2) return path.substr(0, idx + 1);
  31. return path.substr(0, idx);
  32. }
  33. function createLoaderObject(loader) {
  34. var obj = {
  35. path: null,
  36. query: null,
  37. options: null,
  38. ident: null,
  39. normal: null,
  40. pitch: null,
  41. raw: null,
  42. data: null,
  43. pitchExecuted: false,
  44. normalExecuted: false
  45. };
  46. Object.defineProperty(obj, "request", {
  47. enumerable: true,
  48. get: function() {
  49. return obj.path + obj.query;
  50. },
  51. set: function(value) {
  52. if(typeof value === "string") {
  53. var splittedRequest = splitQuery(value);
  54. obj.path = splittedRequest[0];
  55. obj.query = splittedRequest[1];
  56. obj.options = undefined;
  57. obj.ident = undefined;
  58. } else {
  59. if(!value.loader)
  60. throw new Error("request should be a string or object with loader and object (" + JSON.stringify(value) + ")");
  61. obj.path = value.loader;
  62. obj.options = value.options;
  63. obj.ident = value.ident;
  64. if(obj.options === null)
  65. obj.query = "";
  66. else if(obj.options === undefined)
  67. obj.query = "";
  68. else if(typeof obj.options === "string")
  69. obj.query = "?" + obj.options;
  70. else if(obj.ident)
  71. obj.query = "??" + obj.ident;
  72. else if(typeof obj.options === "object" && obj.options.ident)
  73. obj.query = "??" + obj.options.ident;
  74. else
  75. obj.query = "?" + JSON.stringify(obj.options);
  76. }
  77. }
  78. });
  79. obj.request = loader;
  80. if(Object.preventExtensions) {
  81. Object.preventExtensions(obj);
  82. }
  83. return obj;
  84. }
  85. function runSyncOrAsync(fn, context, args, callback) {
  86. var isSync = true;
  87. var isDone = false;
  88. var isError = false; // internal error
  89. var reportedError = false;
  90. context.async = function async() {
  91. if(isDone) {
  92. if(reportedError) return; // ignore
  93. throw new Error("async(): The callback was already called.");
  94. }
  95. isSync = false;
  96. return innerCallback;
  97. };
  98. var innerCallback = context.callback = function() {
  99. if(isDone) {
  100. if(reportedError) return; // ignore
  101. throw new Error("callback(): The callback was already called.");
  102. }
  103. isDone = true;
  104. isSync = false;
  105. try {
  106. callback.apply(null, arguments);
  107. } catch(e) {
  108. isError = true;
  109. throw e;
  110. }
  111. };
  112. try {
  113. var result = (function LOADER_EXECUTION() {
  114. return fn.apply(context, args);
  115. }());
  116. if(isSync) {
  117. isDone = true;
  118. if(result === undefined)
  119. return callback();
  120. if(result && typeof result === "object" && typeof result.then === "function") {
  121. return result.catch(callback).then(function(r) {
  122. callback(null, r);
  123. });
  124. }
  125. return callback(null, result);
  126. }
  127. } catch(e) {
  128. if(isError) throw e;
  129. if(isDone) {
  130. // loader is already "done", so we cannot use the callback function
  131. // for better debugging we print the error on the console
  132. if(typeof e === "object" && e.stack) console.error(e.stack);
  133. else console.error(e);
  134. return;
  135. }
  136. isDone = true;
  137. reportedError = true;
  138. callback(e);
  139. }
  140. }
  141. function convertArgs(args, raw) {
  142. if(!raw && Buffer.isBuffer(args[0]))
  143. args[0] = utf8BufferToString(args[0]);
  144. else if(raw && typeof args[0] === "string")
  145. args[0] = new Buffer(args[0], "utf-8"); // eslint-disable-line
  146. }
  147. function iteratePitchingLoaders(options, loaderContext, callback) {
  148. // abort after last loader
  149. if(loaderContext.loaderIndex >= loaderContext.loaders.length)
  150. return processResource(options, loaderContext, callback);
  151. var currentLoaderObject = loaderContext.loaders[loaderContext.loaderIndex];
  152. // iterate
  153. if(currentLoaderObject.pitchExecuted) {
  154. loaderContext.loaderIndex++;
  155. return iteratePitchingLoaders(options, loaderContext, callback);
  156. }
  157. // load loader module
  158. loadLoader(currentLoaderObject, function(err) {
  159. if(err) return callback(err);
  160. var fn = currentLoaderObject.pitch;
  161. currentLoaderObject.pitchExecuted = true;
  162. if(!fn) return iteratePitchingLoaders(options, loaderContext, callback);
  163. runSyncOrAsync(
  164. fn,
  165. loaderContext, [loaderContext.remainingRequest, loaderContext.previousRequest, currentLoaderObject.data = {}],
  166. function(err) {
  167. if(err) return callback(err);
  168. var args = Array.prototype.slice.call(arguments, 1);
  169. if(args.length > 0) {
  170. loaderContext.loaderIndex--;
  171. iterateNormalLoaders(options, loaderContext, args, callback);
  172. } else {
  173. iteratePitchingLoaders(options, loaderContext, callback);
  174. }
  175. }
  176. );
  177. });
  178. }
  179. function processResource(options, loaderContext, callback) {
  180. // set loader index to last loader
  181. loaderContext.loaderIndex = loaderContext.loaders.length - 1;
  182. var resourcePath = loaderContext.resourcePath;
  183. if(resourcePath) {
  184. loaderContext.addDependency(resourcePath);
  185. options.readResource(resourcePath, function(err, buffer) {
  186. if(err) return callback(err);
  187. options.resourceBuffer = buffer;
  188. iterateNormalLoaders(options, loaderContext, [buffer], callback);
  189. });
  190. } else {
  191. iterateNormalLoaders(options, loaderContext, [null], callback);
  192. }
  193. }
  194. function iterateNormalLoaders(options, loaderContext, args, callback) {
  195. if(loaderContext.loaderIndex < 0)
  196. return callback(null, args);
  197. var currentLoaderObject = loaderContext.loaders[loaderContext.loaderIndex];
  198. // iterate
  199. if(currentLoaderObject.normalExecuted) {
  200. loaderContext.loaderIndex--;
  201. return iterateNormalLoaders(options, loaderContext, args, callback);
  202. }
  203. var fn = currentLoaderObject.normal;
  204. currentLoaderObject.normalExecuted = true;
  205. if(!fn) {
  206. return iterateNormalLoaders(options, loaderContext, args, callback);
  207. }
  208. convertArgs(args, currentLoaderObject.raw);
  209. runSyncOrAsync(fn, loaderContext, args, function(err) {
  210. if(err) return callback(err);
  211. var args = Array.prototype.slice.call(arguments, 1);
  212. iterateNormalLoaders(options, loaderContext, args, callback);
  213. });
  214. }
  215. exports.getContext = function getContext(resource) {
  216. var splitted = splitQuery(resource);
  217. return dirname(splitted[0]);
  218. };
  219. exports.runLoaders = function runLoaders(options, callback) {
  220. // read options
  221. var resource = options.resource || "";
  222. var loaders = options.loaders || [];
  223. var loaderContext = options.context || {};
  224. var readResource = options.readResource || readFile;
  225. //
  226. var splittedResource = resource && splitQuery(resource);
  227. var resourcePath = splittedResource ? splittedResource[0] : undefined;
  228. var resourceQuery = splittedResource ? splittedResource[1] : undefined;
  229. var contextDirectory = resourcePath ? dirname(resourcePath) : null;
  230. // execution state
  231. var requestCacheable = true;
  232. var fileDependencies = [];
  233. var contextDependencies = [];
  234. // prepare loader objects
  235. loaders = loaders.map(createLoaderObject);
  236. loaderContext.context = contextDirectory;
  237. loaderContext.loaderIndex = 0;
  238. loaderContext.loaders = loaders;
  239. loaderContext.resourcePath = resourcePath;
  240. loaderContext.resourceQuery = resourceQuery;
  241. loaderContext.async = null;
  242. loaderContext.callback = null;
  243. loaderContext.cacheable = function cacheable(flag) {
  244. if(flag === false) {
  245. requestCacheable = false;
  246. }
  247. };
  248. loaderContext.dependency = loaderContext.addDependency = function addDependency(file) {
  249. fileDependencies.push(file);
  250. };
  251. loaderContext.addContextDependency = function addContextDependency(context) {
  252. contextDependencies.push(context);
  253. };
  254. loaderContext.getDependencies = function getDependencies() {
  255. return fileDependencies.slice();
  256. };
  257. loaderContext.getContextDependencies = function getContextDependencies() {
  258. return contextDependencies.slice();
  259. };
  260. loaderContext.clearDependencies = function clearDependencies() {
  261. fileDependencies.length = 0;
  262. contextDependencies.length = 0;
  263. requestCacheable = true;
  264. };
  265. Object.defineProperty(loaderContext, "resource", {
  266. enumerable: true,
  267. get: function() {
  268. if(loaderContext.resourcePath === undefined)
  269. return undefined;
  270. return loaderContext.resourcePath + loaderContext.resourceQuery;
  271. },
  272. set: function(value) {
  273. var splittedResource = value && splitQuery(value);
  274. loaderContext.resourcePath = splittedResource ? splittedResource[0] : undefined;
  275. loaderContext.resourceQuery = splittedResource ? splittedResource[1] : undefined;
  276. }
  277. });
  278. Object.defineProperty(loaderContext, "request", {
  279. enumerable: true,
  280. get: function() {
  281. return loaderContext.loaders.map(function(o) {
  282. return o.request;
  283. }).concat(loaderContext.resource || "").join("!");
  284. }
  285. });
  286. Object.defineProperty(loaderContext, "remainingRequest", {
  287. enumerable: true,
  288. get: function() {
  289. if(loaderContext.loaderIndex >= loaderContext.loaders.length - 1 && !loaderContext.resource)
  290. return "";
  291. return loaderContext.loaders.slice(loaderContext.loaderIndex + 1).map(function(o) {
  292. return o.request;
  293. }).concat(loaderContext.resource || "").join("!");
  294. }
  295. });
  296. Object.defineProperty(loaderContext, "currentRequest", {
  297. enumerable: true,
  298. get: function() {
  299. return loaderContext.loaders.slice(loaderContext.loaderIndex).map(function(o) {
  300. return o.request;
  301. }).concat(loaderContext.resource || "").join("!");
  302. }
  303. });
  304. Object.defineProperty(loaderContext, "previousRequest", {
  305. enumerable: true,
  306. get: function() {
  307. return loaderContext.loaders.slice(0, loaderContext.loaderIndex).map(function(o) {
  308. return o.request;
  309. }).join("!");
  310. }
  311. });
  312. Object.defineProperty(loaderContext, "query", {
  313. enumerable: true,
  314. get: function() {
  315. var entry = loaderContext.loaders[loaderContext.loaderIndex];
  316. return entry.options && typeof entry.options === "object" ? entry.options : entry.query;
  317. }
  318. });
  319. Object.defineProperty(loaderContext, "data", {
  320. enumerable: true,
  321. get: function() {
  322. return loaderContext.loaders[loaderContext.loaderIndex].data;
  323. }
  324. });
  325. // finish loader context
  326. if(Object.preventExtensions) {
  327. Object.preventExtensions(loaderContext);
  328. }
  329. var processOptions = {
  330. resourceBuffer: null,
  331. readResource: readResource
  332. };
  333. iteratePitchingLoaders(processOptions, loaderContext, function(err, result) {
  334. if(err) {
  335. return callback(err, {
  336. cacheable: requestCacheable,
  337. fileDependencies: fileDependencies,
  338. contextDependencies: contextDependencies
  339. });
  340. }
  341. callback(null, {
  342. result: result,
  343. resourceBuffer: processOptions.resourceBuffer,
  344. cacheable: requestCacheable,
  345. fileDependencies: fileDependencies,
  346. contextDependencies: contextDependencies
  347. });
  348. });
  349. };