Skip to content

Commit 55afd86

Browse files
authored
Move error overlay middleware (#2216)
1 parent 5f93bc4 commit 55afd86

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

packages/react-dev-utils/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ var getProcessForPort = require('react-dev-utils/getProcessForPort');
236236
getProcessForPort(3000);
237237
```
238238

239+
#### `launchEditor(fileName: string, lineNumber: number): void`
240+
241+
On macOS, tries to find a known running editor process and opens the file in it. It can also be explicitly configured by `REACT_EDITOR`, `VISUAL`, or `EDITOR` environment variables. For example, you can put `REACT_EDITOR=atom` in your `.env.local` file, and Create React App will respect that.
242+
239243
#### `openBrowser(url: string): boolean`
240244

241245
Attempts to open the browser with a given URL.<br>
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
const launchEditor = require('react-dev-utils/launchEditor');
12+
13+
module.exports = function createLaunchEditorMiddleware() {
14+
return function launchEditorMiddleware(req, res, next) {
15+
// Keep this in sync with react-error-overlay
16+
if (req.url.startsWith('/__open-stack-frame-in-editor')) {
17+
launchEditor(req.query.fileName, req.query.lineNumber);
18+
res.end();
19+
} else {
20+
next();
21+
}
22+
};
23+
};

packages/react-error-overlay/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
],
2828
"author": "Joe Haddad <timer150@gmail.com>",
2929
"files": [
30-
"lib/"
30+
"lib/",
31+
"middleware.js"
3132
],
3233
"dependencies": {
3334
"anser": "1.2.5",

packages/react-error-overlay/src/components/frame.js

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ function createFrame(
277277
.indexOf(' ') !== -1;
278278
if (!isInternalWebpackBootstrapCode) {
279279
onSourceClick = () => {
280+
// Keep this in sync with react-error-overlay/middleware.js
280281
fetch(
281282
'/__open-stack-frame-in-editor?fileName=' +
282283
window.encodeURIComponent(sourceFileName) +

packages/react-scripts/config/webpackDevServer.config.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// @remove-on-eject-end
1111
'use strict';
1212

13-
const launchEditor = require('react-dev-utils/launchEditor');
13+
const errorOverlayMiddleware = require('react-error-overlay/middleware');
1414
const config = require('./webpack.config.dev');
1515
const paths = require('./paths');
1616

@@ -70,15 +70,8 @@ module.exports = function(proxy, allowedHost) {
7070
public: allowedHost,
7171
proxy,
7272
setup(app) {
73-
// This lets us open files from the crash overlay.
74-
app.use(function launchEditorMiddleware(req, res, next) {
75-
if (req.url.startsWith('/__open-stack-frame-in-editor')) {
76-
launchEditor(req.query.fileName, req.query.lineNumber);
77-
res.end();
78-
} else {
79-
next();
80-
}
81-
});
73+
// This lets us open files from the runtime error overlay.
74+
app.use(errorOverlayMiddleware());
8275
},
8376
};
8477
};

0 commit comments

Comments
 (0)