Skip to content

Commit c490929

Browse files
jackjocrossjaredpalmer
authored andcommitted
Ignore column from source map and only use pretty errors if file is read (#696)
1 parent b4f9898 commit c490929

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

packages/razzle-dev-utils/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
"prettyNodeErrors.js"
1717
],
1818
"dependencies": {
19+
"@babel/code-frame": "^7.0.0-beta.52",
1920
"chalk": "1.1.3",
20-
"jest-message-util": "^23.2.0",
21+
"jest-message-util": "^23.4.0",
2122
"react-dev-utils": "4.1.0",
2223
"sockjs-client": "1.1.4",
2324
"strip-ansi": "3.0.1"

packages/razzle-dev-utils/prettyNodeErrors.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1+
const fs = require('fs');
12
const {
2-
formatExecError,
3+
getTopFrame,
4+
getStackTraceLines,
35
separateMessageFromStack,
46
} = require('jest-message-util');
7+
const { codeFrameColumns } = require('@babel/code-frame');
58

69
function pretty(error) {
7-
return `\n${formatExecError(
8-
error,
9-
{ rootDir: process.cwd() },
10-
{},
11-
undefined,
12-
true
13-
)}`;
10+
const { message, stack } = error;
11+
const lines = getStackTraceLines(stack);
12+
const topFrame = getTopFrame(lines);
13+
const fallback = `${message}${stack}`;
14+
15+
if (!topFrame) {
16+
return fallback;
17+
}
18+
19+
const { file, line } = topFrame;
20+
try {
21+
const result = codeFrameColumns(
22+
fs.readFileSync(file, 'utf8'),
23+
{ start: { line } },
24+
{ highlightCode: true }
25+
);
26+
return `\n${message}\n\n${result}\n${stack}\n`;
27+
} catch (error) {
28+
return fallback;
29+
}
1430
}
1531

1632
function usePrettyErrors(transform) {
@@ -21,12 +37,12 @@ function usePrettyErrors(transform) {
2137
? separateMessageFromStack(prepareStackTrace(error, trace))
2238
: error;
2339
const transformed = transform ? transform(prepared) : prepared;
24-
2540
return pretty(transformed);
2641
};
2742
}
2843

29-
// Currently needed to fix sourcemap path
44+
// Clean up Webpack's sourcemap namespacing in error stacks
45+
// @see https://github.com/facebook/create-react-app/blob/next/packages/react-dev-utils/formatWebpackMessages.js#L112
3046
const stackTransform = ({ stack = '', ...rest }) => ({
3147
stack: stack.replace('/build/webpack:', ''),
3248
...rest,

yarn.lock

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
dependencies:
1919
"@babel/highlight" "7.0.0-beta.42"
2020

21+
"@babel/code-frame@^7.0.0-beta.52":
22+
version "7.0.0-beta.52"
23+
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.52.tgz#192483bfa0d1e467c101571c21029ccb74af2801"
24+
dependencies:
25+
"@babel/highlight" "7.0.0-beta.52"
26+
2127
2228
version "7.0.0-beta.44"
2329
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0-beta.44.tgz#c7e67b9b5284afcf69b309b50d7d37f3e5033d42"
@@ -64,6 +70,14 @@
6470
esutils "^2.0.2"
6571
js-tokens "^3.0.0"
6672

73+
74+
version "7.0.0-beta.52"
75+
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0-beta.52.tgz#ef24931432f06155e7bc39cdb8a6b37b4a28b3d0"
76+
dependencies:
77+
chalk "^2.0.0"
78+
esutils "^2.0.2"
79+
js-tokens "^3.0.0"
80+
6781
6882
version "7.0.0-beta.44"
6983
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.44.tgz#f8832f4fdcee5d59bf515e595fc5106c529b394f"
@@ -4492,13 +4506,7 @@ file-entry-cache@^2.0.0:
44924506
flat-cache "^1.2.1"
44934507
object-assign "^4.0.1"
44944508

4495-
4496-
version "0.11.2"
4497-
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-0.11.2.tgz#4ff1df28af38719a6098093b88c82c71d1794a34"
4498-
dependencies:
4499-
loader-utils "^1.0.2"
4500-
4501-
file-loader@^1.1.11:
4509+
[email protected], [email protected], file-loader@^1.1.11:
45024510
version "1.1.11"
45034511
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-1.1.11.tgz#6fe886449b0f2a936e43cabaac0cdbfb369506f8"
45044512
dependencies:
@@ -6619,13 +6627,13 @@ jest-message-util@^23.1.0:
66196627
slash "^1.0.0"
66206628
stack-utils "^1.0.1"
66216629

6622-
jest-message-util@^23.2.0:
6623-
version "23.2.0"
6624-
resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-23.2.0.tgz#591e8148fff69cf89b0414809c721756ebefe744"
6630+
jest-message-util@^23.4.0:
6631+
version "23.4.0"
6632+
resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-23.4.0.tgz#17610c50942349508d01a3d1e0bda2c079086a9f"
66256633
dependencies:
66266634
"@babel/code-frame" "^7.0.0-beta.35"
66276635
chalk "^2.0.1"
6628-
micromatch "^3.1.10"
6636+
micromatch "^2.3.11"
66296637
slash "^1.0.0"
66306638
stack-utils "^1.0.1"
66316639

@@ -8088,24 +8096,6 @@ micromatch@^2.1.5, micromatch@^2.3.11:
80888096
parse-glob "^3.0.4"
80898097
regex-cache "^0.4.2"
80908098

8091-
micromatch@^3.1.10:
8092-
version "3.1.10"
8093-
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-3.1.10.tgz#70859bc95c9840952f359a068a3fc49f9ecfac23"
8094-
dependencies:
8095-
arr-diff "^4.0.0"
8096-
array-unique "^0.3.2"
8097-
braces "^2.3.1"
8098-
define-property "^2.0.2"
8099-
extend-shallow "^3.0.2"
8100-
extglob "^2.0.4"
8101-
fragment-cache "^0.2.1"
8102-
kind-of "^6.0.2"
8103-
nanomatch "^1.2.9"
8104-
object.pick "^1.3.0"
8105-
regex-not "^1.0.0"
8106-
snapdragon "^0.8.1"
8107-
to-regex "^3.0.2"
8108-
81098099
micromatch@^3.1.4, micromatch@^3.1.8:
81108100
version "3.1.9"
81118101
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-3.1.9.tgz#15dc93175ae39e52e93087847096effc73efcf89"
@@ -11738,7 +11728,7 @@ to-regex-range@^2.1.0:
1173811728
is-number "^3.0.0"
1173911729
repeat-string "^1.6.1"
1174011730

11741-
to-regex@^3.0.1, to-regex@^3.0.2:
11731+
to-regex@^3.0.1:
1174211732
version "3.0.2"
1174311733
resolved "https://registry.yarnpkg.com/to-regex/-/to-regex-3.0.2.tgz#13cfdd9b336552f30b51f33a8ae1b42a7a7599ce"
1174411734
dependencies:

0 commit comments

Comments
 (0)