less.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. (function() {
  2. var LessPlugin;
  3. module.exports = LessPlugin = (function() {
  4. LessPlugin.identifier = 'less';
  5. LessPlugin.version = '1.0';
  6. function LessPlugin(window, host) {
  7. this.window = window;
  8. this.host = host;
  9. }
  10. LessPlugin.prototype.reload = function(path, options) {
  11. if (this.window.less && this.window.less.refresh) {
  12. if (path.match(/\.less$/i)) {
  13. return this.reloadLess(path);
  14. }
  15. if (options.originalPath.match(/\.less$/i)) {
  16. return this.reloadLess(options.originalPath);
  17. }
  18. }
  19. return false;
  20. };
  21. LessPlugin.prototype.reloadLess = function(path) {
  22. var i, len, link, links;
  23. links = (function() {
  24. var i, len, ref, results;
  25. ref = document.getElementsByTagName('link');
  26. results = [];
  27. for (i = 0, len = ref.length; i < len; i++) {
  28. link = ref[i];
  29. if (link.href && link.rel.match(/^stylesheet\/less$/i) || (link.rel.match(/stylesheet/i) && link.type.match(/^text\/(x-)?less$/i))) {
  30. results.push(link);
  31. }
  32. }
  33. return results;
  34. })();
  35. if (links.length === 0) {
  36. return false;
  37. }
  38. for (i = 0, len = links.length; i < len; i++) {
  39. link = links[i];
  40. link.href = this.host.generateCacheBustUrl(link.href);
  41. }
  42. this.host.console.log("LiveReload is asking LESS to recompile all stylesheets");
  43. this.window.less.refresh(true);
  44. return true;
  45. };
  46. LessPlugin.prototype.analyze = function() {
  47. return {
  48. disable: !!(this.window.less && this.window.less.refresh)
  49. };
  50. };
  51. return LessPlugin;
  52. })();
  53. }).call(this);