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.

1176 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.2';
  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.https ? "s" : "") + "://" + 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. __hasProp = {}.hasOwnProperty;
  250. Connector = require('./connector').Connector;
  251. Timer = require('./timer').Timer;
  252. Options = require('./options').Options;
  253. Reloader = require('./reloader').Reloader;
  254. exports.LiveReload = LiveReload = (function() {
  255. function LiveReload(window) {
  256. var k, v, _ref;
  257. this.window = window;
  258. this.listeners = {};
  259. this.plugins = [];
  260. this.pluginIdentifiers = {};
  261. this.console = this.window.console && this.window.console.log && this.window.console.error ? this.window.location.href.match(/LR-verbose/) ? this.window.console : {
  262. log: function() {},
  263. error: this.window.console.error.bind(this.window.console)
  264. } : {
  265. log: function() {},
  266. error: function() {}
  267. };
  268. if (!(this.WebSocket = this.window.WebSocket || this.window.MozWebSocket)) {
  269. this.console.error("LiveReload disabled because the browser does not seem to support web sockets");
  270. return;
  271. }
  272. if ('LiveReloadOptions' in window) {
  273. this.options = new Options();
  274. _ref = window['LiveReloadOptions'];
  275. for (k in _ref) {
  276. if (!__hasProp.call(_ref, k)) continue;
  277. v = _ref[k];
  278. this.options.set(k, v);
  279. }
  280. } else {
  281. this.options = Options.extract(this.window.document);
  282. if (!this.options) {
  283. this.console.error("LiveReload disabled because it could not find its own <SCRIPT> tag");
  284. return;
  285. }
  286. }
  287. this.reloader = new Reloader(this.window, this.console, Timer);
  288. this.connector = new Connector(this.options, this.WebSocket, Timer, {
  289. connecting: (function(_this) {
  290. return function() {};
  291. })(this),
  292. socketConnected: (function(_this) {
  293. return function() {};
  294. })(this),
  295. connected: (function(_this) {
  296. return function(protocol) {
  297. var _base;
  298. if (typeof (_base = _this.listeners).connect === "function") {
  299. _base.connect();
  300. }
  301. _this.log("LiveReload is connected to " + _this.options.host + ":" + _this.options.port + " (protocol v" + protocol + ").");
  302. return _this.analyze();
  303. };
  304. })(this),
  305. error: (function(_this) {
  306. return function(e) {
  307. return console.log("" + e.message + ".");
  308. };
  309. })(this),
  310. disconnected: (function(_this) {
  311. return function(reason, nextDelay) {
  312. var _base;
  313. if (typeof (_base = _this.listeners).disconnect === "function") {
  314. _base.disconnect();
  315. }
  316. switch (reason) {
  317. case 'cannot-connect':
  318. return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + ", will retry in " + nextDelay + " sec.");
  319. case 'broken':
  320. return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + ", reconnecting in " + nextDelay + " sec.");
  321. case 'handshake-timeout':
  322. return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake timeout), will retry in " + nextDelay + " sec.");
  323. case 'handshake-failed':
  324. return _this.log("LiveReload cannot connect to " + _this.options.host + ":" + _this.options.port + " (handshake failed), will retry in " + nextDelay + " sec.");
  325. case 'manual':
  326. break;
  327. case 'error':
  328. break;
  329. default:
  330. return _this.log("LiveReload disconnected from " + _this.options.host + ":" + _this.options.port + " (" + reason + "), reconnecting in " + nextDelay + " sec.");
  331. }
  332. };
  333. })(this),
  334. message: (function(_this) {
  335. return function(message) {
  336. switch (message.command) {
  337. case 'reload':
  338. return _this.performReload(message);
  339. case 'alert':
  340. return _this.performAlert(message);
  341. }
  342. };
  343. })(this)
  344. });
  345. this.initialized = true;
  346. }
  347. LiveReload.prototype.on = function(eventName, handler) {
  348. return this.listeners[eventName] = handler;
  349. };
  350. LiveReload.prototype.log = function(message) {
  351. return this.console.log("" + message);
  352. };
  353. LiveReload.prototype.performReload = function(message) {
  354. var _ref, _ref1;
  355. this.log("LiveReload received reload request: " + (JSON.stringify(message, null, 2)));
  356. return this.reloader.reload(message.path, {
  357. liveCSS: (_ref = message.liveCSS) != null ? _ref : true,
  358. liveImg: (_ref1 = message.liveImg) != null ? _ref1 : true,
  359. originalPath: message.originalPath || '',
  360. overrideURL: message.overrideURL || '',
  361. serverURL: "http://" + this.options.host + ":" + this.options.port
  362. });
  363. };
  364. LiveReload.prototype.performAlert = function(message) {
  365. return alert(message.message);
  366. };
  367. LiveReload.prototype.shutDown = function() {
  368. var _base;
  369. if (!this.initialized) {
  370. return;
  371. }
  372. this.connector.disconnect();
  373. this.log("LiveReload disconnected.");
  374. return typeof (_base = this.listeners).shutdown === "function" ? _base.shutdown() : void 0;
  375. };
  376. LiveReload.prototype.hasPlugin = function(identifier) {
  377. return !!this.pluginIdentifiers[identifier];
  378. };
  379. LiveReload.prototype.addPlugin = function(pluginClass) {
  380. var plugin;
  381. if (!this.initialized) {
  382. return;
  383. }
  384. if (this.hasPlugin(pluginClass.identifier)) {
  385. return;
  386. }
  387. this.pluginIdentifiers[pluginClass.identifier] = true;
  388. plugin = new pluginClass(this.window, {
  389. _livereload: this,
  390. _reloader: this.reloader,
  391. _connector: this.connector,
  392. console: this.console,
  393. Timer: Timer,
  394. generateCacheBustUrl: (function(_this) {
  395. return function(url) {
  396. return _this.reloader.generateCacheBustUrl(url);
  397. };
  398. })(this)
  399. });
  400. this.plugins.push(plugin);
  401. this.reloader.addPlugin(plugin);
  402. };
  403. LiveReload.prototype.analyze = function() {
  404. var plugin, pluginData, pluginsData, _i, _len, _ref;
  405. if (!this.initialized) {
  406. return;
  407. }
  408. if (!(this.connector.protocol >= 7)) {
  409. return;
  410. }
  411. pluginsData = {};
  412. _ref = this.plugins;
  413. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  414. plugin = _ref[_i];
  415. pluginsData[plugin.constructor.identifier] = pluginData = (typeof plugin.analyze === "function" ? plugin.analyze() : void 0) || {};
  416. pluginData.version = plugin.constructor.version;
  417. }
  418. this.connector.sendCommand({
  419. command: 'info',
  420. plugins: pluginsData,
  421. url: this.window.location.href
  422. });
  423. };
  424. return LiveReload;
  425. })();
  426. }).call(this);
  427. },{"./connector":1,"./options":5,"./reloader":7,"./timer":9}],5:[function(require,module,exports){
  428. (function() {
  429. var Options;
  430. exports.Options = Options = (function() {
  431. function Options() {
  432. this.https = false;
  433. this.host = null;
  434. this.port = 35729;
  435. this.snipver = null;
  436. this.ext = null;
  437. this.extver = null;
  438. this.mindelay = 1000;
  439. this.maxdelay = 60000;
  440. this.handshake_timeout = 5000;
  441. }
  442. Options.prototype.set = function(name, value) {
  443. if (typeof value === 'undefined') {
  444. return;
  445. }
  446. if (!isNaN(+value)) {
  447. value = +value;
  448. }
  449. return this[name] = value;
  450. };
  451. return Options;
  452. })();
  453. Options.extract = function(document) {
  454. var element, keyAndValue, m, mm, options, pair, src, _i, _j, _len, _len1, _ref, _ref1;
  455. _ref = document.getElementsByTagName('script');
  456. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  457. element = _ref[_i];
  458. if ((src = element.src) && (m = src.match(/^[^:]+:\/\/(.*)\/z?livereload\.js(?:\?(.*))?$/))) {
  459. options = new Options();
  460. options.https = src.indexOf("https") === 0;
  461. if (mm = m[1].match(/^([^\/:]+)(?::(\d+))?$/)) {
  462. options.host = mm[1];
  463. if (mm[2]) {
  464. options.port = parseInt(mm[2], 10);
  465. }
  466. }
  467. if (m[2]) {
  468. _ref1 = m[2].split('&');
  469. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  470. pair = _ref1[_j];
  471. if ((keyAndValue = pair.split('=')).length > 1) {
  472. options.set(keyAndValue[0].replace(/-/g, '_'), keyAndValue.slice(1).join('='));
  473. }
  474. }
  475. }
  476. return options;
  477. }
  478. }
  479. return null;
  480. };
  481. }).call(this);
  482. },{}],6:[function(require,module,exports){
  483. (function() {
  484. var PROTOCOL_6, PROTOCOL_7, Parser, ProtocolError,
  485. __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; };
  486. exports.PROTOCOL_6 = PROTOCOL_6 = 'http://livereload.com/protocols/official-6';
  487. exports.PROTOCOL_7 = PROTOCOL_7 = 'http://livereload.com/protocols/official-7';
  488. exports.ProtocolError = ProtocolError = (function() {
  489. function ProtocolError(reason, data) {
  490. this.message = "LiveReload protocol error (" + reason + ") after receiving data: \"" + data + "\".";
  491. }
  492. return ProtocolError;
  493. })();
  494. exports.Parser = Parser = (function() {
  495. function Parser(handlers) {
  496. this.handlers = handlers;
  497. this.reset();
  498. }
  499. Parser.prototype.reset = function() {
  500. return this.protocol = 7;
  501. };
  502. Parser.prototype.process = function(data) {
  503. var command, e, message, options, _ref;
  504. try {
  505. if (this.protocol == null) {
  506. if (data.match(/^!!ver:([\d.]+)$/)) {
  507. this.protocol = 6;
  508. } else if (message = this._parseMessage(data, ['hello'])) {
  509. if (!message.protocols.length) {
  510. throw new ProtocolError("no protocols specified in handshake message");
  511. } else if (__indexOf.call(message.protocols, PROTOCOL_7) >= 0) {
  512. this.protocol = 7;
  513. } else if (__indexOf.call(message.protocols, PROTOCOL_6) >= 0) {
  514. this.protocol = 6;
  515. } else {
  516. throw new ProtocolError("no supported protocols found");
  517. }
  518. }
  519. return this.handlers.connected(this.protocol);
  520. } else if (this.protocol === 6) {
  521. message = JSON.parse(data);
  522. if (!message.length) {
  523. throw new ProtocolError("protocol 6 messages must be arrays");
  524. }
  525. command = message[0], options = message[1];
  526. if (command !== 'refresh') {
  527. throw new ProtocolError("unknown protocol 6 command");
  528. }
  529. return this.handlers.message({
  530. command: 'reload',
  531. path: options.path,
  532. liveCSS: (_ref = options.apply_css_live) != null ? _ref : true
  533. });
  534. } else {
  535. message = this._parseMessage(data, ['reload', 'alert']);
  536. return this.handlers.message(message);
  537. }
  538. } catch (_error) {
  539. e = _error;
  540. if (e instanceof ProtocolError) {
  541. return this.handlers.error(e);
  542. } else {
  543. throw e;
  544. }
  545. }
  546. };
  547. Parser.prototype._parseMessage = function(data, validCommands) {
  548. var e, message, _ref;
  549. try {
  550. message = JSON.parse(data);
  551. } catch (_error) {
  552. e = _error;
  553. throw new ProtocolError('unparsable JSON', data);
  554. }
  555. if (!message.command) {
  556. throw new ProtocolError('missing "command" key', data);
  557. }
  558. if (_ref = message.command, __indexOf.call(validCommands, _ref) < 0) {
  559. throw new ProtocolError("invalid command '" + message.command + "', only valid commands are: " + (validCommands.join(', ')) + ")", data);
  560. }
  561. return message;
  562. };
  563. return Parser;
  564. })();
  565. }).call(this);
  566. },{}],7:[function(require,module,exports){
  567. (function() {
  568. var IMAGE_STYLES, Reloader, numberOfMatchingSegments, pathFromUrl, pathsMatch, pickBestMatch, splitUrl;
  569. splitUrl = function(url) {
  570. var hash, index, params;
  571. if ((index = url.indexOf('#')) >= 0) {
  572. hash = url.slice(index);
  573. url = url.slice(0, index);
  574. } else {
  575. hash = '';
  576. }
  577. if ((index = url.indexOf('?')) >= 0) {
  578. params = url.slice(index);
  579. url = url.slice(0, index);
  580. } else {
  581. params = '';
  582. }
  583. return {
  584. url: url,
  585. params: params,
  586. hash: hash
  587. };
  588. };
  589. pathFromUrl = function(url) {
  590. var path;
  591. url = splitUrl(url).url;
  592. if (url.indexOf('file://') === 0) {
  593. path = url.replace(/^file:\/\/(localhost)?/, '');
  594. } else {
  595. path = url.replace(/^([^:]+:)?\/\/([^:\/]+)(:\d*)?\//, '/');
  596. }
  597. return decodeURIComponent(path);
  598. };
  599. pickBestMatch = function(path, objects, pathFunc) {
  600. var bestMatch, object, score, _i, _len;
  601. bestMatch = {
  602. score: 0
  603. };
  604. for (_i = 0, _len = objects.length; _i < _len; _i++) {
  605. object = objects[_i];
  606. score = numberOfMatchingSegments(path, pathFunc(object));
  607. if (score > bestMatch.score) {
  608. bestMatch = {
  609. object: object,
  610. score: score
  611. };
  612. }
  613. }
  614. if (bestMatch.score > 0) {
  615. return bestMatch;
  616. } else {
  617. return null;
  618. }
  619. };
  620. numberOfMatchingSegments = function(path1, path2) {
  621. var comps1, comps2, eqCount, len;
  622. path1 = path1.replace(/^\/+/, '').toLowerCase();
  623. path2 = path2.replace(/^\/+/, '').toLowerCase();
  624. if (path1 === path2) {
  625. return 10000;
  626. }
  627. comps1 = path1.split('/').reverse();
  628. comps2 = path2.split('/').reverse();
  629. len = Math.min(comps1.length, comps2.length);
  630. eqCount = 0;
  631. while (eqCount < len && comps1[eqCount] === comps2[eqCount]) {
  632. ++eqCount;
  633. }
  634. return eqCount;
  635. };
  636. pathsMatch = function(path1, path2) {
  637. return numberOfMatchingSegments(path1, path2) > 0;
  638. };
  639. IMAGE_STYLES = [
  640. {
  641. selector: 'background',
  642. styleNames: ['backgroundImage']
  643. }, {
  644. selector: 'border',
  645. styleNames: ['borderImage', 'webkitBorderImage', 'MozBorderImage']
  646. }
  647. ];
  648. exports.Reloader = Reloader = (function() {
  649. function Reloader(window, console, Timer) {
  650. this.window = window;
  651. this.console = console;
  652. this.Timer = Timer;
  653. this.document = this.window.document;
  654. this.importCacheWaitPeriod = 200;
  655. this.plugins = [];
  656. }
  657. Reloader.prototype.addPlugin = function(plugin) {
  658. return this.plugins.push(plugin);
  659. };
  660. Reloader.prototype.analyze = function(callback) {
  661. return results;
  662. };
  663. Reloader.prototype.reload = function(path, options) {
  664. var plugin, _base, _i, _len, _ref;
  665. this.options = options;
  666. if ((_base = this.options).stylesheetReloadTimeout == null) {
  667. _base.stylesheetReloadTimeout = 15000;
  668. }
  669. _ref = this.plugins;
  670. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  671. plugin = _ref[_i];
  672. if (plugin.reload && plugin.reload(path, options)) {
  673. return;
  674. }
  675. }
  676. if (options.liveCSS) {
  677. if (path.match(/\.css$/i)) {
  678. if (this.reloadStylesheet(path)) {
  679. return;
  680. }
  681. }
  682. }
  683. if (options.liveImg) {
  684. if (path.match(/\.(jpe?g|png|gif)$/i)) {
  685. this.reloadImages(path);
  686. return;
  687. }
  688. }
  689. return this.reloadPage();
  690. };
  691. Reloader.prototype.reloadPage = function() {
  692. return this.window.document.location.reload();
  693. };
  694. Reloader.prototype.reloadImages = function(path) {
  695. var expando, img, selector, styleNames, styleSheet, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3, _results;
  696. expando = this.generateUniqueString();
  697. _ref = this.document.images;
  698. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  699. img = _ref[_i];
  700. if (pathsMatch(path, pathFromUrl(img.src))) {
  701. img.src = this.generateCacheBustUrl(img.src, expando);
  702. }
  703. }
  704. if (this.document.querySelectorAll) {
  705. for (_j = 0, _len1 = IMAGE_STYLES.length; _j < _len1; _j++) {
  706. _ref1 = IMAGE_STYLES[_j], selector = _ref1.selector, styleNames = _ref1.styleNames;
  707. _ref2 = this.document.querySelectorAll("[style*=" + selector + "]");
  708. for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
  709. img = _ref2[_k];
  710. this.reloadStyleImages(img.style, styleNames, path, expando);
  711. }
  712. }
  713. }
  714. if (this.document.styleSheets) {
  715. _ref3 = this.document.styleSheets;
  716. _results = [];
  717. for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
  718. styleSheet = _ref3[_l];
  719. _results.push(this.reloadStylesheetImages(styleSheet, path, expando));
  720. }
  721. return _results;
  722. }
  723. };
  724. Reloader.prototype.reloadStylesheetImages = function(styleSheet, path, expando) {
  725. var e, rule, rules, styleNames, _i, _j, _len, _len1;
  726. try {
  727. rules = styleSheet != null ? styleSheet.cssRules : void 0;
  728. } catch (_error) {
  729. e = _error;
  730. }
  731. if (!rules) {
  732. return;
  733. }
  734. for (_i = 0, _len = rules.length; _i < _len; _i++) {
  735. rule = rules[_i];
  736. switch (rule.type) {
  737. case CSSRule.IMPORT_RULE:
  738. this.reloadStylesheetImages(rule.styleSheet, path, expando);
  739. break;
  740. case CSSRule.STYLE_RULE:
  741. for (_j = 0, _len1 = IMAGE_STYLES.length; _j < _len1; _j++) {
  742. styleNames = IMAGE_STYLES[_j].styleNames;
  743. this.reloadStyleImages(rule.style, styleNames, path, expando);
  744. }
  745. break;
  746. case CSSRule.MEDIA_RULE:
  747. this.reloadStylesheetImages(rule, path, expando);
  748. }
  749. }
  750. };
  751. Reloader.prototype.reloadStyleImages = function(style, styleNames, path, expando) {
  752. var newValue, styleName, value, _i, _len;
  753. for (_i = 0, _len = styleNames.length; _i < _len; _i++) {
  754. styleName = styleNames[_i];
  755. value = style[styleName];
  756. if (typeof value === 'string') {
  757. newValue = value.replace(/\burl\s*\(([^)]*)\)/, (function(_this) {
  758. return function(match, src) {
  759. if (pathsMatch(path, pathFromUrl(src))) {
  760. return "url(" + (_this.generateCacheBustUrl(src, expando)) + ")";
  761. } else {
  762. return match;
  763. }
  764. };
  765. })(this));
  766. if (newValue !== value) {
  767. style[styleName] = newValue;
  768. }
  769. }
  770. }
  771. };
  772. Reloader.prototype.reloadStylesheet = function(path) {
  773. var imported, link, links, match, style, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1;
  774. links = (function() {
  775. var _i, _len, _ref, _results;
  776. _ref = this.document.getElementsByTagName('link');
  777. _results = [];
  778. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  779. link = _ref[_i];
  780. if (link.rel.match(/^stylesheet$/i) && !link.__LiveReload_pendingRemoval) {
  781. _results.push(link);
  782. }
  783. }
  784. return _results;
  785. }).call(this);
  786. imported = [];
  787. _ref = this.document.getElementsByTagName('style');
  788. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  789. style = _ref[_i];
  790. if (style.sheet) {
  791. this.collectImportedStylesheets(style, style.sheet, imported);
  792. }
  793. }
  794. for (_j = 0, _len1 = links.length; _j < _len1; _j++) {
  795. link = links[_j];
  796. this.collectImportedStylesheets(link, link.sheet, imported);
  797. }
  798. if (this.window.StyleFix && this.document.querySelectorAll) {
  799. _ref1 = this.document.querySelectorAll('style[data-href]');
  800. for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
  801. style = _ref1[_k];
  802. links.push(style);
  803. }
  804. }
  805. this.console.log("LiveReload found " + links.length + " LINKed stylesheets, " + imported.length + " @imported stylesheets");
  806. match = pickBestMatch(path, links.concat(imported), (function(_this) {
  807. return function(l) {
  808. return pathFromUrl(_this.linkHref(l));
  809. };
  810. })(this));
  811. if (match) {
  812. if (match.object.rule) {
  813. this.console.log("LiveReload is reloading imported stylesheet: " + match.object.href);
  814. this.reattachImportedRule(match.object);
  815. } else {
  816. this.console.log("LiveReload is reloading stylesheet: " + (this.linkHref(match.object)));
  817. this.reattachStylesheetLink(match.object);
  818. }
  819. } else {
  820. this.console.log("LiveReload will reload all stylesheets because path '" + path + "' did not match any specific one");
  821. for (_l = 0, _len3 = links.length; _l < _len3; _l++) {
  822. link = links[_l];
  823. this.reattachStylesheetLink(link);
  824. }
  825. }
  826. return true;
  827. };
  828. Reloader.prototype.collectImportedStylesheets = function(link, styleSheet, result) {
  829. var e, index, rule, rules, _i, _len;
  830. try {
  831. rules = styleSheet != null ? styleSheet.cssRules : void 0;
  832. } catch (_error) {
  833. e = _error;
  834. }
  835. if (rules && rules.length) {
  836. for (index = _i = 0, _len = rules.length; _i < _len; index = ++_i) {
  837. rule = rules[index];
  838. switch (rule.type) {
  839. case CSSRule.CHARSET_RULE:
  840. continue;
  841. case CSSRule.IMPORT_RULE:
  842. result.push({
  843. link: link,
  844. rule: rule,
  845. index: index,
  846. href: rule.href
  847. });
  848. this.collectImportedStylesheets(link, rule.styleSheet, result);
  849. break;
  850. default:
  851. break;
  852. }
  853. }
  854. }
  855. };
  856. Reloader.prototype.waitUntilCssLoads = function(clone, func) {
  857. var callbackExecuted, executeCallback, poll;
  858. callbackExecuted = false;
  859. executeCallback = (function(_this) {
  860. return function() {
  861. if (callbackExecuted) {
  862. return;
  863. }
  864. callbackExecuted = true;
  865. return func();
  866. };
  867. })(this);
  868. clone.onload = (function(_this) {
  869. return function() {
  870. _this.console.log("LiveReload: the new stylesheet has finished loading");
  871. _this.knownToSupportCssOnLoad = true;
  872. return executeCallback();
  873. };
  874. })(this);
  875. if (!this.knownToSupportCssOnLoad) {
  876. (poll = (function(_this) {
  877. return function() {
  878. if (clone.sheet) {
  879. _this.console.log("LiveReload is polling until the new CSS finishes loading...");
  880. return executeCallback();
  881. } else {
  882. return _this.Timer.start(50, poll);
  883. }
  884. };
  885. })(this))();
  886. }
  887. return this.Timer.start(this.options.stylesheetReloadTimeout, executeCallback);
  888. };
  889. Reloader.prototype.linkHref = function(link) {
  890. return link.href || link.getAttribute('data-href');
  891. };
  892. Reloader.prototype.reattachStylesheetLink = function(link) {
  893. var clone, parent;
  894. if (link.__LiveReload_pendingRemoval) {
  895. return;
  896. }
  897. link.__LiveReload_pendingRemoval = true;
  898. if (link.tagName === 'STYLE') {
  899. clone = this.document.createElement('link');
  900. clone.rel = 'stylesheet';
  901. clone.media = link.media;
  902. clone.disabled = link.disabled;
  903. } else {
  904. clone = link.cloneNode(false);
  905. }
  906. clone.href = this.generateCacheBustUrl(this.linkHref(link));
  907. parent = link.parentNode;
  908. if (parent.lastChild === link) {
  909. parent.appendChild(clone);
  910. } else {
  911. parent.insertBefore(clone, link.nextSibling);
  912. }
  913. return this.waitUntilCssLoads(clone, (function(_this) {
  914. return function() {
  915. var additionalWaitingTime;
  916. if (/AppleWebKit/.test(navigator.userAgent)) {
  917. additionalWaitingTime = 5;
  918. } else {
  919. additionalWaitingTime = 200;
  920. }
  921. return _this.Timer.start(additionalWaitingTime, function() {
  922. var _ref;
  923. if (!link.parentNode) {
  924. return;
  925. }
  926. link.parentNode.removeChild(link);
  927. clone.onreadystatechange = null;
  928. return (_ref = _this.window.StyleFix) != null ? _ref.link(clone) : void 0;
  929. });
  930. };
  931. })(this));
  932. };
  933. Reloader.prototype.reattachImportedRule = function(_arg) {
  934. var href, index, link, media, newRule, parent, rule, tempLink;
  935. rule = _arg.rule, index = _arg.index, link = _arg.link;
  936. parent = rule.parentStyleSheet;
  937. href = this.generateCacheBustUrl(rule.href);
  938. media = rule.media.length ? [].join.call(rule.media, ', ') : '';
  939. newRule = "@import url(\"" + href + "\") " + media + ";";
  940. rule.__LiveReload_newHref = href;
  941. tempLink = this.document.createElement("link");
  942. tempLink.rel = 'stylesheet';
  943. tempLink.href = href;
  944. tempLink.__LiveReload_pendingRemoval = true;
  945. if (link.parentNode) {
  946. link.parentNode.insertBefore(tempLink, link);
  947. }
  948. return this.Timer.start(this.importCacheWaitPeriod, (function(_this) {
  949. return function() {
  950. if (tempLink.parentNode) {
  951. tempLink.parentNode.removeChild(tempLink);
  952. }
  953. if (rule.__LiveReload_newHref !== href) {
  954. return;
  955. }
  956. parent.insertRule(newRule, index);
  957. parent.deleteRule(index + 1);
  958. rule = parent.cssRules[index];
  959. rule.__LiveReload_newHref = href;
  960. return _this.Timer.start(_this.importCacheWaitPeriod, function() {
  961. if (rule.__LiveReload_newHref !== href) {
  962. return;
  963. }
  964. parent.insertRule(newRule, index);
  965. return parent.deleteRule(index + 1);
  966. });
  967. };
  968. })(this));
  969. };
  970. Reloader.prototype.generateUniqueString = function() {
  971. return 'livereload=' + Date.now();
  972. };
  973. Reloader.prototype.generateCacheBustUrl = function(url, expando) {
  974. var hash, oldParams, originalUrl, params, _ref;
  975. if (expando == null) {
  976. expando = this.generateUniqueString();
  977. }
  978. _ref = splitUrl(url), url = _ref.url, hash = _ref.hash, oldParams = _ref.params;
  979. if (this.options.overrideURL) {
  980. if (url.indexOf(this.options.serverURL) < 0) {
  981. originalUrl = url;
  982. url = this.options.serverURL + this.options.overrideURL + "?url=" + encodeURIComponent(url);
  983. this.console.log("LiveReload is overriding source URL " + originalUrl + " with " + url);
  984. }
  985. }
  986. params = oldParams.replace(/(\?|&)livereload=(\d+)/, function(match, sep) {
  987. return "" + sep + expando;
  988. });
  989. if (params === oldParams) {
  990. if (oldParams.length === 0) {
  991. params = "?" + expando;
  992. } else {
  993. params = "" + oldParams + "&" + expando;
  994. }
  995. }
  996. return url + params + hash;
  997. };
  998. return Reloader;
  999. })();
  1000. }).call(this);
  1001. },{}],8:[function(require,module,exports){
  1002. (function() {
  1003. var CustomEvents, LiveReload, k;
  1004. CustomEvents = require('./customevents');
  1005. LiveReload = window.LiveReload = new (require('./livereload').LiveReload)(window);
  1006. for (k in window) {
  1007. if (k.match(/^LiveReloadPlugin/)) {
  1008. LiveReload.addPlugin(window[k]);
  1009. }
  1010. }
  1011. LiveReload.addPlugin(require('./less'));
  1012. LiveReload.on('shutdown', function() {
  1013. return delete window.LiveReload;
  1014. });
  1015. LiveReload.on('connect', function() {
  1016. return CustomEvents.fire(document, 'LiveReloadConnect');
  1017. });
  1018. LiveReload.on('disconnect', function() {
  1019. return CustomEvents.fire(document, 'LiveReloadDisconnect');
  1020. });
  1021. CustomEvents.bind(document, 'LiveReloadShutDown', function() {
  1022. return LiveReload.shutDown();
  1023. });
  1024. }).call(this);
  1025. },{"./customevents":2,"./less":3,"./livereload":4}],9:[function(require,module,exports){
  1026. (function() {
  1027. var Timer;
  1028. exports.Timer = Timer = (function() {
  1029. function Timer(func) {
  1030. this.func = func;
  1031. this.running = false;
  1032. this.id = null;
  1033. this._handler = (function(_this) {
  1034. return function() {
  1035. _this.running = false;
  1036. _this.id = null;
  1037. return _this.func();
  1038. };
  1039. })(this);
  1040. }
  1041. Timer.prototype.start = function(timeout) {
  1042. if (this.running) {
  1043. clearTimeout(this.id);
  1044. }
  1045. this.id = setTimeout(this._handler, timeout);
  1046. return this.running = true;
  1047. };
  1048. Timer.prototype.stop = function() {
  1049. if (this.running) {
  1050. clearTimeout(this.id);
  1051. this.running = false;
  1052. return this.id = null;
  1053. }
  1054. };
  1055. return Timer;
  1056. })();
  1057. Timer.start = function(timeout, func) {
  1058. return setTimeout(func, timeout);
  1059. };
  1060. }).call(this);
  1061. },{}]},{},[8]);