Skip to content
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

fix(client): Make public path on client match dev server publicPath #2055

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
06f6c53
chore: don't support Node6 (#1976)
hiroppy Jun 5, 2019
af76891
refactor(client): make use of ESM instead of CJS (#1967)
hiroppy Jun 5, 2019
fad833a
chore(deps): update dependency ws to v7 (#1834)
renovate[bot] Jun 5, 2019
b32f7a3
chore(deps): update dependency file-loader to v4 (#1971)
renovate[bot] Jun 5, 2019
342fa6a
chore(deps): update dependency chokidar to v3 (#1902)
renovate[bot] Jun 5, 2019
7901428
chore(deps): introduce open and remove opn (#1865)
hiroppy Jun 5, 2019
898461a
chore: fix tests (#1997)
hiroppy Jun 8, 2019
7526664
test: make use of async/await (#1996)
hiroppy Jun 10, 2019
ec86384
feat(client): delete `none` and `warning` from clientLogLevel (#1998)
hiroppy Jun 10, 2019
742aa13
chore: fix code from master
hiroppy Jun 10, 2019
b136055
chore(deps): update dependency p-retry to v4 (#2009)
hiroppy Jun 10, 2019
50b99b6
fix(options): delete none and warning from optinos.json
hiroppy Jun 15, 2019
5c0dced
chore(deps): update url-loader to 2.1.0
hiroppy Aug 5, 2019
1679797
test: update syntax
hiroppy Aug 5, 2019
c600b0e
refactor(server): remove some variables of options (#2175)
hiroppy Aug 5, 2019
27b1913
chore(deps): update del, supports-color, import-local and execa (#2178)
hiroppy Aug 6, 2019
e819cfe
feat(server): add stdin option to API (#2186)
knagaitsev Aug 10, 2019
64036a0
refactor(server): move socket handling into server.listen (#2061)
knagaitsev Aug 22, 2019
0e875f8
feat(server): async user callback for server.listen (#2218)
knagaitsev Sep 24, 2019
fe80fa4
Union sock options (#2047)
EslamHiko Sep 25, 2019
54a7c77
test(client): added and improved tests for public path updating
knagaitsev Jun 22, 2019
ed5ee20
test(client): updated cli and index tests
knagaitsev Jun 22, 2019
df594ad
refactor(client): updated to latest module importing
knagaitsev Aug 10, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"extends": ["webpack", "prettier"],
"globals": {
"document": true,
"window": true
"window": true,
"self": true,
"WorkerGlobalScope": true,
"__resourceQuery": true,
"__webpack_dev_server_client__": true
},
"parserOptions": {
"sourceType": "script",
Expand Down
9 changes: 0 additions & 9 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ jobs:
node-8:
node_version: ^8.9.0
webpack_version: latest
node-6:
node_version: ^6.9.0
webpack_version: latest
steps:
- task: NodeTool@0
inputs:
Expand Down Expand Up @@ -98,9 +95,6 @@ jobs:
node-8:
node_version: ^8.9.0
webpack_version: latest
node-6:
node_version: ^6.9.0
webpack_version: latest
steps:
- task: NodeTool@0
inputs:
Expand Down Expand Up @@ -149,9 +143,6 @@ jobs:
node-8:
node_version: ^8.9.0
webpack_version: latest
node-6:
node_version: ^6.9.0
webpack_version: latest
steps:
- script: 'git config --global core.autocrlf input'
displayName: 'Config git core.autocrlf'
Expand Down
54 changes: 7 additions & 47 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

/* eslint-disable no-shadow, no-console */

const fs = require('fs');
const net = require('net');
const debug = require('debug')('webpack-dev-server');
const importLocal = require('import-local');
const yargs = require('yargs');
Expand Down Expand Up @@ -115,51 +113,13 @@ function startDevServer(config, options) {
throw err;
}

if (options.socket) {
server.listeningApp.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
const clientSocket = new net.Socket();

clientSocket.on('error', (err) => {
if (err.code === 'ECONNREFUSED') {
// No other server listening on this socket so it can be safely removed
fs.unlinkSync(options.socket);

server.listen(options.socket, options.host, (error) => {
if (error) {
throw error;
}
});
}
});

clientSocket.connect({ path: options.socket }, () => {
throw new Error('This socket is already used');
});
}
});

server.listen(options.socket, options.host, (err) => {
if (err) {
throw err;
}

// chmod 666 (rw rw rw)
const READ_WRITE = 438;

fs.chmod(options.socket, READ_WRITE, (err) => {
if (err) {
throw err;
}
});
});
} else {
server.listen(options.port, options.host, (err) => {
if (err) {
throw err;
}
});
}
// options.socket does not have a default value, so it will only be set
// if the user sets it explicitly
server.listen(options.socket || options.port, options.host, (err) => {
if (err) {
throw err;
}
});
}

processOptions(config, argv, (config, options) => {
Expand Down
5 changes: 5 additions & 0 deletions client-src/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"parserOptions": {
"sourceType": "module"
}
}
2 changes: 0 additions & 2 deletions client-src/clients/BaseClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

/* eslint-disable
no-unused-vars
*/
Expand Down
2 changes: 0 additions & 2 deletions client-src/clients/SockJSClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

/* eslint-disable
no-unused-vars
*/
Expand Down
2 changes: 0 additions & 2 deletions client-src/clients/WebsocketClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

/* global WebSocket */

/* eslint-disable
Expand Down
33 changes: 18 additions & 15 deletions client-src/default/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';

/* global __resourceQuery WorkerGlobalScope self */
/* eslint prefer-destructuring: off */
const stripAnsi = require('strip-ansi');
const socket = require('./socket');
const overlay = require('./overlay');
const { log, setLogLevel } = require('./utils/log');
const sendMessage = require('./utils/sendMessage');
const reloadApp = require('./utils/reloadApp');
const createSocketUrl = require('./utils/createSocketUrl');
import stripAnsi from 'strip-ansi';
import socket from './socket';
import {
clear as clearOverlay,
showMessage as showMessageOverlay,
} from './overlay';
import { log, setLogLevel } from './utils/log';
import sendMessage from './utils/sendMessage';
import reloadApp from './utils/reloadApp';
import createSocketUrl from './utils/createSocketUrl';
import updatePublicPath from './utils/updatePublicPath';

updatePublicPath(__resourceQuery);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid what we change global variable and it is can break code in some rare case, maybe we can rewrite this on using own variable like:

const resourceQuery = getPublicPath(__resourceQuery);

Copy link
Collaborator Author

@knagaitsev knagaitsev Jun 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the changing of the global variable __webpack_public_path__, right?

Good point, but the problem is that hot-update.json is being requested using the global public path variable here: https://github.com/webpack/webpack/blob/master/lib/web/JsonpMainTemplate.runtime.js#L31, so that global public path variable needs to be changed, or else we need to modify core webpack.

I think it is safe enough to do this:

__webpack_public_path__ = __webpack_public_path__ || publicPath;

so if it is already set, we will not change it.


const status = {
isUnloading: false,
Expand Down Expand Up @@ -47,7 +50,7 @@ const onSocketMessage = {
log.info('[WDS] App updated. Recompiling...');
// fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
if (options.useWarningOverlay || options.useErrorOverlay) {
overlay.clear();
clearOverlay();
}
sendMessage('Invalid');
},
Expand All @@ -57,7 +60,7 @@ const onSocketMessage = {
'still-ok': function stillOk() {
log.info('[WDS] Nothing changed.');
if (options.useWarningOverlay || options.useErrorOverlay) {
overlay.clear();
clearOverlay();
}
sendMessage('StillOk');
},
Expand Down Expand Up @@ -93,7 +96,7 @@ const onSocketMessage = {
ok() {
sendMessage('Ok');
if (options.useWarningOverlay || options.useErrorOverlay) {
overlay.clear();
clearOverlay();
}
if (options.initial) {
return (options.initial = false);
Expand All @@ -112,7 +115,7 @@ const onSocketMessage = {
log.warn(strippedWarnings[i]);
}
if (options.useWarningOverlay) {
overlay.showMessage(warnings);
showMessageOverlay(warnings);
}

if (options.initial) {
Expand All @@ -128,7 +131,7 @@ const onSocketMessage = {
log.error(strippedErrors[i]);
}
if (options.useErrorOverlay) {
overlay.showMessage(errors);
showMessageOverlay(errors);
}
options.initial = false;
},
Expand Down
19 changes: 6 additions & 13 deletions client-src/default/overlay.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';

// The error overlay is inspired (and mostly copied) from Create React App (https://github.com/facebookincubator/create-react-app)
// They, in turn, got inspired by webpack-hot-middleware (https://github.com/glenjamin/webpack-hot-middleware).

const ansiHTML = require('ansi-html');
const { AllHtmlEntities } = require('html-entities');
import ansiHTML from 'ansi-html';
import { AllHtmlEntities as Entities } from 'html-entities';

const entities = new AllHtmlEntities();
const entities = new Entities();
const colors = {
reset: ['transparent', 'transparent'],
black: '181818',
Expand Down Expand Up @@ -95,8 +93,8 @@ function ensureOverlayDivExists(onOverlayDivReady) {
document.body.appendChild(overlayIframe);
}

// Successful compilation.
function clear() {
// successful compilation.
export function clear() {
if (!overlayDiv) {
// It is not there in the first place.
return;
Expand All @@ -110,7 +108,7 @@ function clear() {
}

// Compilation with errors (e.g. syntax error or missing modules).
function showMessage(messages) {
export function showMessage(messages) {
ensureOverlayDivExists((div) => {
// Make it look similar to our terminal.
div.innerHTML = `<span style="color: #${
Expand All @@ -120,8 +118,3 @@ function showMessage(messages) {
)}`;
});
}

module.exports = {
clear,
showMessage,
};
5 changes: 1 addition & 4 deletions client-src/default/socket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
'use strict';

/* global __webpack_dev_server_client__ */
/* eslint-disable
camelcase
*/
Expand Down Expand Up @@ -58,4 +55,4 @@ const socket = function initSocket(url, handlers) {
});
};

module.exports = socket;
export default socket;
84 changes: 9 additions & 75 deletions client-src/default/utils/createSocketUrl.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,19 @@
'use strict';

/* global self */

const url = require('url');
const querystring = require('querystring');
const getCurrentScriptSource = require('./getCurrentScriptSource');
import url from 'url';
import getUrlParts from './getUrlParts';

function createSocketUrl(resourceQuery) {
let urlParts;

if (typeof resourceQuery === 'string' && resourceQuery !== '') {
// If this bundle is inlined, use the resource query to get the correct url.
urlParts = url.parse(resourceQuery.substr(1));
} else {
// Else, get the url from the <script> this file was called with.
let scriptHost = getCurrentScriptSource();

// eslint-disable-next-line no-useless-escape
scriptHost = scriptHost.replace(/\/[^\/]+$/, '');
urlParts = url.parse(scriptHost || '/', false, true);
}

if (!urlParts.port || urlParts.port === '0') {
urlParts.port = self.location.port;
}

const { auth, path } = urlParts;
let { hostname, protocol } = urlParts;

// check ipv4 and ipv6 `all hostname`
// why do we need this check?
// hostname n/a for file protocol (example, when using electron, ionic)
// see: https://github.com/webpack/webpack-dev-server/pull/384
const isAnyHostname =
(hostname === '0.0.0.0' || hostname === '::') &&
self.location.hostname &&
// eslint-disable-next-line no-bitwise
!!~self.location.protocol.indexOf('http');

if (isAnyHostname) {
hostname = self.location.hostname;
}

// `hostname` can be empty when the script path is relative. In that case, specifying
// a protocol would result in an invalid URL.
// When https is used in the app, secure websockets are always necessary
// because the browser doesn't accept non-secure websockets.
if (
hostname &&
(self.location.protocol === 'https:' || urlParts.hostname === '0.0.0.0')
) {
protocol = self.location.protocol;
}

// default values of the sock url if they are not provided
let sockHost = hostname;
let sockPath = '/sockjs-node';
let sockPort = urlParts.port;

// eslint-disable-next-line no-undefined
const shouldParsePath = path !== null && path !== undefined && path !== '/';
if (shouldParsePath) {
const parsedQuery = querystring.parse(path);
// all of these sock url params are optionally passed in through
// resourceQuery, so we need to fall back to the default if
// they are not provided
sockHost = parsedQuery.sockHost || sockHost;
sockPath = parsedQuery.sockPath || sockPath;
sockPort = parsedQuery.sockPort || sockPort;
}
const urlParts = getUrlParts(resourceQuery);

return url.format({
protocol,
auth,
hostname: sockHost,
port: sockPort,
protocol: urlParts.protocol,
auth: urlParts.auth,
hostname: urlParts.sockHost,
port: urlParts.sockPort,
// If sockPath is provided it'll be passed in via the resourceQuery as a
// query param so it has to be parsed out of the querystring in order for the
// client to open the socket to the correct location.
pathname: sockPath,
pathname: urlParts.sockPath,
});
}

module.exports = createSocketUrl;
export default createSocketUrl;
4 changes: 1 addition & 3 deletions client-src/default/utils/getCurrentScriptSource.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

function getCurrentScriptSource() {
// `document.currentScript` is the most accurate way to find the current script,
// but is not supported in all browsers.
Expand All @@ -17,4 +15,4 @@ function getCurrentScriptSource() {
throw new Error('[WDS] Failed to get current script source.');
}

module.exports = getCurrentScriptSource;
export default getCurrentScriptSource;
Loading