plugin-loader.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import path from 'path';
  2. import AbstractPluginLoader from '../less/environment/abstract-plugin-loader.js';
  3. /**
  4. * Node Plugin Loader
  5. */
  6. class PluginLoader extends AbstractPluginLoader {
  7. constructor(less) {
  8. super();
  9. this.less = less;
  10. this.require = prefix => {
  11. prefix = path.dirname(prefix);
  12. return id => {
  13. const 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. loadPlugin(filename, basePath, context, environment, fileManager) {
  24. const prefix = filename.slice(0, 1);
  25. const explicit = prefix === '.' || prefix === '/' || filename.slice(-3).toLowerCase() === '.js';
  26. if (!explicit) {
  27. context.prefixes = ['less-plugin-', ''];
  28. }
  29. return new Promise((fulfill, reject) => {
  30. fileManager.loadFile(filename, basePath, context, environment).then(
  31. data => {
  32. try {
  33. fulfill(data);
  34. }
  35. catch (e) {
  36. console.log(e);
  37. reject(e);
  38. }
  39. }
  40. ).catch(err => {
  41. reject(err);
  42. });
  43. });
  44. }
  45. }
  46. export default PluginLoader;