plugin-loader.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __importDefault = (this && this.__importDefault) || function (mod) {
  16. return (mod && mod.__esModule) ? mod : { "default": mod };
  17. };
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var path_1 = __importDefault(require("path"));
  20. var abstract_plugin_loader_js_1 = __importDefault(require("../less/environment/abstract-plugin-loader.js"));
  21. /**
  22. * Node Plugin Loader
  23. */
  24. var PluginLoader = /** @class */ (function (_super) {
  25. __extends(PluginLoader, _super);
  26. function PluginLoader(less) {
  27. var _this = _super.call(this) || this;
  28. _this.less = less;
  29. _this.require = function (prefix) {
  30. prefix = path_1.default.dirname(prefix);
  31. return function (id) {
  32. var str = id.substr(0, 2);
  33. if (str === '..' || str === './') {
  34. return require(path_1.default.join(prefix, id));
  35. }
  36. else {
  37. return require(id);
  38. }
  39. };
  40. };
  41. return _this;
  42. }
  43. PluginLoader.prototype.loadPlugin = function (filename, basePath, context, environment, fileManager) {
  44. var prefix = filename.slice(0, 1);
  45. var explicit = prefix === '.' || prefix === '/' || filename.slice(-3).toLowerCase() === '.js';
  46. if (!explicit) {
  47. context.prefixes = ['less-plugin-', ''];
  48. }
  49. if (context.syncImport) {
  50. return fileManager.loadFileSync(filename, basePath, context, environment);
  51. }
  52. return new Promise(function (fulfill, reject) {
  53. fileManager.loadFile(filename, basePath, context, environment).then(function (data) {
  54. try {
  55. fulfill(data);
  56. }
  57. catch (e) {
  58. console.log(e);
  59. reject(e);
  60. }
  61. }).catch(function (err) {
  62. reject(err);
  63. });
  64. });
  65. };
  66. PluginLoader.prototype.loadPluginSync = function (filename, basePath, context, environment, fileManager) {
  67. context.syncImport = true;
  68. return this.loadPlugin(filename, basePath, context, environment, fileManager);
  69. };
  70. return PluginLoader;
  71. }(abstract_plugin_loader_js_1.default));
  72. exports.default = PluginLoader;
  73. //# sourceMappingURL=plugin-loader.js.map