Skip to content

Commit

Permalink
refactor(client): make use of ESM instead of CJS (#1967)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy committed Jun 24, 2019
1 parent 182fe97 commit 382c882
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 88 deletions.
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
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"
}
}
30 changes: 15 additions & 15 deletions client-src/default/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'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';

const status = {
isUnloading: false,
Expand Down Expand Up @@ -47,7 +47,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 +57,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 +93,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 +112,7 @@ const onSocketMessage = {
log.warn(strippedWarnings[i]);
}
if (options.useWarningOverlay) {
overlay.showMessage(warnings);
showMessageOverlay(warnings);
}

if (options.initial) {
Expand All @@ -128,7 +128,7 @@ const onSocketMessage = {
log.error(strippedErrors[i]);
}
if (options.useErrorOverlay) {
overlay.showMessage(errors);
showMessageOverlay(errors);
}
options.initial = false;
},
Expand Down
29 changes: 11 additions & 18 deletions client-src/default/overlay.js
Original file line number Diff line number Diff line change
@@ -1,10 +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 Entities();

const entities = new AllHtmlEntities();
const colors = {
Expand Down Expand Up @@ -109,19 +109,12 @@ function clear() {
lastOnOverlayDivReady = null;
}

// Compilation with errors (e.g. syntax error or missing modules).
function showMessage(messages) {
ensureOverlayDivExists((div) => {
// Make it look similar to our terminal.
div.innerHTML = `<span style="color: #${
colors.red
}">Failed to compile.</span><br><br>${ansiHTML(
entities.encode(messages[0])
)}`;
});
// Successful compilation.
export function clear() {
destroyErrorOverlay();
}

module.exports = {
clear,
showMessage,
};
// Compilation with errors (e.g. syntax error or missing modules).
export function showMessage(messages) {
showMessageOverlay(messages[0]);
}
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;
12 changes: 4 additions & 8 deletions client-src/default/utils/createSocketUrl.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
'use strict';

/* global self */

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

function createSocketUrl(resourceQuery) {
let urlParts;
Expand Down Expand Up @@ -80,4 +76,4 @@ function createSocketUrl(resourceQuery) {
});
}

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;
11 changes: 3 additions & 8 deletions client-src/default/utils/log.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
import { getLogger } from 'loglevel';

const log = require('loglevel').getLogger('webpack-dev-server');
export const log = getLogger('webpack-dev-server');

const INFO = 'info';
const WARN = 'warn';
Expand All @@ -17,7 +17,7 @@ const NONE = 'none';
// Set the default log level
log.setDefaultLevel(INFO);

function setLogLevel(level) {
export function setLogLevel(level) {
switch (level) {
case INFO:
case WARN:
Expand All @@ -40,8 +40,3 @@ function setLogLevel(level) {
log.error(`[WDS] Unknown clientLogLevel '${level}'`);
}
}

module.exports = {
log,
setLogLevel,
};
8 changes: 2 additions & 6 deletions client-src/default/utils/reloadApp.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
'use strict';

/* global WorkerGlobalScope self */

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

function reloadApp(
{ hotReload, hot, liveReload },
Expand Down Expand Up @@ -46,4 +42,4 @@ function reloadApp(
}
}

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

/* global __resourceQuery WorkerGlobalScope self */

// Send messages to the outside, so plugins can consume it.
function sendMsg(type, data) {
function sendMessage(type, data) {
if (
typeof self !== 'undefined' &&
(typeof WorkerGlobalScope === 'undefined' ||
Expand All @@ -19,4 +15,4 @@ function sendMsg(type, data) {
}
}

module.exports = sendMsg;
export default sendMessage;
2 changes: 0 additions & 2 deletions client-src/default/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const webpack = require('webpack');

module.exports = {
Expand Down
10 changes: 4 additions & 6 deletions client-src/live/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

/* eslint import/no-extraneous-dependencies: off, global-require: off */

const $ = require('jquery');
const stripAnsi = require('strip-ansi');
const socket = require('../default/socket');
require('./style.css');
import $ from 'jquery';
import stripAnsi from 'strip-ansi';
import socket from '../default/socket';
import './style.css';

let hot = false;
let currentHash = '';
Expand Down
2 changes: 0 additions & 2 deletions client-src/live/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const path = require('path');
const webpack = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');
Expand Down
4 changes: 2 additions & 2 deletions client-src/sockjs/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict';
import client from 'sockjs-client';

module.exports = require('sockjs-client');
export default client;
2 changes: 0 additions & 2 deletions client-src/sockjs/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

module.exports = {
mode: 'production',
output: {
Expand Down
6 changes: 4 additions & 2 deletions test/client/utils/createSocketUrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ describe('createSocketUrl', () => {
() => () => url
);

// eslint-disable-next-line global-require
const createSocketUrl = require('../../../client-src/default/utils/createSocketUrl');
const {
default: createSocketUrl,
// eslint-disable-next-line global-require
} = require('../../../../client-src/default/utils/createSocketUrl');

test(`should return the url when __resourceQuery is ${url}`, () => {
expect(createSocketUrl(url)).toMatchSnapshot();
Expand Down
4 changes: 3 additions & 1 deletion test/client/utils/getCurrentScriptSource.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const getCurrentScriptSource = require('../../../client-src/default/utils/getCurrentScriptSource');
const {
default: getCurrentScriptSource,
} = require('../../../../client-src/default/utils/getCurrentScriptSource');

describe('getCurrentScriptSource', () => {
afterEach(() => {
Expand Down
3 changes: 2 additions & 1 deletion test/client/utils/reloadApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ describe('reloadApp', () => {
});

// eslint-disable-next-line global-require
reloadApp = require('../../../client-src/default/utils/reloadApp');
reloadApp = require('../../../../client-src/default/utils/reloadApp')
.default;
});

afterEach(() => {
Expand Down
4 changes: 3 additions & 1 deletion test/client/utils/sendMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

/* global self */

const sendMessage = require('../../../client-src/default/utils/sendMessage');
const {
default: sendMessage,
} = require('../../../../client-src/default/utils/sendMessage');

describe('sendMessage', () => {
afterEach(() => {
Expand Down

0 comments on commit 382c882

Please sign in to comment.