diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/webpack-hot-client/client | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/webpack-hot-client/client')
| -rw-r--r-- | node_modules/webpack-hot-client/client/.gitkeep | 0 | ||||
| -rw-r--r-- | node_modules/webpack-hot-client/client/hot.js | 184 | ||||
| -rw-r--r-- | node_modules/webpack-hot-client/client/index.js | 101 | ||||
| -rw-r--r-- | node_modules/webpack-hot-client/client/log.js | 59 | ||||
| -rw-r--r-- | node_modules/webpack-hot-client/client/socket.js | 51 |
5 files changed, 395 insertions, 0 deletions
diff --git a/node_modules/webpack-hot-client/client/.gitkeep b/node_modules/webpack-hot-client/client/.gitkeep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/node_modules/webpack-hot-client/client/.gitkeep diff --git a/node_modules/webpack-hot-client/client/hot.js b/node_modules/webpack-hot-client/client/hot.js new file mode 100644 index 00000000..5408d196 --- /dev/null +++ b/node_modules/webpack-hot-client/client/hot.js @@ -0,0 +1,184 @@ +'use strict'; + +var log = require('./log'); + +var refresh = 'Please refresh the page.'; +var hotOptions = { + ignoreUnaccepted: true, + ignoreDeclined: true, + ignoreErrored: true, + onUnaccepted: function onUnaccepted(data) { + var chain = [].concat(data.chain); + var last = chain[chain.length - 1]; + + if (last === 0) { + chain.pop(); + } + + log.warn("Ignored an update to unaccepted module ".concat(chain.join(' ➭ '))); + }, + onDeclined: function onDeclined(data) { + log.warn("Ignored an update to declined module ".concat(data.chain.join(' ➭ '))); + }, + onErrored: function onErrored(data) { + log.warn("Ignored an error while updating module ".concat(data.moduleId, " <").concat(data.type, ">")); + log.warn(data.error); + } +}; +var lastHash; + +function upToDate() { + return lastHash.indexOf(__webpack_hash__) >= 0; +} + +function result(modules, appliedModules) { + var unaccepted = modules.filter(function (moduleId) { + return appliedModules && appliedModules.indexOf(moduleId) < 0; + }); + + if (unaccepted.length > 0) { + var message = 'The following modules could not be updated:'; + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = unaccepted[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var _moduleId = _step.value; + message += "\n \u29BB ".concat(_moduleId); + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + + log.warn(message); + } + + if (!(appliedModules || []).length) { + log.info('No Modules Updated.'); + } else { + var _message = ['The following modules were updated:']; + var _iteratorNormalCompletion2 = true; + var _didIteratorError2 = false; + var _iteratorError2 = undefined; + + try { + for (var _iterator2 = appliedModules[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { + var _moduleId3 = _step2.value; + + _message.push(" \u21BB ".concat(_moduleId3)); + } + } catch (err) { + _didIteratorError2 = true; + _iteratorError2 = err; + } finally { + try { + if (!_iteratorNormalCompletion2 && _iterator2.return != null) { + _iterator2.return(); + } + } finally { + if (_didIteratorError2) { + throw _iteratorError2; + } + } + } + + log.info(_message.join('\n')); + var numberIds = appliedModules.every(function (moduleId) { + return typeof moduleId === 'number'; + }); + + if (numberIds) { + log.info('Please consider using the NamedModulesPlugin for module names.'); + } + } +} + +function check(options) { + module.hot.check().then(function (modules) { + if (!modules) { + log.warn("Cannot find update. The server may have been restarted. ".concat(refresh)); + + if (options.reload) { + window.location.reload(); + } + + return; + } + + var hotOpts = options.reload ? {} : hotOptions; + return module.hot.apply(hotOpts).then(function (appliedModules) { + if (!upToDate()) { + check(options); + } + + result(modules, appliedModules); + + if (upToDate()) { + log.info('App is up to date.'); + } + }).catch(function (err) { + var status = module.hot.status(); + + if (['abort', 'fail'].indexOf(status) >= 0) { + log.warn("Cannot apply update. ".concat(refresh)); + log.warn(err.stack || err.message); + + if (options.reload) { + window.location.reload(); + } + } else { + log.warn("Update failed: ".concat(err.stack) || err.message); + } + }); + }).catch(function (err) { + var status = module.hot.status(); + + if (['abort', 'fail'].indexOf(status) >= 0) { + log.warn("Cannot check for update. ".concat(refresh)); + log.warn(err.stack || err.message); + + if (options.reload) { + window.location.reload(); + } + } else { + log.warn("Update check failed: ".concat(err.stack) || err.message); + } + }); +} + +if (module.hot) { + log.info('Hot Module Replacement Enabled. Waiting for signal.'); +} else { + log.error('Hot Module Replacement is disabled.'); +} + +module.exports = function update(currentHash, options) { + lastHash = currentHash; + + if (!upToDate()) { + var status = module.hot.status(); + + if (status === 'idle') { + log.info('Checking for updates to the bundle.'); + check(options); + } else if (['abort', 'fail'].indexOf(status) >= 0) { + log.warn("Cannot apply update. A previous update ".concat(status, "ed. ").concat(refresh)); + + if (options.reload) { + window.location.reload(); + } + } + } +};
\ No newline at end of file diff --git a/node_modules/webpack-hot-client/client/index.js b/node_modules/webpack-hot-client/client/index.js new file mode 100644 index 00000000..2dca618d --- /dev/null +++ b/node_modules/webpack-hot-client/client/index.js @@ -0,0 +1,101 @@ +'use strict'; +/* eslint-disable global-require */ + +(function hotClientEntry() { + // eslint-disable-next-line no-underscore-dangle + if (window.__webpackHotClient__) { + return; + } // eslint-disable-next-line no-underscore-dangle + + + window.__webpackHotClient__ = {}; // this is piped in at runtime build via DefinePlugin in /lib/plugins.js + // eslint-disable-next-line no-unused-vars, no-undef + + var options = __hotClientOptions__; + + var log = require('./log'); // eslint-disable-line import/order + + + log.level = options.logLevel; + + var update = require('./hot'); + + var socket = require('./socket'); + + if (!options) { + throw new Error('Something went awry and __hotClientOptions__ is undefined. Possible bad build. HMR cannot be enabled.'); + } + + var currentHash; + var initial = true; + var isUnloading; + window.addEventListener('beforeunload', function () { + isUnloading = true; + }); + + function reload() { + if (isUnloading) { + return; + } + + if (options.hot) { + log.info('App Updated, Reloading Modules'); + update(currentHash, options); + } else if (options.reload) { + log.info('Refreshing Page'); + window.location.reload(); + } else { + log.warn('Please refresh the page manually.'); + log.info('The `hot` and `reload` options are set to false.'); + } + } + + socket(options, { + compile: function compile(_ref) { + var compilerName = _ref.compilerName; + log.info("webpack: Compiling (".concat(compilerName, ")")); + }, + errors: function errors(_ref2) { + var errors = _ref2.errors; + log.error('webpack: Encountered errors while compiling. Reload prevented.'); + + for (var i = 0; i < errors.length; i++) { + log.error(errors[i]); + } + }, + hash: function hash(_ref3) { + var hash = _ref3.hash; + currentHash = hash; + }, + invalid: function invalid(_ref4) { + var fileName = _ref4.fileName; + log.info("App updated. Recompiling ".concat(fileName)); + }, + ok: function ok() { + if (initial) { + initial = false; + return initial; + } + + reload(); + }, + 'window-reload': function windowReload() { + window.location.reload(); + }, + warnings: function warnings(_ref5) { + var warnings = _ref5.warnings; + log.warn('Warnings while compiling.'); + + for (var i = 0; i < warnings.length; i++) { + log.warn(warnings[i]); + } + + if (initial) { + initial = false; + return initial; + } + + reload(); + } + }); +})();
\ No newline at end of file diff --git a/node_modules/webpack-hot-client/client/log.js b/node_modules/webpack-hot-client/client/log.js new file mode 100644 index 00000000..50e2d02b --- /dev/null +++ b/node_modules/webpack-hot-client/client/log.js @@ -0,0 +1,59 @@ +'use strict'; // eslint-disable-next-line import/no-extraneous-dependencies + +function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } + +function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } + +function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } } + +var loglevel = require('loglevelnext/dist/loglevelnext'); + +var MethodFactory = loglevel.factories.MethodFactory; +var css = { + prefix: 'color: #999; padding: 0 0 0 20px; line-height: 16px; background: url(https://webpack.js.org/6bc5d8cf78d442a984e70195db059b69.svg) no-repeat; background-size: 16px 16px; background-position: 0 -2px;', + reset: 'color: #444' +}; +var log = loglevel.getLogger({ + name: 'hot', + id: 'hot-middleware/client' +}); + +function IconFactory(logger) { + MethodFactory.call(this, logger); +} + +IconFactory.prototype = Object.create(MethodFactory.prototype); +IconFactory.prototype.constructor = IconFactory; + +IconFactory.prototype.make = function make(methodName) { + var og = MethodFactory.prototype.make.call(this, methodName); + return function _() { + for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) { + params[_key] = arguments[_key]; + } + + var args = [].concat(params); + var prefix = '%c「hot」 %c'; + + var _args = _slicedToArray(args, 1), + first = _args[0]; + + if (typeof first === 'string') { + args[0] = prefix + first; + } else { + args.unshift(prefix); + } + + args.splice(1, 0, css.prefix, css.reset); + og.apply(void 0, _toConsumableArray(args)); + }; +}; + +log.factory = new IconFactory(log, {}); +log.group = console.group; // eslint-disable-line no-console + +log.groupCollapsed = console.groupCollapsed; // eslint-disable-line no-console + +log.groupEnd = console.groupEnd; // eslint-disable-line no-console + +module.exports = log;
\ No newline at end of file diff --git a/node_modules/webpack-hot-client/client/socket.js b/node_modules/webpack-hot-client/client/socket.js new file mode 100644 index 00000000..31847255 --- /dev/null +++ b/node_modules/webpack-hot-client/client/socket.js @@ -0,0 +1,51 @@ +'use strict'; + +var url = require('url'); + +var log = require('./log'); + +var maxRetries = 10; +var retry = maxRetries; + +module.exports = function connect(options, handler) { + var socketUrl = url.format({ + protocol: options.https ? 'wss' : 'ws', + hostname: options.webSocket.host, + port: options.webSocket.port, + slashes: true + }); + var open = false; + var socket = new WebSocket(socketUrl); + socket.addEventListener('open', function () { + open = true; + retry = maxRetries; + log.info('WebSocket connected'); + }); + socket.addEventListener('close', function () { + log.warn('WebSocket closed'); + open = false; + socket = null; // exponentation operator ** isn't supported by IE at all + // eslint-disable-next-line no-restricted-properties + + var timeout = 1000 * Math.pow(maxRetries - retry, 2) + Math.random() * 100; + + if (open || retry <= 0) { + log.warn("WebSocket: ending reconnect after ".concat(maxRetries, " attempts")); + return; + } + + log.info("WebSocket: attempting reconnect in ".concat(parseInt(timeout / 1000, 10), "s")); + setTimeout(function () { + retry -= 1; + connect(options, handler); + }, timeout); + }); + socket.addEventListener('message', function (event) { + log.debug('WebSocket: message:', event.data); + var message = JSON.parse(event.data); + + if (handler[message.type]) { + handler[message.type](message.data); + } + }); +};
\ No newline at end of file |
