plugin-loader.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var path = require("path"),
  2. PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise,
  3. AbstractPluginLoader = require("../less/environment/abstract-plugin-loader.js");
  4. /**
  5. * Node Plugin Loader
  6. */
  7. var PluginLoader = function(less) {
  8. this.less = less;
  9. this.require = require;
  10. this.requireRelative = function(prefix) {
  11. prefix = path.dirname(prefix);
  12. return function(id) {
  13. var str = id.substr(0, 2);
  14. if (str === '..' || str === './') {
  15. return require(path.join(prefix, id));
  16. }
  17. else {
  18. return require(id);
  19. }
  20. };
  21. };
  22. };
  23. PluginLoader.prototype = new AbstractPluginLoader();
  24. PluginLoader.prototype.loadPlugin = function(filename, basePath, context, environment, fileManager) {
  25. var self = this;
  26. var prefix = filename.slice(0, 1);
  27. var explicit = prefix === "." || prefix === "/" || filename.slice(-3).toLowerCase() === ".js";
  28. if (!explicit) {
  29. context.prefixes = ['less-plugin-', ''];
  30. }
  31. return new PromiseConstructor(function(fulfill, reject) {
  32. fileManager.loadFile(filename, basePath, context, environment).then(
  33. function(data) {
  34. try {
  35. self.require = self.requireRelative(data.filename);
  36. fulfill(data);
  37. }
  38. catch (e) {
  39. console.log(e);
  40. reject(e);
  41. }
  42. }
  43. ).catch(function(err) {
  44. reject(err);
  45. });
  46. });
  47. };
  48. module.exports = PluginLoader;