file-manager.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var abstract_file_manager_js_1 = tslib_1.__importDefault(require("../less/environment/abstract-file-manager.js"));
  5. var options;
  6. var logger;
  7. var fileCache = {};
  8. // TODOS - move log somewhere. pathDiff and doing something similar in node. use pathDiff in the other browser file for the initial load
  9. var FileManager = function () { };
  10. FileManager.prototype = Object.assign(new abstract_file_manager_js_1.default(), {
  11. alwaysMakePathsAbsolute: function () {
  12. return true;
  13. },
  14. join: function (basePath, laterPath) {
  15. if (!basePath) {
  16. return laterPath;
  17. }
  18. return this.extractUrlParts(laterPath, basePath).path;
  19. },
  20. doXHR: function (url, type, callback, errback) {
  21. var xhr = new XMLHttpRequest();
  22. var async = options.isFileProtocol ? options.fileAsync : true;
  23. if (typeof xhr.overrideMimeType === 'function') {
  24. xhr.overrideMimeType('text/css');
  25. }
  26. logger.debug("XHR: Getting '" + url + "'");
  27. xhr.open('GET', url, async);
  28. xhr.setRequestHeader('Accept', type || 'text/x-less, text/css; q=0.9, */*; q=0.5');
  29. xhr.send(null);
  30. function handleResponse(xhr, callback, errback) {
  31. if (xhr.status >= 200 && xhr.status < 300) {
  32. callback(xhr.responseText, xhr.getResponseHeader('Last-Modified'));
  33. }
  34. else if (typeof errback === 'function') {
  35. errback(xhr.status, url);
  36. }
  37. }
  38. if (options.isFileProtocol && !options.fileAsync) {
  39. if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) {
  40. callback(xhr.responseText);
  41. }
  42. else {
  43. errback(xhr.status, url);
  44. }
  45. }
  46. else if (async) {
  47. xhr.onreadystatechange = function () {
  48. if (xhr.readyState == 4) {
  49. handleResponse(xhr, callback, errback);
  50. }
  51. };
  52. }
  53. else {
  54. handleResponse(xhr, callback, errback);
  55. }
  56. },
  57. supports: function () {
  58. return true;
  59. },
  60. clearFileCache: function () {
  61. fileCache = {};
  62. },
  63. loadFile: function (filename, currentDirectory, options) {
  64. // TODO: Add prefix support like less-node?
  65. // What about multiple paths?
  66. if (currentDirectory && !this.isPathAbsolute(filename)) {
  67. filename = currentDirectory + filename;
  68. }
  69. filename = options.ext ? this.tryAppendExtension(filename, options.ext) : filename;
  70. options = options || {};
  71. // sheet may be set to the stylesheet for the initial load or a collection of properties including
  72. // some context variables for imports
  73. var hrefParts = this.extractUrlParts(filename, window.location.href);
  74. var href = hrefParts.url;
  75. var self = this;
  76. return new Promise(function (resolve, reject) {
  77. if (options.useFileCache && fileCache[href]) {
  78. try {
  79. var lessText = fileCache[href];
  80. return resolve({ contents: lessText, filename: href, webInfo: { lastModified: new Date() } });
  81. }
  82. catch (e) {
  83. return reject({ filename: href, message: "Error loading file " + href + " error was " + e.message });
  84. }
  85. }
  86. self.doXHR(href, options.mime, function doXHRCallback(data, lastModified) {
  87. // per file cache
  88. fileCache[href] = data;
  89. // Use remote copy (re-parse)
  90. resolve({ contents: data, filename: href, webInfo: { lastModified: lastModified } });
  91. }, function doXHRError(status, url) {
  92. reject({ type: 'File', message: "'" + url + "' wasn't found (" + status + ")", href: href });
  93. });
  94. });
  95. }
  96. });
  97. exports.default = (function (opts, log) {
  98. options = opts;
  99. logger = log;
  100. return FileManager;
  101. });
  102. //# sourceMappingURL=file-manager.js.map