-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add React error overlay and HMR source maps #8034
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc9c4d9
Fix source maps in HMR
devongovett e3756c5
Add react-error-overlay
devongovett b396f1a
Add ability to launch editor from browser
devongovett 1d34c7a
Automatically reload error page via HMR
devongovett 39b143d
Update yarn.lock
devongovett c75986f
lint
devongovett 47c0b5d
Remove .babelrc from hmr-runtime
devongovett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
// @flow | ||
|
||
import type {BuildSuccessEvent, Dependency, PluginOptions} from '@parcel/types'; | ||
import type { | ||
BuildSuccessEvent, | ||
Dependency, | ||
PluginOptions, | ||
BundleGraph, | ||
PackagedBundle, | ||
Asset, | ||
} from '@parcel/types'; | ||
import type {Diagnostic} from '@parcel/diagnostic'; | ||
import type {AnsiDiagnosticResult} from '@parcel/utils'; | ||
import type {ServerError, HMRServerOptions} from './types.js.flow'; | ||
|
||
import WebSocket from 'ws'; | ||
import invariant from 'assert'; | ||
import {ansiHtml, prettyDiagnostic, PromiseQueue} from '@parcel/utils'; | ||
import {HMR_ENDPOINT} from './Server'; | ||
|
||
export type HMRAsset = {| | ||
id: string, | ||
url: string, | ||
type: string, | ||
output: string, | ||
envHash: string, | ||
|
@@ -26,7 +35,7 @@ export type HMRMessage = | |
type: 'error', | ||
diagnostics: {| | ||
ansi: Array<AnsiDiagnosticResult>, | ||
html: Array<AnsiDiagnosticResult>, | ||
html: Array<$Rest<AnsiDiagnosticResult, {|codeframe: string|}>>, | ||
|}, | ||
|}; | ||
|
||
|
@@ -81,7 +90,10 @@ export default class HMRServer { | |
return { | ||
message: ansiHtml(d.message), | ||
stack: ansiHtml(d.stack), | ||
codeframe: ansiHtml(d.codeframe), | ||
frames: d.frames.map(f => ({ | ||
location: f.location, | ||
code: ansiHtml(f.code), | ||
})), | ||
hints: d.hints.map(hint => ansiHtml(hint)), | ||
documentation: diagnostics[i].documentationURL ?? '', | ||
}; | ||
|
@@ -141,9 +153,13 @@ export default class HMRServer { | |
|
||
return { | ||
id: event.bundleGraph.getAssetPublicId(asset), | ||
url: getSourceURL(event.bundleGraph, asset), | ||
type: asset.type, | ||
// No need to send the contents of non-JS assets to the client. | ||
output: asset.type === 'js' ? await asset.getCode() : '', | ||
output: | ||
asset.type === 'js' | ||
? await getHotAssetContents(event.bundleGraph, asset) | ||
: '', | ||
envHash: asset.env.id, | ||
depsByBundle, | ||
}; | ||
|
@@ -185,3 +201,34 @@ function getSpecifier(dep: Dependency): string { | |
|
||
return dep.specifier; | ||
} | ||
|
||
export async function getHotAssetContents( | ||
bundleGraph: BundleGraph<PackagedBundle>, | ||
asset: Asset, | ||
): Promise<string> { | ||
let output = await asset.getCode(); | ||
if (asset.type === 'js') { | ||
let publicId = bundleGraph.getAssetPublicId(asset); | ||
output = `parcelHotUpdate['${publicId}'] = function (require, module, exports) {${output}}`; | ||
} | ||
|
||
let sourcemap = await asset.getMap(); | ||
if (sourcemap) { | ||
let sourcemapStringified = await sourcemap.stringify({ | ||
format: 'inline', | ||
sourceRoot: '/__parcel_source_root/', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somewhat related, but this source root seems to be displayed by react-error-overlay rather than either the original file path, or nothing. Not sure if there is a way to improve that? |
||
// $FlowFixMe | ||
fs: asset.fs, | ||
}); | ||
|
||
invariant(typeof sourcemapStringified === 'string'); | ||
output += `\n//# sourceMappingURL=${sourcemapStringified}`; | ||
output += `\n//# sourceURL=${getSourceURL(bundleGraph, asset)}\n`; | ||
} | ||
|
||
return output; | ||
} | ||
|
||
function getSourceURL(bundleGraph, asset) { | ||
return HMR_ENDPOINT + asset.id; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit unfortunate, but couldn't think of a better way...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could make
codeframe
a getter that processesframes
in the default manner so there's less duplication. Beyond disgusting regex I don't think there's any other way to support rewriting paths than saving theframes
array.