LoaderRunner.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.then(function(r) {
  122. callback(null, r);
  123. }, callback);
  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) {
  160. loaderContext.cacheable(false);
  161. return callback(err);
  162. }
  163. var fn = currentLoaderObject.pitch;
  164. currentLoaderObject.pitchExecuted = true;
  165. if(!fn) return iteratePitchingLoaders(options, loaderContext, callback);
  166. runSyncOrAsync(
  167. fn,
  168. loaderContext, [loaderContext.remainingRequest, loaderContext.previousRequest, currentLoaderObject.data = {}],
  169. function(err) {
  170. if(err) return callback(err);
  171. var args = Array.prototype.slice.call(arguments, 1);
  172. if(args.length > 0) {
  173. loaderContext.loaderIndex--;
  174. iterateNormalLoaders(options, loaderContext, args, callback);
  175. } else {
  176. iteratePitchingLoaders(options, loaderContext, callback);
  177. }
  178. }
  179. );
  180. });
  181. }
  182. function processResource(options, loaderContext, callback) {
  183. // set loader index to last loader
  184. loaderContext.loaderIndex = loaderContext.loaders.length - 1;
  185. var resourcePath = loaderContext.resourcePath;
  186. if(resourcePath) {
  187. loaderContext.addDependency(resourcePath);
  188. options.readResource(resourcePath, function(err, buffer) {
  189. if(err) return callback(err);
  190. options.resourceBuffer = buffer;
  191. iterateNormalLoaders(options, loaderContext, [buffer], callback);
  192. });
  193. } else {
  194. iterateNormalLoaders(options, loaderContext, [null], callback);
  195. }
  196. }
  197. function iterateNormalLoaders(options, loaderContext, args, callback) {
  198. if(loaderContext.loaderIndex < 0)
  199. return callback(null, args);
  200. var currentLoaderObject = loaderContext.loaders[loaderContext.loaderIndex];
  201. // iterate
  202. if(currentLoaderObject.normalExecuted) {
  203. loaderContext.loaderIndex--;
  204. return iterateNormalLoaders(options, loaderContext, args, callback);
  205. }
  206. var fn = currentLoaderObject.normal;
  207. currentLoaderObject.normalExecuted = true;
  208. if(!fn) {
  209. return iterateNormalLoaders(options, loaderContext, args, callback);
  210. }
  211. convertArgs(args, currentLoaderObject.raw);
  212. runSyncOrAsync(fn, loaderContext, args, function(err) {
  213. if(err) return callback(err);
  214. var args = Array.prototype.slice.call(arguments, 1);
  215. iterateNormalLoaders(options, loaderContext, args, callback);
  216. });
  217. }
  218. exports.getContext = function getContext(resource) {
  219. var splitted = splitQuery(resource);
  220. return dirname(splitted[0]);
  221. };
  222. exports.runLoaders = function runLoaders(options, callback) {
  223. // read options
  224. var resource = options.resource || "";
  225. var loaders = options.loaders || [];
  226. var loaderContext = options.context || {};
  227. var readResource = options.readResource || readFile;
  228. //
  229. var splittedResource = resource && splitQuery(resource);
  230. var resourcePath = splittedResource ? splittedResource[0] : undefined;
  231. var resourceQuery = splittedResource ? splittedResource[1] : undefined;
  232. var contextDirectory = resourcePath ? dirname(resourcePath) : null;
  233. // execution state
  234. var requestCacheable = true;
  235. var fileDependencies = [];
  236. var contextDependencies = [];
  237. // prepare loader objects
  238. loaders = loaders.map(createLoaderObject);
  239. loaderContext.context = contextDirectory;
  240. loaderContext.loaderIndex = 0;
  241. loaderContext.loaders = loaders;
  242. loaderContext.resourcePath = resourcePath;
  243. loaderContext.resourceQuery = resourceQuery;
  244. loaderContext.async = null;
  245. loaderContext.callback = null;
  246. loaderContext.cacheable = function cacheable(flag) {
  247. if(flag === false) {
  248. requestCacheable = false;
  249. }
  250. };
  251. loaderContext.dependency = loaderContext.addDependency = function addDependency(file) {
  252. fileDependencies.push(file);
  253. };
  254. loaderContext.addContextDependency = function addContextDependency(context) {
  255. contextDependencies.push(context);
  256. };
  257. loaderContext.getDependencies = function getDependencies() {
  258. return fileDependencies.slice();
  259. };
  260. loaderContext.getContextDependencies = function getContextDependencies() {
  261. return contextDependencies.slice();
  262. };
  263. loaderContext.clearDependencies = function clearDependencies() {
  264. fileDependencies.length = 0;
  265. contextDependencies.length = 0;
  266. requestCacheable = true;
  267. };
  268. Object.defineProperty(loaderContext, "resource", {
  269. enumerable: true,
  270. get: function() {
  271. if(loaderContext.resourcePath === undefined)
  272. return undefined;
  273. return loaderContext.resourcePath + loaderContext.resourceQuery;
  274. },
  275. set: function(value) {
  276. var splittedResource = value && splitQuery(value);
  277. loaderContext.resourcePath = splittedResource ? splittedResource[0] : undefined;
  278. loaderContext.resourceQuery = splittedResource ? splittedResource[1] : undefined;
  279. }
  280. });
  281. Object.defineProperty(loaderContext, "request", {
  282. enumerable: true,
  283. get: function() {
  284. return loaderContext.loaders.map(function(o) {
  285. return o.request;
  286. }).concat(loaderContext.resource || "").join("!");
  287. }
  288. });
  289. Object.defineProperty(loaderContext, "remainingRequest", {
  290. enumerable: true,
  291. get: function() {
  292. if(loaderContext.loaderIndex >= loaderContext.loaders.length - 1 && !loaderContext.resource)
  293. return "";
  294. return loaderContext.loaders.slice(loaderContext.loaderIndex + 1).map(function(o) {
  295. return o.request;
  296. }).concat(loaderContext.resource || "").join("!");
  297. }
  298. });
  299. Object.defineProperty(loaderContext, "currentRequest", {
  300. enumerable: true,
  301. get: function() {
  302. return loaderContext.loaders.slice(loaderContext.loaderIndex).map(function(o) {
  303. return o.request;
  304. }).concat(loaderContext.resource || "").join("!");
  305. }
  306. });
  307. Object.defineProperty(loaderContext, "previousRequest", {
  308. enumerable: true,
  309. get: function() {
  310. return loaderContext.loaders.slice(0, loaderContext.loaderIndex).map(function(o) {
  311. return o.request;
  312. }).join("!");
  313. }
  314. });
  315. Object.defineProperty(loaderContext, "query", {
  316. enumerable: true,
  317. get: function() {
  318. var entry = loaderContext.loaders[loaderContext.loaderIndex];
  319. return entry.options && typeof entry.options === "object" ? entry.options : entry.query;
  320. }
  321. });
  322. Object.defineProperty(loaderContext, "data", {
  323. enumerable: true,
  324. get: function() {
  325. return loaderContext.loaders[loaderContext.loaderIndex].data;
  326. }
  327. });
  328. // finish loader context
  329. if(Object.preventExtensions) {
  330. Object.preventExtensions(loaderContext);
  331. }
  332. var processOptions = {
  333. resourceBuffer: null,
  334. readResource: readResource
  335. };
  336. iteratePitchingLoaders(processOptions, loaderContext, function(err, result) {
  337. if(err) {
  338. return callback(err, {
  339. cacheable: requestCacheable,
  340. fileDependencies: fileDependencies,
  341. contextDependencies: contextDependencies
  342. });
  343. }
  344. callback(null, {
  345. result: result,
  346. resourceBuffer: processOptions.resourceBuffer,
  347. cacheable: requestCacheable,
  348. fileDependencies: fileDependencies,
  349. contextDependencies: contextDependencies
  350. });
  351. });
  352. };