plugin-loader.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 = function(prefix) {
  10. prefix = path.dirname(prefix);
  11. return function(id) {
  12. var str = id.substr(0, 2);
  13. if (str === '..' || str === './') {
  14. return require(path.join(prefix, id));
  15. }
  16. else {
  17. return require(id);
  18. }
  19. };
  20. };
  21. };
  22. PluginLoader.prototype = new AbstractPluginLoader();
  23. PluginLoader.prototype.loadPlugin = function(filename, basePath, context, environment, fileManager) {
  24. var prefix = filename.slice(0, 1);
  25. var explicit = prefix === '.' || prefix === '/' || filename.slice(-3).toLowerCase() === '.js';
  26. if (!explicit) {
  27. context.prefixes = ['less-plugin-', ''];
  28. }
  29. return new PromiseConstructor(function(fulfill, reject) {
  30. fileManager.loadFile(filename, basePath, context, environment).then(
  31. function(data) {
  32. try {
  33. fulfill(data);
  34. }
  35. catch (e) {
  36. console.log(e);
  37. reject(e);
  38. }
  39. }
  40. ).catch(function(err) {
  41. reject(err);
  42. });
  43. });
  44. };
  45. module.exports = PluginLoader;