environment.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. /**
  3. * @todo Document why this abstraction exists, and the relationship between
  4. * environment, file managers, and plugin manager
  5. */
  6. var __importDefault = (this && this.__importDefault) || function (mod) {
  7. return (mod && mod.__esModule) ? mod : { "default": mod };
  8. };
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. var logger_1 = __importDefault(require("../logger"));
  11. var environment = /** @class */ (function () {
  12. function environment(externalEnvironment, fileManagers) {
  13. this.fileManagers = fileManagers || [];
  14. externalEnvironment = externalEnvironment || {};
  15. var optionalFunctions = ['encodeBase64', 'mimeLookup', 'charsetLookup', 'getSourceMapGenerator'];
  16. var requiredFunctions = [];
  17. var functions = requiredFunctions.concat(optionalFunctions);
  18. for (var i = 0; i < functions.length; i++) {
  19. var propName = functions[i];
  20. var environmentFunc = externalEnvironment[propName];
  21. if (environmentFunc) {
  22. this[propName] = environmentFunc.bind(externalEnvironment);
  23. }
  24. else if (i < requiredFunctions.length) {
  25. this.warn("missing required function in environment - " + propName);
  26. }
  27. }
  28. }
  29. environment.prototype.getFileManager = function (filename, currentDirectory, options, environment, isSync) {
  30. if (!filename) {
  31. logger_1.default.warn('getFileManager called with no filename.. Please report this issue. continuing.');
  32. }
  33. if (currentDirectory == null) {
  34. logger_1.default.warn('getFileManager called with null directory.. Please report this issue. continuing.');
  35. }
  36. var fileManagers = this.fileManagers;
  37. if (options.pluginManager) {
  38. fileManagers = [].concat(fileManagers).concat(options.pluginManager.getFileManagers());
  39. }
  40. for (var i = fileManagers.length - 1; i >= 0; i--) {
  41. var fileManager = fileManagers[i];
  42. if (fileManager[isSync ? 'supportsSync' : 'supports'](filename, currentDirectory, options, environment)) {
  43. return fileManager;
  44. }
  45. }
  46. return null;
  47. };
  48. environment.prototype.addFileManager = function (fileManager) {
  49. this.fileManagers.push(fileManager);
  50. };
  51. environment.prototype.clearFileManagers = function () {
  52. this.fileManagers = [];
  53. };
  54. return environment;
  55. }());
  56. exports.default = environment;
  57. //# sourceMappingURL=environment.js.map