loadLoader.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var LoaderLoadingError = require("./LoaderLoadingError");
  2. module.exports = function loadLoader(loader, callback) {
  3. if(typeof System === "object" && typeof System.import === "function") {
  4. System.import(loader.path).catch(callback).then(function(module) {
  5. loader.normal = typeof module === "function" ? module : module.default;
  6. loader.pitch = module.pitch;
  7. loader.raw = module.raw;
  8. if(typeof loader.normal !== "function" && typeof loader.pitch !== "function") {
  9. return callback(new LoaderLoadingError(
  10. "Module '" + loader.path + "' is not a loader (must have normal or pitch function)"
  11. ));
  12. }
  13. callback();
  14. });
  15. } else {
  16. try {
  17. var module = require(loader.path);
  18. } catch(e) {
  19. // it is possible for node to choke on a require if the FD descriptor
  20. // limit has been reached. give it a chance to recover.
  21. if(e instanceof Error && e.code === "EMFILE") {
  22. var retry = loadLoader.bind(null, loader, callback);
  23. if(typeof setImmediate === "function") {
  24. // node >= 0.9.0
  25. return setImmediate(retry);
  26. } else {
  27. // node < 0.9.0
  28. return process.nextTick(retry);
  29. }
  30. }
  31. return callback(e);
  32. }
  33. if(typeof module !== "function" && typeof module !== "object") {
  34. return callback(new LoaderLoadingError(
  35. "Module '" + loader.path + "' is not a loader (export function or es6 module)"
  36. ));
  37. }
  38. loader.normal = typeof module === "function" ? module : module.default;
  39. loader.pitch = module.pitch;
  40. loader.raw = module.raw;
  41. if(typeof loader.normal !== "function" && typeof loader.pitch !== "function") {
  42. return callback(new LoaderLoadingError(
  43. "Module '" + loader.path + "' is not a loader (must have normal or pitch function)"
  44. ));
  45. }
  46. callback();
  47. }
  48. };