a zip code crypto-currency system good for red ONLY

ContextReplacementPlugin.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const path = require("path");
  7. const ContextElementDependency = require("./dependencies/ContextElementDependency");
  8. class ContextReplacementPlugin {
  9. constructor(resourceRegExp, newContentResource, newContentRecursive, newContentRegExp) {
  10. this.resourceRegExp = resourceRegExp;
  11. if(typeof newContentResource === "function") {
  12. this.newContentCallback = newContentResource;
  13. } else if(typeof newContentResource === "string" && typeof newContentRecursive === "object") {
  14. this.newContentResource = newContentResource;
  15. this.newContentCreateContextMap = (fs, callback) => {
  16. callback(null, newContentRecursive);
  17. };
  18. } else if(typeof newContentResource === "string" && typeof newContentRecursive === "function") {
  19. this.newContentResource = newContentResource;
  20. this.newContentCreateContextMap = newContentRecursive;
  21. } else {
  22. if(typeof newContentResource !== "string") {
  23. newContentRegExp = newContentRecursive;
  24. newContentRecursive = newContentResource;
  25. newContentResource = undefined;
  26. }
  27. if(typeof newContentRecursive !== "boolean") {
  28. newContentRegExp = newContentRecursive;
  29. newContentRecursive = undefined;
  30. }
  31. this.newContentResource = newContentResource;
  32. this.newContentRecursive = newContentRecursive;
  33. this.newContentRegExp = newContentRegExp;
  34. }
  35. }
  36. apply(compiler) {
  37. const resourceRegExp = this.resourceRegExp;
  38. const newContentCallback = this.newContentCallback;
  39. const newContentResource = this.newContentResource;
  40. const newContentRecursive = this.newContentRecursive;
  41. const newContentRegExp = this.newContentRegExp;
  42. const newContentCreateContextMap = this.newContentCreateContextMap;
  43. compiler.plugin("context-module-factory", (cmf) => {
  44. cmf.plugin("before-resolve", (result, callback) => {
  45. if(!result) return callback();
  46. if(resourceRegExp.test(result.request)) {
  47. if(typeof newContentResource !== "undefined")
  48. result.request = newContentResource;
  49. if(typeof newContentRecursive !== "undefined")
  50. result.recursive = newContentRecursive;
  51. if(typeof newContentRegExp !== "undefined")
  52. result.regExp = newContentRegExp;
  53. if(typeof newContentCallback === "function") {
  54. newContentCallback(result);
  55. } else {
  56. result.dependencies.forEach((d) => {
  57. if(d.critical)
  58. d.critical = false;
  59. });
  60. }
  61. }
  62. return callback(null, result);
  63. });
  64. cmf.plugin("after-resolve", (result, callback) => {
  65. if(!result) return callback();
  66. if(resourceRegExp.test(result.resource)) {
  67. if(typeof newContentResource !== "undefined")
  68. result.resource = path.resolve(result.resource, newContentResource);
  69. if(typeof newContentRecursive !== "undefined")
  70. result.recursive = newContentRecursive;
  71. if(typeof newContentRegExp !== "undefined")
  72. result.regExp = newContentRegExp;
  73. if(typeof newContentCreateContextMap === "function")
  74. result.resolveDependencies = createResolveDependenciesFromContextMap(newContentCreateContextMap);
  75. if(typeof newContentCallback === "function") {
  76. const origResource = result.resource;
  77. newContentCallback(result);
  78. if(result.resource !== origResource) {
  79. result.resource = path.resolve(origResource, result.resource);
  80. }
  81. } else {
  82. result.dependencies.forEach((d) => {
  83. if(d.critical)
  84. d.critical = false;
  85. });
  86. }
  87. }
  88. return callback(null, result);
  89. });
  90. });
  91. }
  92. }
  93. const createResolveDependenciesFromContextMap = (createContextMap) => {
  94. return function resolveDependenciesFromContextMap(fs, resource, recursive, regExp, callback) {
  95. createContextMap(fs, (err, map) => {
  96. if(err) return callback(err);
  97. const dependencies = Object.keys(map).map((key) => {
  98. return new ContextElementDependency(map[key], key);
  99. });
  100. callback(null, dependencies);
  101. });
  102. };
  103. };
  104. module.exports = ContextReplacementPlugin;