LoaderRunner.js 12 KB

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