Skip to content

Commit 90caf91

Browse files
swernerxgregberge
authored andcommitted
feat: made socket listener optional (required when using dev-middleware only) (#34)
1 parent 5f75ff2 commit 90caf91

5 files changed

Lines changed: 299 additions & 236 deletions

File tree

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
},
2525
"devDependencies": {
2626
"@babel/cli": "^7.2.3",
27-
"@babel/core": "^7.3.4",
28-
"@babel/preset-env": "^7.3.4",
27+
"@babel/core": "^7.4.0",
28+
"@babel/preset-env": "^7.4.1",
2929
"babel-eslint": "^10.0.1",
3030
"conventional-github-releaser": "^3.1.2",
31-
"eslint": "^5.15.1",
31+
"eslint": "^5.15.3",
3232
"eslint-config-airbnb-base": "^13.1.0",
3333
"eslint-config-prettier": "^4.1.0",
3434
"eslint-plugin-import": "^2.16.0",
3535
"prettier": "^1.16.4",
36-
"standard-version": "^5.0.1"
36+
"standard-version": "^5.0.2"
3737
},
3838
"license": "MIT",
3939
"dependencies": {
Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
11
/* eslint-disable */
2-
import url from 'url';
3-
import SockJS from 'sockjs-client';
42
import {
53
setEditorHandler,
6-
reportBuildError,
74
startReportingRuntimeErrors,
85
} from 'react-error-overlay'
96
import launchEditorEndpoint from 'react-dev-utils/launchEditorEndpoint'
10-
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages';
11-
12-
13-
14-
const connection = new SockJS(
15-
url.format({
16-
protocol: window.location.protocol,
17-
hostname: window.location.hostname,
18-
port: window.location.port,
19-
// Hardcoded in WebpackDevServer
20-
pathname: '/sockjs-node',
21-
})
22-
);
23-
247

258
setEditorHandler(errorLocation => {
269
// Keep this sync with errorOverlayMiddleware.js
@@ -46,18 +29,3 @@ startReportingRuntimeErrors({
4629
}
4730
},
4831
})
49-
50-
connection.onmessage = function(e) {
51-
const {type, data} = JSON.parse(e.data);
52-
switch (type) {
53-
case 'errors':
54-
const {errors} = formatWebpackMessages({
55-
errors: data,
56-
warnings: [],
57-
});
58-
reportBuildError(errors[0]);
59-
break;
60-
default:
61-
// Do nothing.
62-
}
63-
};

src/entry-devserver.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import url from 'url';
2+
import SockJS from 'sockjs-client';
3+
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages';
4+
import {
5+
reportBuildError,
6+
} from 'react-error-overlay'
7+
8+
const connection = new SockJS(
9+
url.format({
10+
protocol: window.location.protocol,
11+
hostname: window.location.hostname,
12+
port: window.location.port,
13+
// Hardcoded in WebpackDevServer
14+
pathname: '/sockjs-node',
15+
})
16+
);
17+
18+
connection.onmessage = function onmessage(e) {
19+
const {type, data} = JSON.parse(e.data);
20+
let formatted
21+
switch (type) {
22+
case 'errors':
23+
formatted = formatWebpackMessages({
24+
errors: data,
25+
warnings: [],
26+
});
27+
reportBuildError(formatted.errors[0]);
28+
break;
29+
default:
30+
// Do nothing.
31+
}
32+
};

src/index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import errorOverlayMiddleware from 'react-dev-utils/errorOverlayMiddleware'
22

3+
const chunkPathBasic = require.resolve('./entry-basic')
4+
const chunkPathDevServer = require.resolve('./entry-devserver')
5+
36
class ErrorOverlayPlugin {
47
apply(compiler) {
58
const className = this.constructor.name
69

710
if (compiler.options.mode !== 'development') return
811

12+
const devServerEnabled = !!compiler.options.devServer
13+
914
compiler.hooks.entryOption.tap(className, (context, entry) => {
10-
const chunkPath = require.resolve('./entry')
11-
adjustEntry(entry, chunkPath)
15+
adjustEntry(entry, devServerEnabled)
1216
})
1317

1418
compiler.hooks.afterResolvers.tap(className, ({ options }) => {
15-
if (options.devServer) {
19+
if (devServerEnabled) {
1620
const originalBefore = options.devServer.before
1721
options.devServer.before = (app, server) => {
1822
if (originalBefore) {
@@ -25,7 +29,7 @@ class ErrorOverlayPlugin {
2529
}
2630
}
2731

28-
function adjustEntry(entry, chunkPath) {
32+
function adjustEntry(entry, enableDevServer) {
2933
if (typeof entry === 'string') {
3034
throw new Error(
3135
`We currently do not inject our entry code into single-file anonymous entries.
@@ -34,12 +38,18 @@ Please use a multi-main (array) or object-form \`entry\` setting for now.`,
3438
}
3539

3640
if (Array.isArray(entry)) {
37-
if (!entry.includes(chunkPath)) {
38-
entry.unshift(chunkPath)
41+
if (enableDevServer) {
42+
if (!entry.includes(chunkPathDevServer)) {
43+
entry.unshift(chunkPathDevServer)
44+
}
45+
}
46+
47+
if (!entry.includes(chunkPathBasic)) {
48+
entry.unshift(chunkPathBasic)
3949
}
4050
} else {
4151
Object.keys(entry).forEach(entryName => {
42-
entry[entryName] = adjustEntry(entry[entryName], chunkPath)
52+
entry[entryName] = adjustEntry(entry[entryName], enableDevServer)
4353
})
4454
}
4555

0 commit comments

Comments
 (0)