UI for Zipcoin Blue

RequireContextPlugin.js 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RequireContextDependency = require("./RequireContextDependency");
  7. const ContextElementDependency = require("./ContextElementDependency");
  8. const RequireContextDependencyParserPlugin = require("./RequireContextDependencyParserPlugin");
  9. class RequireContextPlugin {
  10. constructor(modulesDirectories, extensions, mainFiles) {
  11. if(!Array.isArray(modulesDirectories))
  12. throw new Error("modulesDirectories must be an array");
  13. if(!Array.isArray(extensions))
  14. throw new Error("extensions must be an array");
  15. this.modulesDirectories = modulesDirectories;
  16. this.extensions = extensions;
  17. this.mainFiles = mainFiles;
  18. }
  19. apply(compiler) {
  20. compiler.plugin("compilation", (compilation, params) => {
  21. const contextModuleFactory = params.contextModuleFactory;
  22. const normalModuleFactory = params.normalModuleFactory;
  23. compilation.dependencyFactories.set(RequireContextDependency, contextModuleFactory);
  24. compilation.dependencyTemplates.set(RequireContextDependency, new RequireContextDependency.Template());
  25. compilation.dependencyFactories.set(ContextElementDependency, normalModuleFactory);
  26. params.normalModuleFactory.plugin("parser", (parser, parserOptions) => {
  27. if(typeof parserOptions.requireContext !== "undefined" && !parserOptions.requireContext)
  28. return;
  29. parser.apply(new RequireContextDependencyParserPlugin());
  30. });
  31. params.contextModuleFactory.plugin("alternatives", (items, callback) => {
  32. if(items.length === 0) return callback(null, items);
  33. callback(null, items.map((obj) => {
  34. return this.extensions.filter((ext) => {
  35. const l = obj.request.length;
  36. return l > ext.length && obj.request.substr(l - ext.length, l) === ext;
  37. }).map((ext) => {
  38. const l = obj.request.length;
  39. return {
  40. context: obj.context,
  41. request: obj.request.substr(0, l - ext.length)
  42. };
  43. }).concat(obj);
  44. }).reduce((a, b) => a.concat(b), []));
  45. });
  46. params.contextModuleFactory.plugin("alternatives", (items, callback) => {
  47. if(items.length === 0) return callback(null, items);
  48. callback(null, items.map((obj) => {
  49. return this.mainFiles.filter((mainFile) => {
  50. const l = obj.request.length;
  51. return l > mainFile.length + 1 && obj.request.substr(l - mainFile.length - 1, l) === "/" + mainFile;
  52. }).map((mainFile) => {
  53. const l = obj.request.length;
  54. return [{
  55. context: obj.context,
  56. request: obj.request.substr(0, l - mainFile.length)
  57. }, {
  58. context: obj.context,
  59. request: obj.request.substr(0, l - mainFile.length - 1)
  60. }];
  61. }).reduce((a, b) => a.concat(b), []).concat(obj);
  62. }).reduce((a, b) => a.concat(b), []));
  63. });
  64. params.contextModuleFactory.plugin("alternatives", (items, callback) => {
  65. if(items.length === 0) return callback(null, items);
  66. callback(null, items.map((obj) => {
  67. for(let i = 0; i < this.modulesDirectories.length; i++) {
  68. const dir = this.modulesDirectories[i];
  69. const idx = obj.request.indexOf("./" + dir + "/");
  70. if(idx === 0) {
  71. obj.request = obj.request.slice(dir.length + 3);
  72. break;
  73. }
  74. }
  75. return obj;
  76. }));
  77. });
  78. });
  79. }
  80. }
  81. module.exports = RequireContextPlugin;