Skip to content

refactor: client #3129

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 11 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 23 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

module.exports = {
extends: ['webpack', 'prettier'],
globals: {
document: true,
window: true,
},
parserOptions: {
sourceType: 'script',
ecmaVersion: 10,
ecmaVersion: 2018,
},
env: {
node: true,
es6: true,
},
rules: {
curly: 'error',
Expand All @@ -21,12 +21,30 @@ module.exports = {
'global-require': 'off',
},
overrides: [
{
files: ['client-src/**/*.js'],
env: {
browser: true,
},
},
{
files: ['test/**/*.js'],
rules: {
'no-console': 'off',
},
},
{
files: [
'test/client/**/*.js',
'test/e2e/**/*.js',
'test/fixtures/**/*.js',
'test/server/liveReload-option.test.js',
],
env: {
browser: true,
node: true,
},
},
{
files: ['examples/**/*.js'],
env: {
Expand Down
11 changes: 10 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ module.exports = (api) => {
api.cache(true);

return {
presets: ['@babel/preset-env'],
presets: [
[
'@babel/preset-env',
{
targets: {
node: '0.12',
},
},
],
],
env: {
test: {
plugins: ['@babel/plugin-transform-runtime'],
Expand Down
7 changes: 4 additions & 3 deletions client-src/clients/SockJSClient.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict';

const SockJS = require('sockjs-client/dist/sockjs');
const { log } = require('../default/utils/log');
const SockJS = require('../modules/sockjs-client');
const { log } = require('../utils/log');
const BaseClient = require('./BaseClient');

module.exports = class SockJSClient extends BaseClient {
constructor(url) {
super();

const sockUrl = url.replace(/^(?:chrome-extension|file)/i, 'http');
this.sock = new SockJS(sockUrl);

this.sock = new SockJS(sockUrl);
this.sock.onerror = (err) => {
log.error(err);
};
Expand Down
7 changes: 3 additions & 4 deletions client-src/clients/WebsocketClient.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';

/* global WebSocket */

const { log } = require('../default/utils/log');
const { log } = require('../utils/log');
const BaseClient = require('./BaseClient');

module.exports = class WebsocketClient extends BaseClient {
constructor(url) {
super();

const wsUrl = url.replace(/^(?:http|chrome-extension|file)/i, 'ws');
this.client = new WebSocket(wsUrl);

this.client = new WebSocket(wsUrl);
this.client.onerror = (err) => {
log.error(err);
};
Expand Down
27 changes: 0 additions & 27 deletions client-src/default/webpack.config.js

This file was deleted.

5 changes: 3 additions & 2 deletions client-src/default/index.js → client-src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

/* global __resourceQuery WorkerGlobalScope self */
const stripAnsi = require('../transpiled-modules/strip-ansi');
/* global __resourceQuery WorkerGlobalScope */

const stripAnsi = require('./modules/strip-ansi');
const socket = require('./socket');
const overlay = require('./overlay');
const { log, setLogLevel } = require('./utils/log');
Expand Down
6 changes: 6 additions & 0 deletions client-src/modules/logger/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

// eslint-disable-next-line import/no-extraneous-dependencies
require('core-js/stable/symbol');

module.exports = require('webpack/lib/logging/runtime');
File renamed without changes.
3 changes: 2 additions & 1 deletion client-src/default/socket.js → client-src/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Client =
typeof __webpack_dev_server_client__ !== 'undefined'
? __webpack_dev_server_client__
: // eslint-disable-next-line import/no-unresolved
require('../clients/WebsocketClient');
require('./clients/WebsocketClient');

let retries = 0;
let client = null;
Expand All @@ -37,6 +37,7 @@ const socket = function initSocket(url, handlers) {
// Respectfully copied from the package `got`.
// eslint-disable-next-line no-mixed-operators, no-restricted-properties
const retryInMs = 1000 * Math.pow(2, retries) + Math.random() * 100;

retries += 1;

setTimeout(() => {
Expand Down
17 changes: 0 additions & 17 deletions client-src/sockjs/webpack.config.js

This file was deleted.

3 changes: 0 additions & 3 deletions client-src/transpiled-modules/log.js

This file was deleted.

47 changes: 0 additions & 47 deletions client-src/transpiled-modules/webpack.config.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

/* global self */

const url = require('url');
const getCurrentScriptSource = require('./getCurrentScriptSource');

Expand Down
11 changes: 3 additions & 8 deletions client-src/default/utils/log.js → client-src/utils/log.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
'use strict';

const log = require('../../transpiled-modules/log');
const logger = require('../modules/logger');

const name = 'webpack-dev-server';
// default level is set on the client side, so it does not need
// to be set by the CLI or API
const defaultLevel = 'info';

function setLogLevel(level) {
log.configureDefaultLogger({
level,
});
logger.configureDefaultLogger({ level });
}

setLogLevel(defaultLevel);

module.exports = {
log: log.getLogger(name),
setLogLevel,
};
module.exports = { log: logger.getLogger(name), setLogLevel };
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

/* global WorkerGlobalScope self */

const { log } = require('./log');

function reloadApp(
Expand All @@ -11,10 +9,14 @@ function reloadApp(
if (isUnloading || !hotReload) {
return;
}

if (hot) {
log.info('App hot update...');

const hotEmitter = require('webpack/hot/emitter');

hotEmitter.emit('webpackHotUpdate', currentHash);

if (typeof self !== 'undefined' && self.window) {
// broadcast update to window
self.postMessage(`webpackHotUpdate${currentHash}`, '*');
Expand All @@ -23,13 +25,15 @@ function reloadApp(
// 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

/* global __resourceQuery WorkerGlobalScope self */
/* global __resourceQuery WorkerGlobalScope */

// Send messages to the outside, so plugins can consume it.
function sendMsg(type, data) {
Expand Down
72 changes: 72 additions & 0 deletions client-src/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

const path = require('path');
const webpack = require('webpack');
const { merge } = require('webpack-merge');

const baseForModules = {
devtool: false,
mode: 'development',
output: {
path: path.resolve(__dirname, '../client/modules'),
libraryTarget: 'commonjs2',
},
target: webpack.webpack ? ['web', 'es5'] : 'web',
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
},
],
},
],
},
};

module.exports = [
merge(baseForModules, {
entry: path.join(__dirname, 'modules/logger/index.js'),
output: {
filename: 'logger/index.js',
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
plugins: ['@babel/plugin-transform-object-assign'],
},
},
],
},
],
},
plugins: [
new webpack.NormalModuleReplacementPlugin(
/^tapable\/lib\/SyncBailHook/,
path.join(__dirname, 'modules/logger/SyncBailHookFake.js')
),
],
}),
merge(baseForModules, {
entry: path.join(__dirname, 'modules/strip-ansi/index.js'),
output: {
filename: 'strip-ansi/index.js',
},
}),
merge(baseForModules, {
entry: path.join(__dirname, 'modules/sockjs-client/index.js'),
output: {
filename: 'sockjs-client/index.js',
library: 'SockJS',
libraryTarget: 'umd',
globalObject: "(typeof self !== 'undefined' ? self : this)",
},
}),
];
Loading