a zip code crypto-currency system good for red ONLY

ExternalModuleFactoryPlugin.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ExternalModule = require("./ExternalModule");
  7. class ExternalModuleFactoryPlugin {
  8. constructor(type, externals) {
  9. this.type = type;
  10. this.externals = externals;
  11. }
  12. apply(normalModuleFactory) {
  13. const globalType = this.type;
  14. normalModuleFactory.plugin("factory", factory => (data, callback) => {
  15. const context = data.context;
  16. const dependency = data.dependencies[0];
  17. function handleExternal(value, type, callback) {
  18. if(typeof type === "function") {
  19. callback = type;
  20. type = undefined;
  21. }
  22. if(value === false) return factory(data, callback);
  23. if(value === true) value = dependency.request;
  24. if(typeof type === "undefined" && /^[a-z0-9]+ /.test(value)) {
  25. const idx = value.indexOf(" ");
  26. type = value.substr(0, idx);
  27. value = value.substr(idx + 1);
  28. }
  29. callback(null, new ExternalModule(value, type || globalType, dependency.request));
  30. return true;
  31. }
  32. (function handleExternals(externals, callback) {
  33. if(typeof externals === "string") {
  34. if(externals === dependency.request) {
  35. return handleExternal(dependency.request, callback);
  36. }
  37. } else if(Array.isArray(externals)) {
  38. let i = 0;
  39. (function next() {
  40. let asyncFlag;
  41. const handleExternalsAndCallback = function handleExternalsAndCallback(err, module) {
  42. if(err) return callback(err);
  43. if(!module) {
  44. if(asyncFlag) {
  45. asyncFlag = false;
  46. return;
  47. }
  48. return next();
  49. }
  50. callback(null, module);
  51. };
  52. do {
  53. asyncFlag = true;
  54. if(i >= externals.length) return callback();
  55. handleExternals(externals[i++], handleExternalsAndCallback);
  56. } while (!asyncFlag); // eslint-disable-line keyword-spacing
  57. asyncFlag = false;
  58. }());
  59. return;
  60. } else if(externals instanceof RegExp) {
  61. if(externals.test(dependency.request)) {
  62. return handleExternal(dependency.request, callback);
  63. }
  64. } else if(typeof externals === "function") {
  65. externals.call(null, context, dependency.request, function(err, value, type) {
  66. if(err) return callback(err);
  67. if(typeof value !== "undefined") {
  68. handleExternal(value, type, callback);
  69. } else {
  70. callback();
  71. }
  72. });
  73. return;
  74. } else if(typeof externals === "object" && Object.prototype.hasOwnProperty.call(externals, dependency.request)) {
  75. return handleExternal(externals[dependency.request], callback);
  76. }
  77. callback();
  78. }(this.externals, function(err, module) {
  79. if(err) return callback(err);
  80. if(!module) return handleExternal(false, callback);
  81. return callback(null, module);
  82. }));
  83. });
  84. }
  85. }
  86. module.exports = ExternalModuleFactoryPlugin;