Skip to content

refactor: es modules in client #3550

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 17 commits into from
Jul 22, 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
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@ module.exports = {
overrides: [
{
files: ["client-src/**/*.js"],
excludedFiles: [
"client-src/webpack.config.js",
"client-src/modules/logger/SyncBailHookFake.js",
],
parserOptions: {
sourceType: "module",
allowImportExportEverywhere: true,
},
env: {
browser: true,
},
rules: {
"import/extensions": ["error", "always"],
},
},
{
files: ["test/**/*.js"],
Expand Down
12 changes: 12 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ module.exports = (api) => {
[
"@babel/preset-env",
{
modules: false,
targets: {
esmodules: true,
node: "0.12",
},
},
Expand All @@ -17,6 +19,16 @@ module.exports = (api) => {
plugins: ["@babel/plugin-transform-object-assign"],
env: {
test: {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "12.13.0",
},
},
],
],
plugins: ["@babel/plugin-transform-runtime"],
},
},
Expand Down
10 changes: 4 additions & 6 deletions client-src/clients/SockJSClient.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use strict";
import SockJS from "../modules/sockjs-client/index.js";
import { log } from "../utils/log.js";

const SockJS = require("../modules/sockjs-client");
const { log } = require("../utils/log");

module.exports = class SockJSClient {
export default class SockJSClient {
constructor(url) {
// SockJS requires `http` and `https` protocols
this.sock = new SockJS(
Expand All @@ -28,4 +26,4 @@ module.exports = class SockJSClient {
f(e.data);
};
}
};
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
import { log } from "../utils/log.js";

const { log } = require("../utils/log");

module.exports = class WebsocketClient {
export default class WebSocketClient {
constructor(url) {
this.client = new WebSocket(url);
this.client.onerror = (error) => {
Expand All @@ -24,4 +22,4 @@ module.exports = class WebsocketClient {
f(e.data);
};
}
};
}
34 changes: 16 additions & 18 deletions client-src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"use strict";

/* global __resourceQuery WorkerGlobalScope */

const webpackHotLog = require("webpack/hot/log");
const stripAnsi = require("./modules/strip-ansi");
const parseURL = require("./utils/parseURL");
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");
/* global __resourceQuery */

import webpackHotLog from "webpack/hot/log.js";
import stripAnsi from "./modules/strip-ansi/index.js";
import parseURL from "./utils/parseURL.js";
import socket from "./socket.js";
import { show, hide } from "./overlay.js";
import { log, setLogLevel } from "./utils/log.js";
import sendMessage from "./utils/sendMessage.js";
import reloadApp from "./utils/reloadApp.js";
import createSocketURL from "./utils/createSocketURL.js";

const status = { isUnloading: false, currentHash: "" };
const options = {
Expand Down Expand Up @@ -66,7 +64,7 @@ const onSocketMessage = {

// Fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
if (options.overlay) {
overlay.hide();
hide();
}

sendMessage("Invalid");
Expand Down Expand Up @@ -100,7 +98,7 @@ const onSocketMessage = {
log.info("Nothing changed.");

if (options.overlay) {
overlay.hide();
hide();
}

sendMessage("StillOk");
Expand All @@ -109,7 +107,7 @@ const onSocketMessage = {
sendMessage("Ok");

if (options.overlay) {
overlay.hide();
hide();
}

if (options.initial) {
Expand Down Expand Up @@ -156,7 +154,7 @@ const onSocketMessage = {
: options.overlay && options.overlay.warnings;

if (needShowOverlay) {
overlay.show(warnings, "warnings");
show(warnings, "warnings");
}

if (options.initial) {
Expand Down Expand Up @@ -184,7 +182,7 @@ const onSocketMessage = {
: options.overlay && options.overlay.errors;

if (needShowOverlay) {
overlay.show(errors, "errors");
show(errors, "errors");
}

options.initial = false;
Expand Down
4 changes: 1 addition & 3 deletions client-src/modules/logger/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
"use strict";

module.exports = require("webpack/lib/logging/runtime");
export { default } from "webpack/lib/logging/runtime.js";
4 changes: 1 addition & 3 deletions client-src/modules/sockjs-client/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
"use strict";

// eslint-disable-next-line import/no-extraneous-dependencies
module.exports = require("sockjs-client");
export { default } from "sockjs-client";
4 changes: 2 additions & 2 deletions client-src/modules/strip-ansi/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"use strict";
import stripAnsi from "strip-ansi";

module.exports = require("strip-ansi").default;
export default stripAnsi;
4 changes: 1 addition & 3 deletions client-src/overlay.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"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).

Expand Down Expand Up @@ -154,4 +152,4 @@ function show(messages, type) {
});
}

module.exports = { show, hide };
export { show, hide };
20 changes: 11 additions & 9 deletions client-src/socket.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
"use strict";

/* global __webpack_dev_server_client__ */
/* eslint-disable
camelcase
*/

import WebSocketClient from "./clients/WebSocketClient.js";

// this WebsocketClient is here as a default fallback, in case the client is not injected
/* eslint-disable camelcase */
const Client =
// eslint-disable-next-line camelcase, no-nested-ternary
typeof __webpack_dev_server_client__ !== "undefined"
? __webpack_dev_server_client__
: // eslint-disable-next-line import/no-unresolved
require("./clients/WebsocketClient");
? // eslint-disable-next-line camelcase
typeof __webpack_dev_server_client__.default !== "undefined"
? __webpack_dev_server_client__.default
: __webpack_dev_server_client__
: WebSocketClient;
/* eslint-enable camelcase */

let retries = 0;
let client = null;
Expand Down Expand Up @@ -54,4 +56,4 @@ const socket = function initSocket(url, handlers) {
});
};

module.exports = socket;
export default socket;
6 changes: 2 additions & 4 deletions client-src/utils/createSocketURL.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

const url = require("url");
import url from "url";

// We handle legacy API that is Node.js specific, and a newer API that implements the same WHATWG URL Standard used by web browsers
// Please look at https://nodejs.org/api/url.html#url_url_strings_and_url_objects
Expand Down Expand Up @@ -92,4 +90,4 @@ function createSocketURL(parsedURL) {
});
}

module.exports = createSocketURL;
export default createSocketURL;
4 changes: 1 addition & 3 deletions client-src/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 @@ -25,4 +23,4 @@ function getCurrentScriptSource() {
throw new Error("[webpack-dev-server] Failed to get current script source.");
}

module.exports = getCurrentScriptSource;
export default getCurrentScriptSource;
8 changes: 4 additions & 4 deletions client-src/utils/log.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

const logger = require("../modules/logger");
import logger from "../modules/logger/index.js";

const name = "webpack-dev-server";
// default level is set on the client side, so it does not need
Expand All @@ -13,4 +11,6 @@ function setLogLevel(level) {

setLogLevel(defaultLevel);

module.exports = { log: logger.getLogger(name), setLogLevel };
const log = logger.getLogger(name);

export { log, setLogLevel };
8 changes: 3 additions & 5 deletions client-src/utils/parseURL.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"use strict";

const url = require("url");
const getCurrentScriptSource = require("./getCurrentScriptSource");
import url from "url";
import getCurrentScriptSource from "./getCurrentScriptSource.js";

function parseURL(resourceQuery) {
let options = {};
Expand Down Expand Up @@ -44,4 +42,4 @@ function parseURL(resourceQuery) {
return options;
}

module.exports = parseURL;
export default parseURL;
9 changes: 3 additions & 6 deletions client-src/utils/reloadApp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

const { log } = require("./log");
import hotEmitter from "webpack/hot/emitter.js";
import { log } from "./log.js";

function reloadApp({ hot, liveReload }, { isUnloading, currentHash }) {
if (isUnloading) {
Expand All @@ -23,8 +22,6 @@ function reloadApp({ hot, liveReload }, { isUnloading, currentHash }) {
if (hot && allowToHot) {
log.info("App hot update...");

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

hotEmitter.emit("webpackHotUpdate", currentHash);

if (typeof self !== "undefined" && self.window) {
Expand Down Expand Up @@ -53,4 +50,4 @@ function reloadApp({ hot, liveReload }, { isUnloading, currentHash }) {
}
}

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

/* global __resourceQuery WorkerGlobalScope */

// Send messages to the outside, so plugins can consume it.
Expand All @@ -13,4 +11,4 @@ function sendMsg(type, data) {
}
}

module.exports = sendMsg;
export default sendMsg;
18 changes: 17 additions & 1 deletion client-src/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ const path = require("path");
const webpack = require("webpack");
const { merge } = require("webpack-merge");

const library = webpack.webpack
? {
library: {
// type: "module",
type: "commonjs",
},
}
: { libraryTarget: "umd" };

const baseForModules = {
devtool: false,
mode: "development",
// TODO enable this in future after fix bug with `eval` in webpack
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be wise to link to the bug for future reference

// experiments: {
// outputModule: true,
// },
output: {
path: path.resolve(__dirname, "../client/modules"),
libraryTarget: "commonjs2",
...library,
},
optimization: {
minimize: false,
},
target: webpack.webpack ? ["web", "es5"] : "web",
module: {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/DevServerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DevServerPlugin {
);
} else if (clientTransport === "ws") {
ClientImplementation = require.resolve(
"../../client/clients/WebsocketClient"
"../../client/clients/WebSocketClient"
);
} else {
try {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"test": "npm run test:coverage",
"pretest": "npm run lint",
"prepare": "npm run build:client && husky install",
"build:client": "rimraf ./client/* && babel client-src/ --out-dir client/ --ignore \"webpack.config.js\" && webpack --config client/webpack.config.js",
"build:client": "rimraf ./client/* && babel client-src/ --out-dir client/ --ignore \"client-src/webpack.config.js\" --ignore \"client-src/modules\" && webpack --config client-src/webpack.config.js",
"release": "standard-version"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/validate-options.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ exports[`options validate should throw an error on the "port" option with 'null'
* options.port should be \\"auto\\"."
`;

exports[`options validate should throw an error on the "proxy" option with 'false' value 1`] = `
exports[`options validate should throw an error on the "proxy" option with '() => {}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.proxy should be one of these:
object { … } | [object { … } | function, ...]
Expand All @@ -525,7 +525,7 @@ exports[`options validate should throw an error on the "proxy" option with 'fals
[object { … } | function, ...]"
`;

exports[`options validate should throw an error on the "proxy" option with 'function () {}' value 1`] = `
exports[`options validate should throw an error on the "proxy" option with 'false' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.proxy should be one of these:
object { … } | [object { … } | function, ...]
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/validate-options.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ exports[`options validate should throw an error on the "port" option with 'null'
* options.port should be \\"auto\\"."
`;

exports[`options validate should throw an error on the "proxy" option with 'false' value 1`] = `
exports[`options validate should throw an error on the "proxy" option with '() => {}' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.proxy should be one of these:
object { … } | [object { … } | function, ...]
Expand All @@ -525,7 +525,7 @@ exports[`options validate should throw an error on the "proxy" option with 'fals
[object { … } | function, ...]"
`;

exports[`options validate should throw an error on the "proxy" option with 'function () {}' value 1`] = `
exports[`options validate should throw an error on the "proxy" option with 'false' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.proxy should be one of these:
object { … } | [object { … } | function, ...]
Expand Down
Loading