Skip to content

Commit 9307755

Browse files
feat: use ECMA modules in client (#3550)
1 parent 977a70d commit 9307755

33 files changed

+135
-105
lines changed

.eslintrc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,20 @@ module.exports = {
2424
overrides: [
2525
{
2626
files: ["client-src/**/*.js"],
27+
excludedFiles: [
28+
"client-src/webpack.config.js",
29+
"client-src/modules/logger/SyncBailHookFake.js",
30+
],
31+
parserOptions: {
32+
sourceType: "module",
33+
allowImportExportEverywhere: true,
34+
},
2735
env: {
2836
browser: true,
2937
},
38+
rules: {
39+
"import/extensions": ["error", "always"],
40+
},
3041
},
3142
{
3243
files: ["test/**/*.js"],

babel.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ module.exports = (api) => {
88
[
99
"@babel/preset-env",
1010
{
11+
modules: false,
1112
targets: {
13+
esmodules: true,
1214
node: "0.12",
1315
},
1416
},
@@ -17,6 +19,16 @@ module.exports = (api) => {
1719
plugins: ["@babel/plugin-transform-object-assign"],
1820
env: {
1921
test: {
22+
presets: [
23+
[
24+
"@babel/preset-env",
25+
{
26+
targets: {
27+
node: "12.13.0",
28+
},
29+
},
30+
],
31+
],
2032
plugins: ["@babel/plugin-transform-runtime"],
2133
},
2234
},

client-src/clients/SockJSClient.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
"use strict";
1+
import SockJS from "../modules/sockjs-client/index.js";
2+
import { log } from "../utils/log.js";
23

3-
const SockJS = require("../modules/sockjs-client");
4-
const { log } = require("../utils/log");
5-
6-
module.exports = class SockJSClient {
4+
export default class SockJSClient {
75
constructor(url) {
86
// SockJS requires `http` and `https` protocols
97
this.sock = new SockJS(
@@ -28,4 +26,4 @@ module.exports = class SockJSClient {
2826
f(e.data);
2927
};
3028
}
31-
};
29+
}

client-src/clients/WebsocketClient.js renamed to client-src/clients/WebSocketClient.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
"use strict";
1+
import { log } from "../utils/log.js";
22

3-
const { log } = require("../utils/log");
4-
5-
module.exports = class WebsocketClient {
3+
export default class WebSocketClient {
64
constructor(url) {
75
this.client = new WebSocket(url);
86
this.client.onerror = (error) => {
@@ -24,4 +22,4 @@ module.exports = class WebsocketClient {
2422
f(e.data);
2523
};
2624
}
27-
};
25+
}

client-src/index.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
"use strict";
2-
3-
/* global __resourceQuery WorkerGlobalScope */
4-
5-
const webpackHotLog = require("webpack/hot/log");
6-
const stripAnsi = require("./modules/strip-ansi");
7-
const parseURL = require("./utils/parseURL");
8-
const socket = require("./socket");
9-
const overlay = require("./overlay");
10-
const { log, setLogLevel } = require("./utils/log");
11-
const sendMessage = require("./utils/sendMessage");
12-
const reloadApp = require("./utils/reloadApp");
13-
const createSocketURL = require("./utils/createSocketURL");
1+
/* global __resourceQuery */
2+
3+
import webpackHotLog from "webpack/hot/log.js";
4+
import stripAnsi from "./modules/strip-ansi/index.js";
5+
import parseURL from "./utils/parseURL.js";
6+
import socket from "./socket.js";
7+
import { show, hide } from "./overlay.js";
8+
import { log, setLogLevel } from "./utils/log.js";
9+
import sendMessage from "./utils/sendMessage.js";
10+
import reloadApp from "./utils/reloadApp.js";
11+
import createSocketURL from "./utils/createSocketURL.js";
1412

1513
const status = { isUnloading: false, currentHash: "" };
1614
const options = {
@@ -66,7 +64,7 @@ const onSocketMessage = {
6664

6765
// Fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
6866
if (options.overlay) {
69-
overlay.hide();
67+
hide();
7068
}
7169

7270
sendMessage("Invalid");
@@ -100,7 +98,7 @@ const onSocketMessage = {
10098
log.info("Nothing changed.");
10199

102100
if (options.overlay) {
103-
overlay.hide();
101+
hide();
104102
}
105103

106104
sendMessage("StillOk");
@@ -109,7 +107,7 @@ const onSocketMessage = {
109107
sendMessage("Ok");
110108

111109
if (options.overlay) {
112-
overlay.hide();
110+
hide();
113111
}
114112

115113
if (options.initial) {
@@ -156,7 +154,7 @@ const onSocketMessage = {
156154
: options.overlay && options.overlay.warnings;
157155

158156
if (needShowOverlay) {
159-
overlay.show(warnings, "warnings");
157+
show(warnings, "warnings");
160158
}
161159

162160
if (options.initial) {
@@ -184,7 +182,7 @@ const onSocketMessage = {
184182
: options.overlay && options.overlay.errors;
185183

186184
if (needShowOverlay) {
187-
overlay.show(errors, "errors");
185+
show(errors, "errors");
188186
}
189187

190188
options.initial = false;

client-src/modules/logger/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
"use strict";
2-
3-
module.exports = require("webpack/lib/logging/runtime");
1+
export { default } from "webpack/lib/logging/runtime.js";
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
"use strict";
2-
31
// eslint-disable-next-line import/no-extraneous-dependencies
4-
module.exports = require("sockjs-client");
2+
export { default } from "sockjs-client";
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
"use strict";
1+
import stripAnsi from "strip-ansi";
22

3-
module.exports = require("strip-ansi").default;
3+
export default stripAnsi;

client-src/overlay.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
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

@@ -154,4 +152,4 @@ function show(messages, type) {
154152
});
155153
}
156154

157-
module.exports = { show, hide };
155+
export { show, hide };

client-src/socket.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
"use strict";
2-
31
/* global __webpack_dev_server_client__ */
4-
/* eslint-disable
5-
camelcase
6-
*/
2+
3+
import WebSocketClient from "./clients/WebSocketClient.js";
74

85
// this WebsocketClient is here as a default fallback, in case the client is not injected
6+
/* eslint-disable camelcase */
97
const Client =
8+
// eslint-disable-next-line camelcase, no-nested-ternary
109
typeof __webpack_dev_server_client__ !== "undefined"
11-
? __webpack_dev_server_client__
12-
: // eslint-disable-next-line import/no-unresolved
13-
require("./clients/WebsocketClient");
10+
? // eslint-disable-next-line camelcase
11+
typeof __webpack_dev_server_client__.default !== "undefined"
12+
? __webpack_dev_server_client__.default
13+
: __webpack_dev_server_client__
14+
: WebSocketClient;
15+
/* eslint-enable camelcase */
1416

1517
let retries = 0;
1618
let client = null;
@@ -54,4 +56,4 @@ const socket = function initSocket(url, handlers) {
5456
});
5557
};
5658

57-
module.exports = socket;
59+
export default socket;

0 commit comments

Comments
 (0)