Skip to content

Commit 2ee9cbf

Browse files
committed
refactor(client): make use of ESM instead of CJS (#1967)
1 parent 260063f commit 2ee9cbf

23 files changed

+62
-92
lines changed

.eslintrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"extends": ["webpack", "prettier"],
33
"globals": {
44
"document": true,
5-
"window": true
5+
"window": true,
6+
"self": true,
7+
"WorkerGlobalScope": true,
8+
"__resourceQuery": true,
9+
"__webpack_dev_server_client__": true
610
},
711
"parserOptions": {
812
"sourceType": "script",

client-src/.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"parserOptions": {
3+
"sourceType": "module"
4+
}
5+
}

client-src/clients/BaseClient.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
/* eslint-disable
42
no-unused-vars
53
*/

client-src/clients/SockJSClient.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
/* eslint-disable
42
no-unused-vars
53
*/

client-src/clients/WebsocketClient.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
/* global WebSocket */
42

53
/* eslint-disable

client-src/default/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
'use strict';
2-
3-
/* global __resourceQuery WorkerGlobalScope self */
41
/* eslint prefer-destructuring: off */
5-
const stripAnsi = require('strip-ansi');
6-
const socket = require('./socket');
7-
const overlay = require('./overlay');
8-
const { log, setLogLevel } = require('./utils/log');
9-
const sendMessage = require('./utils/sendMessage');
10-
const reloadApp = require('./utils/reloadApp');
11-
const createSocketUrl = require('./utils/createSocketUrl');
2+
import stripAnsi from 'strip-ansi';
3+
import socket from './socket';
4+
import {
5+
clear as clearOverlay,
6+
showMessage as showMessageOverlay,
7+
} from './overlay';
8+
import { log, setLogLevel } from './utils/log';
9+
import sendMessage from './utils/sendMessage';
10+
import reloadApp from './utils/reloadApp';
11+
import createSocketUrl from './utils/createSocketUrl';
1212

1313
const status = {
1414
isUnloading: false,
@@ -47,7 +47,7 @@ const onSocketMessage = {
4747
log.info('[WDS] App updated. Recompiling...');
4848
// fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
4949
if (options.useWarningOverlay || options.useErrorOverlay) {
50-
overlay.clear();
50+
clearOverlay();
5151
}
5252
sendMessage('Invalid');
5353
},
@@ -57,7 +57,7 @@ const onSocketMessage = {
5757
'still-ok': function stillOk() {
5858
log.info('[WDS] Nothing changed.');
5959
if (options.useWarningOverlay || options.useErrorOverlay) {
60-
overlay.clear();
60+
clearOverlay();
6161
}
6262
sendMessage('StillOk');
6363
},
@@ -93,7 +93,7 @@ const onSocketMessage = {
9393
ok() {
9494
sendMessage('Ok');
9595
if (options.useWarningOverlay || options.useErrorOverlay) {
96-
overlay.clear();
96+
clearOverlay();
9797
}
9898
if (options.initial) {
9999
return (options.initial = false);
@@ -112,7 +112,7 @@ const onSocketMessage = {
112112
log.warn(strippedWarnings[i]);
113113
}
114114
if (options.useWarningOverlay) {
115-
overlay.showMessage(warnings);
115+
showMessageOverlay(warnings);
116116
}
117117

118118
if (options.initial) {
@@ -128,7 +128,7 @@ const onSocketMessage = {
128128
log.error(strippedErrors[i]);
129129
}
130130
if (options.useErrorOverlay) {
131-
overlay.showMessage(errors);
131+
showMessageOverlay(errors);
132132
}
133133
options.initial = false;
134134
},

client-src/default/overlay.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict';
2-
31
// The error overlay is inspired (and mostly copied) from Create React App (https://github.com/facebookincubator/create-react-app)
42
// They, in turn, got inspired by webpack-hot-middleware (https://github.com/glenjamin/webpack-hot-middleware).
53

6-
const ansiHTML = require('ansi-html');
7-
const { AllHtmlEntities } = require('html-entities');
4+
import ansiHTML from 'ansi-html';
5+
import { AllHtmlEntities as Entities } from 'html-entities';
86

9-
const entities = new AllHtmlEntities();
7+
const entities = new Entities();
108
const colors = {
119
reset: ['transparent', 'transparent'],
1210
black: '181818',
@@ -95,8 +93,8 @@ function ensureOverlayDivExists(onOverlayDivReady) {
9593
document.body.appendChild(overlayIframe);
9694
}
9795

98-
// Successful compilation.
99-
function clear() {
96+
// successful compilation.
97+
export function clear() {
10098
if (!overlayDiv) {
10199
// It is not there in the first place.
102100
return;
@@ -110,7 +108,7 @@ function clear() {
110108
}
111109

112110
// Compilation with errors (e.g. syntax error or missing modules).
113-
function showMessage(messages) {
111+
export function showMessage(messages) {
114112
ensureOverlayDivExists((div) => {
115113
// Make it look similar to our terminal.
116114
div.innerHTML = `<span style="color: #${
@@ -120,8 +118,3 @@ function showMessage(messages) {
120118
)}`;
121119
});
122120
}
123-
124-
module.exports = {
125-
clear,
126-
showMessage,
127-
};

client-src/default/socket.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
'use strict';
2-
3-
/* global __webpack_dev_server_client__ */
41
/* eslint-disable
52
camelcase
63
*/
@@ -58,4 +55,4 @@ const socket = function initSocket(url, handlers) {
5855
});
5956
};
6057

61-
module.exports = socket;
58+
export default socket;

client-src/default/utils/createSocketUrl.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
'use strict';
2-
3-
/* global self */
4-
5-
const url = require('url');
6-
const getCurrentScriptSource = require('./getCurrentScriptSource');
1+
import url from 'url';
2+
import querystring from 'querystring';
3+
import getCurrentScriptSource from './getCurrentScriptSource';
74

85
function createSocketUrl(resourceQuery, currentLocation) {
96
let urlParts;
@@ -94,4 +91,4 @@ function getSocketUrl(urlParts, loc) {
9491
});
9592
}
9693

97-
module.exports = createSocketUrl;
94+
export default createSocketUrl;

client-src/default/utils/getCurrentScriptSource.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
function getCurrentScriptSource() {
42
// `document.currentScript` is the most accurate way to find the current script,
53
// but is not supported in all browsers.
@@ -17,4 +15,4 @@ function getCurrentScriptSource() {
1715
throw new Error('[WDS] Failed to get current script source.');
1816
}
1917

20-
module.exports = getCurrentScriptSource;
18+
export default getCurrentScriptSource;

0 commit comments

Comments
 (0)