[6a3a178] | 1 | 'use strict';
|
---|
| 2 | /* global __resourceQuery WorkerGlobalScope self */
|
---|
| 3 |
|
---|
| 4 | /* eslint prefer-destructuring: off */
|
---|
| 5 |
|
---|
| 6 | var stripAnsi = require('strip-ansi');
|
---|
| 7 |
|
---|
| 8 | var socket = require('./socket');
|
---|
| 9 |
|
---|
| 10 | var overlay = require('./overlay');
|
---|
| 11 |
|
---|
| 12 | var _require = require('./utils/log'),
|
---|
| 13 | log = _require.log,
|
---|
| 14 | setLogLevel = _require.setLogLevel;
|
---|
| 15 |
|
---|
| 16 | var sendMessage = require('./utils/sendMessage');
|
---|
| 17 |
|
---|
| 18 | var reloadApp = require('./utils/reloadApp');
|
---|
| 19 |
|
---|
| 20 | var createSocketUrl = require('./utils/createSocketUrl');
|
---|
| 21 |
|
---|
| 22 | var status = {
|
---|
| 23 | isUnloading: false,
|
---|
| 24 | currentHash: ''
|
---|
| 25 | };
|
---|
| 26 | var options = {
|
---|
| 27 | hot: false,
|
---|
| 28 | hotReload: true,
|
---|
| 29 | liveReload: false,
|
---|
| 30 | initial: true,
|
---|
| 31 | useWarningOverlay: false,
|
---|
| 32 | useErrorOverlay: false,
|
---|
| 33 | useProgress: false
|
---|
| 34 | };
|
---|
| 35 | var socketUrl = createSocketUrl(__resourceQuery);
|
---|
| 36 | self.addEventListener('beforeunload', function () {
|
---|
| 37 | status.isUnloading = true;
|
---|
| 38 | });
|
---|
| 39 |
|
---|
| 40 | if (typeof window !== 'undefined') {
|
---|
| 41 | var qs = window.location.search.toLowerCase();
|
---|
| 42 | options.hotReload = qs.indexOf('hotreload=false') === -1;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | var onSocketMessage = {
|
---|
| 46 | hot: function hot() {
|
---|
| 47 | options.hot = true;
|
---|
| 48 | log.info('[WDS] Hot Module Replacement enabled.');
|
---|
| 49 | },
|
---|
| 50 | liveReload: function liveReload() {
|
---|
| 51 | options.liveReload = true;
|
---|
| 52 | log.info('[WDS] Live Reloading enabled.');
|
---|
| 53 | },
|
---|
| 54 | invalid: function invalid() {
|
---|
| 55 | log.info('[WDS] App updated. Recompiling...'); // fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
|
---|
| 56 |
|
---|
| 57 | if (options.useWarningOverlay || options.useErrorOverlay) {
|
---|
| 58 | overlay.clear();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | sendMessage('Invalid');
|
---|
| 62 | },
|
---|
| 63 | hash: function hash(_hash) {
|
---|
| 64 | status.currentHash = _hash;
|
---|
| 65 | },
|
---|
| 66 | 'still-ok': function stillOk() {
|
---|
| 67 | log.info('[WDS] Nothing changed.');
|
---|
| 68 |
|
---|
| 69 | if (options.useWarningOverlay || options.useErrorOverlay) {
|
---|
| 70 | overlay.clear();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | sendMessage('StillOk');
|
---|
| 74 | },
|
---|
| 75 | 'log-level': function logLevel(level) {
|
---|
| 76 | var hotCtx = require.context('webpack/hot', false, /^\.\/log$/);
|
---|
| 77 |
|
---|
| 78 | if (hotCtx.keys().indexOf('./log') !== -1) {
|
---|
| 79 | hotCtx('./log').setLogLevel(level);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | setLogLevel(level);
|
---|
| 83 | },
|
---|
| 84 | overlay: function overlay(value) {
|
---|
| 85 | if (typeof document !== 'undefined') {
|
---|
| 86 | if (typeof value === 'boolean') {
|
---|
| 87 | options.useWarningOverlay = false;
|
---|
| 88 | options.useErrorOverlay = value;
|
---|
| 89 | } else if (value) {
|
---|
| 90 | options.useWarningOverlay = value.warnings;
|
---|
| 91 | options.useErrorOverlay = value.errors;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | },
|
---|
| 95 | progress: function progress(_progress) {
|
---|
| 96 | if (typeof document !== 'undefined') {
|
---|
| 97 | options.useProgress = _progress;
|
---|
| 98 | }
|
---|
| 99 | },
|
---|
| 100 | 'progress-update': function progressUpdate(data) {
|
---|
| 101 | if (options.useProgress) {
|
---|
| 102 | log.info("[WDS] ".concat(data.percent, "% - ").concat(data.msg, "."));
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | sendMessage('Progress', data);
|
---|
| 106 | },
|
---|
| 107 | ok: function ok() {
|
---|
| 108 | sendMessage('Ok');
|
---|
| 109 |
|
---|
| 110 | if (options.useWarningOverlay || options.useErrorOverlay) {
|
---|
| 111 | overlay.clear();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if (options.initial) {
|
---|
| 115 | return options.initial = false;
|
---|
| 116 | } // eslint-disable-line no-return-assign
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | reloadApp(options, status);
|
---|
| 120 | },
|
---|
| 121 | 'content-changed': function contentChanged() {
|
---|
| 122 | log.info('[WDS] Content base changed. Reloading...');
|
---|
| 123 | self.location.reload();
|
---|
| 124 | },
|
---|
| 125 | warnings: function warnings(_warnings) {
|
---|
| 126 | log.warn('[WDS] Warnings while compiling.');
|
---|
| 127 |
|
---|
| 128 | var strippedWarnings = _warnings.map(function (warning) {
|
---|
| 129 | return stripAnsi(warning);
|
---|
| 130 | });
|
---|
| 131 |
|
---|
| 132 | sendMessage('Warnings', strippedWarnings);
|
---|
| 133 |
|
---|
| 134 | for (var i = 0; i < strippedWarnings.length; i++) {
|
---|
| 135 | log.warn(strippedWarnings[i]);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | if (options.useWarningOverlay) {
|
---|
| 139 | overlay.showMessage(_warnings);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | if (options.initial) {
|
---|
| 143 | return options.initial = false;
|
---|
| 144 | } // eslint-disable-line no-return-assign
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | reloadApp(options, status);
|
---|
| 148 | },
|
---|
| 149 | errors: function errors(_errors) {
|
---|
| 150 | log.error('[WDS] Errors while compiling. Reload prevented.');
|
---|
| 151 |
|
---|
| 152 | var strippedErrors = _errors.map(function (error) {
|
---|
| 153 | return stripAnsi(error);
|
---|
| 154 | });
|
---|
| 155 |
|
---|
| 156 | sendMessage('Errors', strippedErrors);
|
---|
| 157 |
|
---|
| 158 | for (var i = 0; i < strippedErrors.length; i++) {
|
---|
| 159 | log.error(strippedErrors[i]);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | if (options.useErrorOverlay) {
|
---|
| 163 | overlay.showMessage(_errors);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | options.initial = false;
|
---|
| 167 | },
|
---|
| 168 | error: function error(_error) {
|
---|
| 169 | log.error(_error);
|
---|
| 170 | },
|
---|
| 171 | close: function close() {
|
---|
| 172 | log.error('[WDS] Disconnected!');
|
---|
| 173 | sendMessage('Close');
|
---|
| 174 | }
|
---|
| 175 | };
|
---|
| 176 | socket(socketUrl, onSocketMessage); |
---|