-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
refactor(client): separate into modules #1948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.