UNPKG

webpack-plugin-serve

Version:
86 lines (71 loc) 2.1 kB
/* Copyright © 2018 Andrew Powell This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. The above copyright notice and this permission notice shall be included in all copies or substantial portions of this Source Code Form. */ const { error, info, refresh, warn } = require('./log')(); let latest = true; const hmr = (onFailure) => { return { onUnaccepted(data) { onFailure(); warn('Change in unaccepted module(s):\n', data); warn(data); }, onDeclined(data) { onFailure(); warn('Change in declined module(s):\n', data); }, onErrored(data) { onFailure(); error('Error in module(s):\n', data); } }; }; const replace = async (buildHash, hash, refreshOnFailure) => { const { apply, check, status } = module.hot; if (hash) { // eslint-disable-next-line no-undef latest = hash.includes(buildHash); } if (!latest) { const hmrStatus = status(); if (hmrStatus === 'abort' || hmrStatus === 'fail') { warn(`An HMR update was triggered, but ${hmrStatus}ed. ${refresh}`); return; } let modules; try { modules = await check(false); } catch (e) { // noop. this typically happens when a MultiCompiler has more than one compiler that includes // this script, and an update happens with a hash that isn't part of the compiler/module this // instance was loaded for. return; } if (!modules) { warn(`No modules found for replacement. ${refresh}`); return; } modules = await apply( hmr( refreshOnFailure ? () => { if (refreshOnFailure) { // eslint-disable-next-line no-undef location.reload(); } } : () => {} ) ); if (modules) { latest = true; info(`Build ${hash.slice(0, 7)} replaced:\n`, modules); } } }; module.exports = { replace };