-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(client): separate into modules (#1948)
- Loading branch information
1 parent
fb31770
commit ffb2c79
Showing
12 changed files
with
340 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ npm-debug.log | |
.nyc_output | ||
|
||
client | ||
!/test/client | ||
coverage | ||
ssl/*.pem | ||
node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
function getCurrentScriptSource() { | ||
// `document.currentScript` is the most accurate way to find the current script, | ||
// but is not supported in all browsers. | ||
if (document.currentScript) { | ||
return document.currentScript.getAttribute('src'); | ||
} | ||
// Fall back to getting all scripts in the document. | ||
const scriptElements = document.scripts || []; | ||
const currentScript = scriptElements[scriptElements.length - 1]; | ||
|
||
if (currentScript) { | ||
return currentScript.getAttribute('src'); | ||
} | ||
// Fail as there was no script to use. | ||
throw new Error('[WDS] Failed to get current script source.'); | ||
} | ||
|
||
module.exports = getCurrentScriptSource; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
/* global WorkerGlobalScope self */ | ||
|
||
const log = require('loglevel').getLogger('webpack-dev-server'); | ||
|
||
function reloadApp( | ||
{ hotReload, hot, liveReload }, | ||
{ isUnloading, currentHash } | ||
) { | ||
if (isUnloading || !hotReload) { | ||
return; | ||
} | ||
if (hot) { | ||
log.info('[WDS] App hot update...'); | ||
// eslint-disable-next-line global-require | ||
const hotEmitter = require('webpack/hot/emitter'); | ||
hotEmitter.emit('webpackHotUpdate', currentHash); | ||
if (typeof self !== 'undefined' && self.window) { | ||
// broadcast update to window | ||
self.postMessage(`webpackHotUpdate${currentHash}`, '*'); | ||
} | ||
} | ||
// allow refreshing the page only if liveReload isn't disabled | ||
else if (liveReload) { | ||
let rootWindow = self; | ||
// use parent window for reload (in case we're in an iframe with no valid src) | ||
const intervalId = self.setInterval(() => { | ||
if (rootWindow.location.protocol !== 'about:') { | ||
// reload immediately if protocol is valid | ||
applyReload(rootWindow, intervalId); | ||
} else { | ||
rootWindow = rootWindow.parent; | ||
if (rootWindow.parent === rootWindow) { | ||
// if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways | ||
applyReload(rootWindow, intervalId); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function applyReload(rootWindow, intervalId) { | ||
clearInterval(intervalId); | ||
log.info('[WDS] App updated. Reloading...'); | ||
rootWindow.location.reload(); | ||
} | ||
} | ||
|
||
module.exports = reloadApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
/* global __resourceQuery WorkerGlobalScope self */ | ||
|
||
// Send messages to the outside, so plugins can consume it. | ||
function sendMsg(type, data) { | ||
if ( | ||
typeof self !== 'undefined' && | ||
(typeof WorkerGlobalScope === 'undefined' || | ||
!(self instanceof WorkerGlobalScope)) | ||
) { | ||
self.postMessage( | ||
{ | ||
type: `webpack${type}`, | ||
data, | ||
}, | ||
'*' | ||
); | ||
} | ||
} | ||
|
||
module.exports = sendMsg; |
Large diffs are not rendered by default.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
test/client/default/utils/__snapshots__/getCurrentScriptSource.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getCurrentScriptSource should fail when script source doesn't exist 1`] = `[Error: [WDS] Failed to get current script source.]`; |
Oops, something went wrong.