|
1 |
| -import fs from 'fs' |
2 |
| -import { codeFrameColumns } from '@babel/code-frame'; |
| 1 | +// We try to load node dependencies |
| 2 | +let readFileSync = null |
| 3 | +let codeFrameColumns = null |
3 | 4 |
|
4 |
| -// Frame has the form "at myMethod (location/to/my/file.js:10:2)" |
| 5 | +try { |
| 6 | + const nodeRequire = module && module.require |
| 7 | + readFileSync = nodeRequire.call(module, 'fs').readFileSync |
| 8 | + codeFrameColumns = nodeRequire.call(module, '@babel/code-frame') |
| 9 | + .codeFrameColumns |
| 10 | +} catch { |
| 11 | + // We're in a browser environment |
| 12 | + console.warn( |
| 13 | + 'Printing the user trace is not supported in a browser environment', |
| 14 | + ) |
| 15 | +} |
| 16 | + |
| 17 | +// frame has the form "at myMethod (location/to/my/file.js:10:2)" |
5 | 18 | function getCodeFrame(frame) {
|
6 | 19 | const locationStart = frame.indexOf('(') + 1
|
7 | 20 | const locationEnd = frame.indexOf(')')
|
8 | 21 | const frameLocation = frame.slice(locationStart, locationEnd)
|
9 | 22 |
|
10 |
| - const frameLocationElements = frameLocation.split(":") |
11 |
| - const [filename, line, column] = [frameLocationElements[0], parseInt(frameLocationElements[1], 10), parseInt(frameLocationElements[2], 10)] |
12 |
| - let rawFileContents = "" |
| 23 | + const frameLocationElements = frameLocation.split(':') |
| 24 | + const [filename, line, column] = [ |
| 25 | + frameLocationElements[0], |
| 26 | + parseInt(frameLocationElements[1], 10), |
| 27 | + parseInt(frameLocationElements[2], 10), |
| 28 | + ] |
| 29 | + |
| 30 | + let rawFileContents = '' |
13 | 31 | try {
|
14 |
| - rawFileContents = fs.readFileSync(filename, 'utf-8') |
| 32 | + rawFileContents = readFileSync(filename, 'utf-8') |
15 | 33 | } catch {
|
16 | 34 | console.warn(`Couldn't read file ${filename} for displaying the code frame`)
|
| 35 | + return '' |
17 | 36 | }
|
18 |
| - return codeFrameColumns(rawFileContents, { |
19 |
| - start: { line, column }, |
20 |
| - }, { |
21 |
| - highlightCode: true, |
22 |
| - linesBelow: 0, |
23 |
| - }) |
| 37 | + |
| 38 | + const codeFrame = codeFrameColumns( |
| 39 | + rawFileContents, |
| 40 | + { |
| 41 | + start: {line, column}, |
| 42 | + }, |
| 43 | + { |
| 44 | + highlightCode: true, |
| 45 | + linesBelow: 0, |
| 46 | + }, |
| 47 | + ) |
| 48 | + return `${codeFrame}\n` |
24 | 49 | }
|
25 | 50 |
|
26 | 51 | function getUserTrace() {
|
| 52 | + // If we couldn't load dependencies, we can't generate a user trace |
| 53 | + if (!readFileSync || !codeFrameColumns) { |
| 54 | + return '' |
| 55 | + } |
27 | 56 | const err = new Error()
|
28 | 57 | const firstClientCodeFrame = err.stack
|
29 | 58 | .split('\n')
|
30 | 59 | .slice(1) // Remove first line which has the form "Error: TypeError"
|
31 | 60 | .find(frame => !frame.includes('node_modules/')) // Ignore frames from 3rd party libraries
|
32 | 61 |
|
33 |
| - return `${getCodeFrame(firstClientCodeFrame)}\n` |
| 62 | + return getCodeFrame(firstClientCodeFrame) |
34 | 63 | }
|
35 | 64 |
|
36 | 65 | export {getUserTrace}
|
0 commit comments