You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1157 lines
37KB

  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. (function() {
  3. var Connector, PROTOCOL_6, PROTOCOL_7, Parser, Version, _ref;
  4. _ref = require('./protocol'), Parser = _ref.Parser, PROTOCOL_6 = _ref.PROTOCOL_6, PROTOCOL_7 = _ref.PROTOCOL_7;
  5. Version = '2.2.1';
  6. exports.Connector = Connector = (function() {
  7. function Connector(options, WebSocket, Timer, handlers) {
  8. this.options = options;
  9. this.WebSocket = WebSocket;
  10. this.Timer = Timer;
  11. this.handlers = handlers;
  12. this._uri = "ws://" + this.options.host + ":" + this.options.port + "/livereload";
  13. this._nextDelay = this.options.mindelay;
  14. this._connectionDesired = false;
  15. this.protocol = 0;
  16. this.protocolParser = new Parser({
  17. connected: (function(_this) {
  18. return function(protocol) {
  19. _this.protocol = protocol;
  20. _this._handshakeTimeout.stop();
  21. _this._nextDelay = _this.options.mindelay;
  22. _this._disconnectionReason = 'broken';
  23. return _this.handlers.connected(protocol);
  24. };
  25. })(this),
  26. error: (function(_this) {
  27. return function(e) {
  28. _this.handlers.error(e);
  29. return _this._closeOnError();
  30. };
  31. })(this),
  32. message: (function(_this) {
  33. return function(message) {
  34. return _this.handlers.message(message);
  35. };
  36. })(this)
  37. });
  38. this._handshakeTimeout = new Timer((function(_this) {
  39. return function() {
  40. if (!_this._isSocketConnected()) {
  41. return;
  42. }
  43. _this._disconnectionReason = 'handshake-timeout';
  44. return _this.socket.close();
  45. };
  46. })(this));
  47. this._reconnectTimer = new Timer((function(_this) {
  48. return function() {
  49. if (!_this._connectionDesired) {
  50. return;
  51. }
  52. return _this.connect();
  53. };
  54. })(this));
  55. this.connect();
  56. }
  57. Connector.prototype._isSocketConnected = function() {
  58. return this.socket && this.socket.readyState === this.WebSocket.OPEN;
  59. };
  60. Connector.prototype.connect = function() {
  61. this._connectionDesired = true;
  62. if (this._isSocketConnected()) {
  63. return;
  64. }
  65. this._reconnectTimer.stop();
  66. this._disconnectionReason = 'cannot-connect';
  67. this.protocolParser.reset();
  68. this.handlers.connecting();
  69. this.socket = new this.WebSocket(this._uri);
  70. this.socket.onopen = (function(_this) {
  71. return function(e) {
  72. return _this._onopen(e);
  73. };
  74. })(this);
  75. this.socket.onclose = (function(_this) {
  76. return function(e) {
  77. return _this._onclose(e);
  78. };
  79. })(this);
  80. this.socket.onmessage = (function(_this) {
  81. return function(e) {
  82. return _this._onmessage(e);
  83. };
  84. })(this);
  85. return this.socket.onerror = (function(_this) {
  86. return function(e) {
  87. return _this._onerror(e);
  88. };
  89. })(this);
  90. };
  91. Connector.prototype.disconnect = function() {
  92. this._connectionDesired = false;
  93. this._reconnectTimer.stop();
  94. if (!this._isSocketConnected()) {
  95. return;
  96. }
  97. this._disconnectionReason = 'manual';
  98. return this.socket.close();
  99. };
  100. Connector.prototype._scheduleReconnection = function() {
  101. if (!this._connectionDesired) {
  102. return;
  103. }
  104. if (!this._reconnectTimer.running) {
  105. this._reconnectTimer.start(this._nextDelay);
  106. return this._nextDelay = Math.min(this.options.maxdelay, this._nextDelay * 2);
  107. }
  108. };
  109. Connector.prototype.sendCommand = function(command) {
  110. if (this.protocol == null) {
  111. return;
  112. }
  113. return this._sendCommand(command);
  114. };
  115. Connector.prototype._sendCommand = function(command) {
  116. return this.socket.send(JSON.stringify(command));
  117. };
  118. Connector.prototype._closeOnError = function() {
  119. this._handshakeTimeout.stop();
  120. this._disconnectionReason = 'error';
  121. return this.socket.close();
  122. };
  123. Connector.prototype._onopen = function(e) {
  124. var hello;
  125. this.handlers.socketConnected();
  126. this._disconnectionReason = 'handshake-failed';
  127. hello = {
  128. command: 'hello',
  129. protocols: [PROTOCOL_6, PROTOCOL_7]
  130. };
  131. hello.ver = Version;
  132. if (this.options.ext) {
  133. hello.ext = this.options.ext;
  134. }
  135. if (this.options.extver) {
  136. hello.extver = this.options.extver;
  137. }
  138. if (this.options.snipver) {
  139. hello.snipver = this.options.snipver;
  140. }
  141. this._sendCommand(hello);
  142. return this._handshakeTimeout.start(this.options.handshake_timeout);
  143. };
  144. Connector.prototype._onclose = function(e) {
  145. this.protocol = 0;
  146. this.handlers.disconnected(this._disconnectionReason, this._nextDelay);
  147. return this._scheduleReconnection();
  148. };
  149. Connector.prototype._onerror = function(e) {};
  150. Connector.prototype._onmessage = function(e) {
  151. return this.protocolParser.process(e.data);
  152. };
  153. return Connector;
  154. })();
  155. }).call(this);
  156. },{"./protocol":6}],2:[function(require,module,exports){
  157. (function() {
  158. var CustomEvents;
  159. CustomEvents = {
  160. bind: function(element, eventName, handler) {
  161. if (element.addEventListener) {
  162. return element.addEventListener(eventName, handler, false);
  163. } else if (element.attachEvent) {
  164. element[eventName] = 1;
  165. return element.attachEvent('onpropertychange', function(event) {
  166. if (event.propertyName === eventName) {
  167. return handler();
  168. }
  169. });
  170. } else {
  171. throw new Error("Attempt to attach custom event " + eventName + " to something which isn't a DOMElement");
  172. }
  173. },
  174. fire: function(element, eventName) {
  175. var event;
  176. if (element.addEventListener) {
  177. event = document.createEvent('HTMLEvents');
  178. event.initEvent(eventName, true, true);
  179. return document.dispatchEvent(event);
  180. } else if (element.attachEvent) {
  181. if (element[eventName]) {
  182. return element[eventName]++;
  183. }
  184. } else {
  185. throw new Error("Attempt to fire custom event " + eventName + " on something which isn't a DOMElement");
  186. }
  187. }
  188. };
  189. exports.bind = CustomEvents.bind;
  190. exports.fire = CustomEvents.fire;
  191. }).call(this);
  192. },{}],3:[function(require,module,exports){
  193. (function() {
  194. var LessPlugin;
  195. module.exports = LessPlugin = (function() {
  196. LessPlugin.identifier = 'less';
  197. LessPlugin.version = '1.0';
  198. function LessPlugin(window, host) {
  199. this.window = window;
  200. this.host = host;
  201. }
  202. LessPlugin.prototype.reload = function(path, options) {
  203. if (this.window.less && this.window.less.refresh) {
  204. if (path.match(/\.less$/i)) {
  205. return this.reloadLess(path);
  206. }
  207. if (options.originalPath.match(/\.less$/i)) {
  208. return this.reloadLess(options.originalPath);
  209. }
  210. }
  211. return false;
  212. };
  213. LessPlugin.prototype.reloadLess = function(path) {
  214. var link, links, _i, _len;
  215. links = (function() {
  216. var _i, _len, _ref, _results;
  217. _ref = document.getElementsByTagName('link');
  218. _results = [];
  219. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  220. link = _ref[_i];
  221. if (link.href && link.rel.match(/^stylesheet\/less$/i) || (link.rel.match(/stylesheet/i) && link.type.match(/^text\/(x-)?less$/i))) {
  222. _results.push(link);
  223. }
  224. }
  225. return _results;
  226. })();
  227. if (links.length === 0) {
  228. return false;
  229. }
  230. for (_i = 0, _len = links.length; _i < _len; _i++) {
  231. link = links[_i];
  232. link.href = this.host.generateCacheBustUrl(link.href);
  233. }
  234. this.host.console.log("LiveReload is asking LESS to recompile all stylesheets");
  235. this.window.less.refresh(true);
  236. return true;
  237. };
  238. LessPlugin.prototype.analyze = function() {
  239. return {
  240. disable: !!(this.window.less && this.window.less.refresh)
  241. };
  242. };
  243. return LessPlugin;
  244. })();
  245. }).call(this);
  246. },{}],4:[function(require,module,exports){
  247. (function() {
  248. var Connector, LiveReload, Options, Reloader, Timer;
  249. Connector = require('./connector').Connector;
  250. Timer = require('./timer').Timer;
  251. Options = require('./options').Options;
  252. Reloader = require('./reloader').Reloader;
  253. exports.LiveReload = LiveReload = (function() {
  254. function LiveReload(window) {
  255. this.window = window;
  256. this.listeners = {};
  257. this.plugins = [];
  258. this.pluginIdentifiers = {};
  259. this.console = this.window.location.href.match(/LR-verbose/) && this.window.console && this.window.console.log && this.window.console.error ? this.window.console : {
  260. log: function() {},
  261. error: function() {}
  262. };
  263. if (!(this.WebSocket = this.window.WebSocket || this.window.MozWebSocket)) {
  264. this.console.error("LiveReload disabled because the browser does not seem to support web sockets");
  265. return;
  266. }
  267. if (!(this.options = Options.extract(this.window.document))) {
  268. this.console.error("LiveReload disabled because it could not find its own <SCRIPT> tag");
  269. return;
  270. }
  271. this.reloader = new Reloader(this.window, this.console, Timer);
  272. this.connector = new Connector(this.options, this.WebSocket, Timer, {
  273. connecting: (function(_this) {
  274. return function() {};
  275. })(this),
  276. socketConnected: (function(_this) {
  277. return function() {};
  278. })(this),
  279. connected: (function(_this) {
  280. return function(protocol) {
  281. var _base;
  282. if (typeof (_base = _this.listeners).connect === "function") {
  283. _base.connect();
  284. }
  285. _this.log("LiveReload is connected to " + _this.options.host + ":" + _this.options.port + " (protocol v" + protocol + ").");
  286. return _this.analyze();
  287. };
  288. })(this),
  289. error: (function(_this) {
  290. return function(e) {
  291. console.log(e);
  292. // if (e instanceof ProtocolError) {
  293. // if (typeof console !== "undefined" && console !== null) {
  294. // return console.log("" + e.message + ".");
  295. // }
  296. // } else {
  297. // if (typeof console !== "undefined" && console !== null) {
  298. // return console.log("LiveReload internal error: " + e.message);
  299. // }
  300. // }
  301. };
  302. })(this),
  303. disconnected: (function(_this) {
  304. return function(reason, nextDelay) {
  305. var _base;
  306. if (typeof (_base = _this.listeners).disconnect === "function") {
  307. _base.disconnect();
  308. }
  309. switch (reason) {
  310. case 'cannot-connect':
  311. return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + ", will retry in " + nextDelay + " sec.");
  312. case 'broken':
  313. return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + ", reconnecting in " + nextDelay + " sec.");
  314. case 'handshake-timeout':
  315. return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake timeout), will retry in " + nextDelay + " sec.");
  316. case 'handshake-failed':
  317. return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake failed), will retry in " + nextDelay + " sec.");
  318. case 'manual':
  319. break;
  320. case 'error':
  321. break;
  322. default:
  323. return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + " (" + reason + "), reconnecting in " + nextDelay + " sec.");
  324. }
  325. };
  326. })(this),
  327. message: (function(_this) {
  328. return function(message) {
  329. switch (message.command) {
  330. case 'reload':
  331. return _this.performReload(message);
  332. case 'alert':
  333. return _this.performAlert(message);
  334. }
  335. };
  336. })(this)
  337. });
  338. }
  339. LiveReload.prototype.on = function(eventName, handler) {
  340. return this.listeners[eventName] = handler;
  341. };
  342. LiveReload.prototype.log = function(message) {
  343. return this.console.log("" + message);
  344. };
  345. LiveReload.prototype.performReload = function(message) {
  346. var _ref, _ref1;
  347. this.log("LiveReload received reload request: " + (JSON.stringify(message, null, 2)));
  348. return this.reloader.reload(message.path, {
  349. liveCSS: (_ref = message.liveCSS) != null ? _ref : true,
  350. liveImg: (_ref1 = message.liveImg) != null ? _ref1 : true,
  351. originalPath: message.originalPath || '',
  352. overrideURL: message.overrideURL || '',
  353. serverURL: "http://" + this.options.host + ":" + this.options.port
  354. });
  355. };
  356. LiveReload.prototype.performAlert = function(message) {
  357. return alert(message.message);
  358. };
  359. LiveReload.prototype.shutDown = function() {
  360. var _base;
  361. this.connector.disconnect();
  362. this.log("LiveReload disconnected.");
  363. return typeof (_base = this.listeners).shutdown === "function" ? _base.shutdown() : void 0;
  364. };
  365. LiveReload.prototype.hasPlugin = function(identifier) {
  366. return !!this.pluginIdentifiers[identifier];
  367. };
  368. LiveReload.prototype.addPlugin = function(pluginClass) {
  369. var plugin;
  370. if (this.hasPlugin(pluginClass.identifier)) {
  371. return;
  372. }
  373. this.pluginIdentifiers[pluginClass.identifier] = true;
  374. plugin = new pluginClass(this.window, {
  375. _livereload: this,
  376. _reloader: this.reloader,
  377. _connector: this.connector,
  378. console: this.console,
  379. Timer: Timer,
  380. generateCacheBustUrl: (function(_this) {
  381. return function(url) {
  382. return _this.reloader.generateCacheBustUrl(url);
  383. };
  384. })(this)
  385. });
  386. this.plugins.push(plugin);
  387. this.reloader.addPlugin(plugin);
  388. };
  389. LiveReload.prototype.analyze = function() {
  390. var plugin, pluginData, pluginsData, _i, _len, _ref;
  391. if (!(this.connector.protocol >= 7)) {
  392. return;
  393. }
  394. pluginsData = {};
  395. _ref = this.plugins;
  396. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  397. plugin = _ref[_i];
  398. pluginsData[plugin.constructor.identifier] = pluginData = (typeof plugin.analyze === "function" ? plugin.analyze() : void 0) || {};
  399. pluginData.version = plugin.constructor.version;
  400. }
  401. this.connector.sendCommand({
  402. command: 'info',
  403. plugins: pluginsData,
  404. url: this.window.location.href
  405. });
  406. };
  407. return LiveReload;
  408. })();
  409. }).call(this);
  410. },{"./connector":1,"./options":5,"./reloader":7,"./timer":9}],5:[function(require,module,exports){
  411. (function() {
  412. var Options;
  413. exports.Options = Options = (function() {
  414. function Options() {
  415. this.host = null;
  416. this.port = 35729;
  417. this.snipver = null;
  418. this.ext = null;
  419. this.extver = null;
  420. this.mindelay = 1000;
  421. this.maxdelay = 60000;
  422. this.handshake_timeout = 5000;
  423. }
  424. Options.prototype.set = function(name, value) {
  425. if (typeof value === 'undefined') {
  426. return;
  427. }
  428. if (!isNaN(+value)) {
  429. value = +value;
  430. }
  431. return this[name] = value;
  432. };
  433. return Options;
  434. })();
  435. Options.extract = function(document) {
  436. var element, keyAndValue, m, mm, options, pair, src, _i, _j, _len, _len1, _ref, _ref1;
  437. _ref = document.getElementsByTagName('script');
  438. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  439. element = _ref[_i];
  440. if ((src = element.src) && (m = src.match(/^[^:]+:\/\/(.*)\/z?livereload\.js(?:\?(.*))?$/))) {
  441. options = new Options();
  442. if (mm = m[1].match(/^([^\/:]+)(?::(\d+))?$/)) {
  443. options.host = mm[1];
  444. if (mm[2]) {
  445. options.port = parseInt(mm[2], 10);
  446. }
  447. }
  448. if (m[2]) {
  449. _ref1 = m[2].split('&');
  450. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  451. pair = _ref1[_j];
  452. if ((keyAndValue = pair.split('=')).length > 1) {
  453. options.set(keyAndValue[0].replace(/-/g, '_'), keyAndValue.slice(1).join('='));
  454. }
  455. }
  456. }
  457. return options;
  458. }
  459. }
  460. return null;
  461. };
  462. }).call(this);
  463. },{}],6:[function(require,module,exports){
  464. (function() {
  465. var PROTOCOL_6, PROTOCOL_7, Parser, ProtocolError,
  466. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  467. exports.PROTOCOL_6 = PROTOCOL_6 = 'http://livereload.com/protocols/official-6';
  468. exports.PROTOCOL_7 = PROTOCOL_7 = 'http://livereload.com/protocols/official-7';
  469. exports.ProtocolError = ProtocolError = (function() {
  470. function ProtocolError(reason, data) {
  471. this.message = "LiveReload protocol error (" + reason + ") after receiving data: \"" + data + "\".";
  472. }
  473. return ProtocolError;
  474. })();
  475. exports.Parser = Parser = (function() {
  476. function Parser(handlers) {
  477. this.handlers = handlers;
  478. this.reset();
  479. }
  480. Parser.prototype.reset = function() {
  481. return this.protocol = null;
  482. };
  483. Parser.prototype.process = function(data) {
  484. var command, e, message, options, _ref;
  485. try {
  486. if (this.protocol == null) {
  487. if (data.match(/^!!ver:([\d.]+)$/)) {
  488. this.protocol = 6;
  489. } else if (message = this._parseMessage(data, ['hello'])) {
  490. if (!message.protocols.length) {
  491. throw new ProtocolError("no protocols specified in handshake message");
  492. } else if (__indexOf.call(message.protocols, PROTOCOL_7) >= 0) {
  493. this.protocol = 7;
  494. } else if (__indexOf.call(message.protocols, PROTOCOL_6) >= 0) {
  495. this.protocol = 6;
  496. } else {
  497. throw new ProtocolError("no supported protocols found");
  498. }
  499. }
  500. return this.handlers.connected(this.protocol);
  501. } else if (this.protocol === 6) {
  502. message = JSON.parse(data);
  503. if (!message.length) {
  504. throw new ProtocolError("protocol 6 messages must be arrays");
  505. }
  506. command = message[0], options = message[1];
  507. if (command !== 'refresh') {
  508. throw new ProtocolError("unknown protocol 6 command");
  509. }
  510. return this.handlers.message({
  511. command: 'reload',
  512. path: options.path,
  513. liveCSS: (_ref = options.apply_css_live) != null ? _ref : true
  514. });
  515. } else {
  516. message = this._parseMessage(data, ['reload', 'alert']);
  517. return this.handlers.message(message);
  518. }
  519. } catch (_error) {
  520. e = _error;
  521. if (e instanceof ProtocolError) {
  522. return this.handlers.error(e);
  523. } else {
  524. throw e;
  525. }
  526. }
  527. };
  528. Parser.prototype._parseMessage = function(data, validCommands) {
  529. var e, message, _ref;
  530. try {
  531. message = JSON.parse(data);
  532. } catch (_error) {
  533. e = _error;
  534. throw new ProtocolError('unparsable JSON', data);
  535. }
  536. if (!message.command) {
  537. throw new ProtocolError('missing "command" key', data);
  538. }
  539. if (_ref = message.command, __indexOf.call(validCommands, _ref) < 0) {
  540. throw new ProtocolError("invalid command '" + message.command + "', only valid commands are: " + (validCommands.join(', ')) + ")", data);
  541. }
  542. return message;
  543. };
  544. return Parser;
  545. })();
  546. }).call(this);
  547. },{}],7:[function(require,module,exports){
  548. (function() {
  549. var IMAGE_STYLES, Reloader, numberOfMatchingSegments, pathFromUrl, pathsMatch, pickBestMatch, splitUrl;
  550. splitUrl = function(url) {
  551. var hash, index, params;
  552. if ((index = url.indexOf('#')) >= 0) {
  553. hash = url.slice(index);
  554. url = url.slice(0, index);
  555. } else {
  556. hash = '';
  557. }
  558. if ((index = url.indexOf('?')) >= 0) {
  559. params = url.slice(index);
  560. url = url.slice(0, index);
  561. } else {
  562. params = '';
  563. }
  564. return {
  565. url: url,
  566. params: params,
  567. hash: hash
  568. };
  569. };
  570. pathFromUrl = function(url) {
  571. var path;
  572. url = splitUrl(url).url;
  573. if (url.indexOf('file://') === 0) {
  574. path = url.replace(/^file:\/\/(localhost)?/, '');
  575. } else {
  576. path = url.replace(/^([^:]+:)?\/\/([^:\/]+)(:\d*)?\//, '/');
  577. }
  578. return decodeURIComponent(path);
  579. };
  580. pickBestMatch = function(path, objects, pathFunc) {
  581. var bestMatch, object, score, _i, _len;
  582. bestMatch = {
  583. score: 0
  584. };
  585. for (_i = 0, _len = objects.length; _i < _len; _i++) {
  586. object = objects[_i];
  587. score = numberOfMatchingSegments(path, pathFunc(object));
  588. if (score > bestMatch.score) {
  589. bestMatch = {
  590. object: object,
  591. score: score
  592. };
  593. }
  594. }
  595. if (bestMatch.score > 0) {
  596. return bestMatch;
  597. } else {
  598. return null;
  599. }
  600. };
  601. numberOfMatchingSegments = function(path1, path2) {
  602. var comps1, comps2, eqCount, len;
  603. path1 = path1.replace(/^\/+/, '').toLowerCase();
  604. path2 = path2.replace(/^\/+/, '').toLowerCase();
  605. if (path1 === path2) {
  606. return 10000;
  607. }
  608. comps1 = path1.split('/').reverse();
  609. comps2 = path2.split('/').reverse();
  610. len = Math.min(comps1.length, comps2.length);
  611. eqCount = 0;
  612. while (eqCount < len && comps1[eqCount] === comps2[eqCount]) {
  613. ++eqCount;
  614. }
  615. return eqCount;
  616. };
  617. pathsMatch = function(path1, path2) {
  618. return numberOfMatchingSegments(path1, path2) > 0;
  619. };
  620. IMAGE_STYLES = [
  621. {
  622. selector: 'background',
  623. styleNames: ['backgroundImage']
  624. }, {
  625. selector: 'border',
  626. styleNames: ['borderImage', 'webkitBorderImage', 'MozBorderImage']
  627. }
  628. ];
  629. exports.Reloader = Reloader = (function() {
  630. function Reloader(window, console, Timer) {
  631. this.window = window;
  632. this.console = console;
  633. this.Timer = Timer;
  634. this.document = this.window.document;
  635. this.importCacheWaitPeriod = 200;
  636. this.plugins = [];
  637. }
  638. Reloader.prototype.addPlugin = function(plugin) {
  639. return this.plugins.push(plugin);
  640. };
  641. Reloader.prototype.analyze = function(callback) {
  642. return results;
  643. };
  644. Reloader.prototype.reload = function(path, options) {
  645. var plugin, _base, _i, _len, _ref;
  646. this.options = options;
  647. if ((_base = this.options).stylesheetReloadTimeout == null) {
  648. _base.stylesheetReloadTimeout = 15000;
  649. }
  650. _ref = this.plugins;
  651. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  652. plugin = _ref[_i];
  653. if (plugin.reload && plugin.reload(path, options)) {
  654. return;
  655. }
  656. }
  657. if (options.liveCSS) {
  658. if (path.match(/\.css$/i)) {
  659. if (this.reloadStylesheet(path)) {
  660. return;
  661. }
  662. }
  663. }
  664. if (options.liveImg) {
  665. if (path.match(/\.(jpe?g|png|gif)$/i)) {
  666. this.reloadImages(path);
  667. return;
  668. }
  669. }
  670. return this.reloadPage();
  671. };
  672. Reloader.prototype.reloadPage = function() {
  673. return this.window.document.location.reload();
  674. };
  675. Reloader.prototype.reloadImages = function(path) {
  676. var expando, img, selector, styleNames, styleSheet, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3, _results;
  677. expando = this.generateUniqueString();
  678. _ref = this.document.images;
  679. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  680. img = _ref[_i];
  681. if (pathsMatch(path, pathFromUrl(img.src))) {
  682. img.src = this.generateCacheBustUrl(img.src, expando);
  683. }
  684. }
  685. if (this.document.querySelectorAll) {
  686. for (_j = 0, _len1 = IMAGE_STYLES.length; _j < _len1; _j++) {
  687. _ref1 = IMAGE_STYLES[_j], selector = _ref1.selector, styleNames = _ref1.styleNames;
  688. _ref2 = this.document.querySelectorAll("[style*=" + selector + "]");
  689. for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
  690. img = _ref2[_k];
  691. this.reloadStyleImages(img.style, styleNames, path, expando);
  692. }
  693. }
  694. }
  695. if (this.document.styleSheets) {
  696. _ref3 = this.document.styleSheets;
  697. _results = [];
  698. for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
  699. styleSheet = _ref3[_l];
  700. _results.push(this.reloadStylesheetImages(styleSheet, path, expando));
  701. }
  702. return _results;
  703. }
  704. };
  705. Reloader.prototype.reloadStylesheetImages = function(styleSheet, path, expando) {
  706. var e, rule, rules, styleNames, _i, _j, _len, _len1;
  707. try {
  708. rules = styleSheet != null ? styleSheet.cssRules : void 0;
  709. } catch (_error) {
  710. e = _error;
  711. }
  712. if (!rules) {
  713. return;
  714. }
  715. for (_i = 0, _len = rules.length; _i < _len; _i++) {
  716. rule = rules[_i];
  717. switch (rule.type) {
  718. case CSSRule.IMPORT_RULE:
  719. this.reloadStylesheetImages(rule.styleSheet, path, expando);
  720. break;
  721. case CSSRule.STYLE_RULE:
  722. for (_j = 0, _len1 = IMAGE_STYLES.length; _j < _len1; _j++) {
  723. styleNames = IMAGE_STYLES[_j].styleNames;
  724. this.reloadStyleImages(rule.style, styleNames, path, expando);
  725. }
  726. break;
  727. case CSSRule.MEDIA_RULE:
  728. this.reloadStylesheetImages(rule, path, expando);
  729. }
  730. }
  731. };
  732. Reloader.prototype.reloadStyleImages = function(style, styleNames, path, expando) {
  733. var newValue, styleName, value, _i, _len;
  734. for (_i = 0, _len = styleNames.length; _i < _len; _i++) {
  735. styleName = styleNames[_i];
  736. value = style[styleName];
  737. if (typeof value === 'string') {
  738. newValue = value.replace(/\burl\s*\(([^)]*)\)/, (function(_this) {
  739. return function(match, src) {
  740. if (pathsMatch(path, pathFromUrl(src))) {
  741. return "url(" + (_this.generateCacheBustUrl(src, expando)) + ")";
  742. } else {
  743. return match;
  744. }
  745. };
  746. })(this));
  747. if (newValue !== value) {
  748. style[styleName] = newValue;
  749. }
  750. }
  751. }
  752. };
  753. Reloader.prototype.reloadStylesheet = function(path) {
  754. var imported, link, links, match, style, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1;
  755. links = (function() {
  756. var _i, _len, _ref, _results;
  757. _ref = this.document.getElementsByTagName('link');
  758. _results = [];
  759. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  760. link = _ref[_i];
  761. if (link.rel.match(/^stylesheet$/i) && !link.__LiveReload_pendingRemoval) {
  762. _results.push(link);
  763. }
  764. }
  765. return _results;
  766. }).call(this);
  767. imported = [];
  768. _ref = this.document.getElementsByTagName('style');
  769. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  770. style = _ref[_i];
  771. if (style.sheet) {
  772. this.collectImportedStylesheets(style, style.sheet, imported);
  773. }
  774. }
  775. for (_j = 0, _len1 = links.length; _j < _len1; _j++) {
  776. link = links[_j];
  777. this.collectImportedStylesheets(link, link.sheet, imported);
  778. }
  779. if (this.window.StyleFix && this.document.querySelectorAll) {
  780. _ref1 = this.document.querySelectorAll('style[data-href]');
  781. for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
  782. style = _ref1[_k];
  783. links.push(style);
  784. }
  785. }
  786. this.console.log("LiveReload found " + links.length + " LINKed stylesheets, " + imported.length + " @imported stylesheets");
  787. match = pickBestMatch(path, links.concat(imported), (function(_this) {
  788. return function(l) {
  789. return pathFromUrl(_this.linkHref(l));
  790. };
  791. })(this));
  792. if (match) {
  793. if (match.object.rule) {
  794. this.console.log("LiveReload is reloading imported stylesheet: " + match.object.href);
  795. this.reattachImportedRule(match.object);
  796. } else {
  797. this.console.log("LiveReload is reloading stylesheet: " + (this.linkHref(match.object)));
  798. this.reattachStylesheetLink(match.object);
  799. }
  800. } else {
  801. this.console.log("LiveReload will reload all stylesheets because path '" + path + "' did not match any specific one");
  802. for (_l = 0, _len3 = links.length; _l < _len3; _l++) {
  803. link = links[_l];
  804. this.reattachStylesheetLink(link);
  805. }
  806. }
  807. return true;
  808. };
  809. Reloader.prototype.collectImportedStylesheets = function(link, styleSheet, result) {
  810. var e, index, rule, rules, _i, _len;
  811. try {
  812. rules = styleSheet != null ? styleSheet.cssRules : void 0;
  813. } catch (_error) {
  814. e = _error;
  815. }
  816. if (rules && rules.length) {
  817. for (index = _i = 0, _len = rules.length; _i < _len; index = ++_i) {
  818. rule = rules[index];
  819. switch (rule.type) {
  820. case CSSRule.CHARSET_RULE:
  821. continue;
  822. case CSSRule.IMPORT_RULE:
  823. result.push({
  824. link: link,
  825. rule: rule,
  826. index: index,
  827. href: rule.href
  828. });
  829. this.collectImportedStylesheets(link, rule.styleSheet, result);
  830. break;
  831. default:
  832. break;
  833. }
  834. }
  835. }
  836. };
  837. Reloader.prototype.waitUntilCssLoads = function(clone, func) {
  838. var callbackExecuted, executeCallback, poll;
  839. callbackExecuted = false;
  840. executeCallback = (function(_this) {
  841. return function() {
  842. if (callbackExecuted) {
  843. return;
  844. }
  845. callbackExecuted = true;
  846. return func();
  847. };
  848. })(this);
  849. clone.onload = (function(_this) {
  850. return function() {
  851. _this.console.log("LiveReload: the new stylesheet has finished loading");
  852. _this.knownToSupportCssOnLoad = true;
  853. return executeCallback();
  854. };
  855. })(this);
  856. if (!this.knownToSupportCssOnLoad) {
  857. (poll = (function(_this) {
  858. return function() {
  859. if (clone.sheet) {
  860. _this.console.log("LiveReload is polling until the new CSS finishes loading...");
  861. return executeCallback();
  862. } else {
  863. return _this.Timer.start(50, poll);
  864. }
  865. };
  866. })(this))();
  867. }
  868. return this.Timer.start(this.options.stylesheetReloadTimeout, executeCallback);
  869. };
  870. Reloader.prototype.linkHref = function(link) {
  871. return link.href || link.getAttribute('data-href');
  872. };
  873. Reloader.prototype.reattachStylesheetLink = function(link) {
  874. var clone, parent;
  875. if (link.__LiveReload_pendingRemoval) {
  876. return;
  877. }
  878. link.__LiveReload_pendingRemoval = true;
  879. if (link.tagName === 'STYLE') {
  880. clone = this.document.createElement('link');
  881. clone.rel = 'stylesheet';
  882. clone.media = link.media;
  883. clone.disabled = link.disabled;
  884. } else {
  885. clone = link.cloneNode(false);
  886. }
  887. clone.href = this.generateCacheBustUrl(this.linkHref(link));
  888. parent = link.parentNode;
  889. if (parent.lastChild === link) {
  890. parent.appendChild(clone);
  891. } else {
  892. parent.insertBefore(clone, link.nextSibling);
  893. }
  894. return this.waitUntilCssLoads(clone, (function(_this) {
  895. return function() {
  896. var additionalWaitingTime;
  897. if (/AppleWebKit/.test(navigator.userAgent)) {
  898. additionalWaitingTime = 5;
  899. } else {
  900. additionalWaitingTime = 200;
  901. }
  902. return _this.Timer.start(additionalWaitingTime, function() {
  903. var _ref;
  904. if (!link.parentNode) {
  905. return;
  906. }
  907. link.parentNode.removeChild(link);
  908. clone.onreadystatechange = null;
  909. return (_ref = _this.window.StyleFix) != null ? _ref.link(clone) : void 0;
  910. });
  911. };
  912. })(this));
  913. };
  914. Reloader.prototype.reattachImportedRule = function(_arg) {
  915. var href, index, link, media, newRule, parent, rule, tempLink;
  916. rule = _arg.rule, index = _arg.index, link = _arg.link;
  917. parent = rule.parentStyleSheet;
  918. href = this.generateCacheBustUrl(rule.href);
  919. media = rule.media.length ? [].join.call(rule.media, ', ') : '';
  920. newRule = "@import url(\"" + href + "\") " + media + ";";
  921. rule.__LiveReload_newHref = href;
  922. tempLink = this.document.createElement("link");
  923. tempLink.rel = 'stylesheet';
  924. tempLink.href = href;
  925. tempLink.__LiveReload_pendingRemoval = true;
  926. if (link.parentNode) {
  927. link.parentNode.insertBefore(tempLink, link);
  928. }
  929. return this.Timer.start(this.importCacheWaitPeriod, (function(_this) {
  930. return function() {
  931. if (tempLink.parentNode) {
  932. tempLink.parentNode.removeChild(tempLink);
  933. }
  934. if (rule.__LiveReload_newHref !== href) {
  935. return;
  936. }
  937. parent.insertRule(newRule, index);
  938. parent.deleteRule(index + 1);
  939. rule = parent.cssRules[index];
  940. rule.__LiveReload_newHref = href;
  941. return _this.Timer.start(_this.importCacheWaitPeriod, function() {
  942. if (rule.__LiveReload_newHref !== href) {
  943. return;
  944. }
  945. parent.insertRule(newRule, index);
  946. return parent.deleteRule(index + 1);
  947. });
  948. };
  949. })(this));
  950. };
  951. Reloader.prototype.generateUniqueString = function() {
  952. return 'livereload=' + Date.now();
  953. };
  954. Reloader.prototype.generateCacheBustUrl = function(url, expando) {
  955. var hash, oldParams, originalUrl, params, _ref;
  956. if (expando == null) {
  957. expando = this.generateUniqueString();
  958. }
  959. _ref = splitUrl(url), url = _ref.url, hash = _ref.hash, oldParams = _ref.params;
  960. if (this.options.overrideURL) {
  961. if (url.indexOf(this.options.serverURL) < 0) {
  962. originalUrl = url;
  963. url = this.options.serverURL + this.options.overrideURL + "?url=" + encodeURIComponent(url);
  964. this.console.log("LiveReload is overriding source URL " + originalUrl + " with " + url);
  965. }
  966. }
  967. params = oldParams.replace(/(\?|&)livereload=(\d+)/, function(match, sep) {
  968. return "" + sep + expando;
  969. });
  970. if (params === oldParams) {
  971. if (oldParams.length === 0) {
  972. params = "?" + expando;
  973. } else {
  974. params = "" + oldParams + "&" + expando;
  975. }
  976. }
  977. return url + params + hash;
  978. };
  979. return Reloader;
  980. })();
  981. }).call(this);
  982. },{}],8:[function(require,module,exports){
  983. (function() {
  984. var CustomEvents, LiveReload, k;
  985. CustomEvents = require('./customevents');
  986. LiveReload = window.LiveReload = new (require('./livereload').LiveReload)(window);
  987. for (k in window) {
  988. if (k.match(/^LiveReloadPlugin/)) {
  989. LiveReload.addPlugin(window[k]);
  990. }
  991. }
  992. LiveReload.addPlugin(require('./less'));
  993. LiveReload.on('shutdown', function() {
  994. return delete window.LiveReload;
  995. });
  996. LiveReload.on('connect', function() {
  997. return CustomEvents.fire(document, 'LiveReloadConnect');
  998. });
  999. LiveReload.on('disconnect', function() {
  1000. return CustomEvents.fire(document, 'LiveReloadDisconnect');
  1001. });
  1002. CustomEvents.bind(document, 'LiveReloadShutDown', function() {
  1003. return LiveReload.shutDown();
  1004. });
  1005. }).call(this);
  1006. },{"./customevents":2,"./less":3,"./livereload":4}],9:[function(require,module,exports){
  1007. (function() {
  1008. var Timer;
  1009. exports.Timer = Timer = (function() {
  1010. function Timer(func) {
  1011. this.func = func;
  1012. this.running = false;
  1013. this.id = null;
  1014. this._handler = (function(_this) {
  1015. return function() {
  1016. _this.running = false;
  1017. _this.id = null;
  1018. return _this.func();
  1019. };
  1020. })(this);
  1021. }
  1022. Timer.prototype.start = function(timeout) {
  1023. if (this.running) {
  1024. clearTimeout(this.id);
  1025. }
  1026. this.id = setTimeout(this._handler, timeout);
  1027. return this.running = true;
  1028. };
  1029. Timer.prototype.stop = function() {
  1030. if (this.running) {
  1031. clearTimeout(this.id);
  1032. this.running = false;
  1033. return this.id = null;
  1034. }
  1035. };
  1036. return Timer;
  1037. })();
  1038. Timer.start = function(timeout, func) {
  1039. return setTimeout(func, timeout);
  1040. };
  1041. }).call(this);
  1042. },{}]},{},[8]);