LoaderRunner.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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) {
  206. if(err) return callback(err);
  207. var args = Array.prototype.slice.call(arguments, 1);
  208. options.resourceBuffer = args[0];
  209. iterateNormalLoaders(options, loaderContext, args, callback);
  210. });
  211. } else {
  212. iterateNormalLoaders(options, loaderContext, [null], callback);
  213. }
  214. }
  215. function iterateNormalLoaders(options, loaderContext, args, callback) {
  216. if(loaderContext.loaderIndex < 0)
  217. return callback(null, args);
  218. var currentLoaderObject = loaderContext.loaders[loaderContext.loaderIndex];
  219. // iterate
  220. if(currentLoaderObject.normalExecuted) {
  221. loaderContext.loaderIndex--;
  222. return iterateNormalLoaders(options, loaderContext, args, callback);
  223. }
  224. var fn = currentLoaderObject.normal;
  225. currentLoaderObject.normalExecuted = true;
  226. if(!fn) {
  227. return iterateNormalLoaders(options, loaderContext, args, callback);
  228. }
  229. convertArgs(args, currentLoaderObject.raw);
  230. runSyncOrAsync(fn, loaderContext, args, function(err) {
  231. if(err) return callback(err);
  232. var args = Array.prototype.slice.call(arguments, 1);
  233. iterateNormalLoaders(options, loaderContext, args, callback);
  234. });
  235. }
  236. exports.getContext = function getContext(resource) {
  237. var path = parsePathQueryFragment(resource).path;
  238. return dirname(path);
  239. };
  240. exports.runLoaders = function runLoaders(options, callback) {
  241. // read options
  242. var resource = options.resource || "";
  243. var loaders = options.loaders || [];
  244. var loaderContext = options.context || {};
  245. var processResource = options.processResource || ((readResource, context, resource, callback) => {
  246. context.addDependency(resource);
  247. readResource(resource, callback);
  248. }).bind(null, options.readResource || readFile);
  249. //
  250. var splittedResource = resource && parsePathQueryFragment(resource);
  251. var resourcePath = splittedResource ? splittedResource.path : undefined;
  252. var resourceQuery = splittedResource ? splittedResource.query : undefined;
  253. var resourceFragment = splittedResource ? splittedResource.fragment : undefined;
  254. var contextDirectory = resourcePath ? dirname(resourcePath) : null;
  255. // execution state
  256. var requestCacheable = true;
  257. var fileDependencies = [];
  258. var contextDependencies = [];
  259. var missingDependencies = [];
  260. // prepare loader objects
  261. loaders = loaders.map(createLoaderObject);
  262. loaderContext.context = contextDirectory;
  263. loaderContext.loaderIndex = 0;
  264. loaderContext.loaders = loaders;
  265. loaderContext.resourcePath = resourcePath;
  266. loaderContext.resourceQuery = resourceQuery;
  267. loaderContext.resourceFragment = resourceFragment;
  268. loaderContext.async = null;
  269. loaderContext.callback = null;
  270. loaderContext.cacheable = function cacheable(flag) {
  271. if(flag === false) {
  272. requestCacheable = false;
  273. }
  274. };
  275. loaderContext.dependency = loaderContext.addDependency = function addDependency(file) {
  276. fileDependencies.push(file);
  277. };
  278. loaderContext.addContextDependency = function addContextDependency(context) {
  279. contextDependencies.push(context);
  280. };
  281. loaderContext.addMissingDependency = function addMissingDependency(context) {
  282. missingDependencies.push(context);
  283. };
  284. loaderContext.getDependencies = function getDependencies() {
  285. return fileDependencies.slice();
  286. };
  287. loaderContext.getContextDependencies = function getContextDependencies() {
  288. return contextDependencies.slice();
  289. };
  290. loaderContext.getMissingDependencies = function getMissingDependencies() {
  291. return missingDependencies.slice();
  292. };
  293. loaderContext.clearDependencies = function clearDependencies() {
  294. fileDependencies.length = 0;
  295. contextDependencies.length = 0;
  296. missingDependencies.length = 0;
  297. requestCacheable = true;
  298. };
  299. Object.defineProperty(loaderContext, "resource", {
  300. enumerable: true,
  301. get: function() {
  302. if(loaderContext.resourcePath === undefined)
  303. return undefined;
  304. return loaderContext.resourcePath.replace(/#/g, "\0#") + loaderContext.resourceQuery.replace(/#/g, "\0#") + loaderContext.resourceFragment;
  305. },
  306. set: function(value) {
  307. var splittedResource = value && parsePathQueryFragment(value);
  308. loaderContext.resourcePath = splittedResource ? splittedResource.path : undefined;
  309. loaderContext.resourceQuery = splittedResource ? splittedResource.query : undefined;
  310. loaderContext.resourceFragment = splittedResource ? splittedResource.fragment : undefined;
  311. }
  312. });
  313. Object.defineProperty(loaderContext, "request", {
  314. enumerable: true,
  315. get: function() {
  316. return loaderContext.loaders.map(function(o) {
  317. return o.request;
  318. }).concat(loaderContext.resource || "").join("!");
  319. }
  320. });
  321. Object.defineProperty(loaderContext, "remainingRequest", {
  322. enumerable: true,
  323. get: function() {
  324. if(loaderContext.loaderIndex >= loaderContext.loaders.length - 1 && !loaderContext.resource)
  325. return "";
  326. return loaderContext.loaders.slice(loaderContext.loaderIndex + 1).map(function(o) {
  327. return o.request;
  328. }).concat(loaderContext.resource || "").join("!");
  329. }
  330. });
  331. Object.defineProperty(loaderContext, "currentRequest", {
  332. enumerable: true,
  333. get: function() {
  334. return loaderContext.loaders.slice(loaderContext.loaderIndex).map(function(o) {
  335. return o.request;
  336. }).concat(loaderContext.resource || "").join("!");
  337. }
  338. });
  339. Object.defineProperty(loaderContext, "previousRequest", {
  340. enumerable: true,
  341. get: function() {
  342. return loaderContext.loaders.slice(0, loaderContext.loaderIndex).map(function(o) {
  343. return o.request;
  344. }).join("!");
  345. }
  346. });
  347. Object.defineProperty(loaderContext, "query", {
  348. enumerable: true,
  349. get: function() {
  350. var entry = loaderContext.loaders[loaderContext.loaderIndex];
  351. return entry.options && typeof entry.options === "object" ? entry.options : entry.query;
  352. }
  353. });
  354. Object.defineProperty(loaderContext, "data", {
  355. enumerable: true,
  356. get: function() {
  357. return loaderContext.loaders[loaderContext.loaderIndex].data;
  358. }
  359. });
  360. // finish loader context
  361. if(Object.preventExtensions) {
  362. Object.preventExtensions(loaderContext);
  363. }
  364. var processOptions = {
  365. resourceBuffer: null,
  366. processResource: processResource
  367. };
  368. iteratePitchingLoaders(processOptions, loaderContext, function(err, result) {
  369. if(err) {
  370. return callback(err, {
  371. cacheable: requestCacheable,
  372. fileDependencies: fileDependencies,
  373. contextDependencies: contextDependencies,
  374. missingDependencies: missingDependencies
  375. });
  376. }
  377. callback(null, {
  378. result: result,
  379. resourceBuffer: processOptions.resourceBuffer,
  380. cacheable: requestCacheable,
  381. fileDependencies: fileDependencies,
  382. contextDependencies: contextDependencies,
  383. missingDependencies: missingDependencies
  384. });
  385. });
  386. };