-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2279 from bugsnag/PLAT-13155
plugin-react: TypesScript and rollup
- Loading branch information
Showing
13 changed files
with
320 additions
and
165 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const babelConfig = require('../../babel.config.js') | ||
|
||
module.exports = babelConfig |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import babel from '@rollup/plugin-babel' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import nodeResolve from '@rollup/plugin-node-resolve' | ||
import typescript from '@rollup/plugin-typescript' | ||
|
||
import createRollupConfig, { sharedOutput } from '../../.rollup/index.mjs' | ||
|
||
const plugins = [ | ||
nodeResolve({ | ||
browser: true | ||
}), | ||
commonjs(), | ||
typescript({ | ||
removeComments: true, | ||
// don't output anything if there's a TS error | ||
noEmitOnError: true, | ||
compilerOptions: { | ||
target: 'es2015' | ||
} | ||
}), | ||
babel({ babelHelpers: 'bundled' }) | ||
] | ||
|
||
const external = ['react'] | ||
|
||
export default [ | ||
createRollupConfig({ | ||
input: 'src/index-es.ts', | ||
output: [ | ||
{ | ||
...sharedOutput, | ||
preserveModules: false, | ||
entryFileNames: '[name].mjs', | ||
format: 'esm' | ||
} | ||
], | ||
external, | ||
plugins | ||
}), | ||
createRollupConfig({ | ||
input: 'src/index-cjs.ts', | ||
output: [ | ||
{ | ||
...sharedOutput, | ||
entryFileNames: '[name].cjs', | ||
format: 'cjs' | ||
}, | ||
], | ||
external, | ||
plugins | ||
}), | ||
createRollupConfig({ | ||
input: 'src/index-umd.ts', | ||
output: [ | ||
{ | ||
...sharedOutput, | ||
entryFileNames: 'bugsnag-react.js', | ||
format: 'umd', | ||
name: 'BugsnagReact' | ||
}, | ||
], | ||
external, | ||
plugins | ||
}) | ||
] |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import BugsnagPluginReact, { formatComponentStack } from './plugin' | ||
|
||
import assign from '@bugsnag/core/lib/es-utils/assign' | ||
|
||
export default assign(BugsnagPluginReact, { formatComponentStack }) |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, formatComponentStack } from './plugin' |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import BugsnagPluginReact, { formatComponentStack } from './plugin' | ||
|
||
import assign from '@bugsnag/core/lib/es-utils/assign' | ||
|
||
export default assign(BugsnagPluginReact, { formatComponentStack }) |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import React, { ErrorInfo } from 'react' | ||
|
||
import { Plugin, OnErrorCallback, Client } from '@bugsnag/core' | ||
|
||
interface BugsnagErrorBoundaryProps { | ||
children?: React.ReactNode | undefined | ||
onError?: OnErrorCallback | ||
FallbackComponent?: React.ComponentType<{ | ||
error: Error | ||
info: React.ErrorInfo | ||
clearError: () => void | ||
}> | ||
} | ||
|
||
export type BugsnagErrorBoundary = React.ComponentType<BugsnagErrorBoundaryProps> | ||
|
||
export interface BugsnagPluginReactResult { | ||
createErrorBoundary(react?: typeof React): BugsnagErrorBoundary | ||
} | ||
|
||
// add a new call signature for the getPlugin() method that types the react plugin result | ||
declare module '@bugsnag/core' { | ||
interface Client { | ||
getPlugin(id: 'react'): BugsnagPluginReactResult | undefined | ||
} | ||
} | ||
|
||
export default class BugsnagPluginReact implements Plugin { | ||
public readonly name: string; | ||
private readonly lazy: boolean; | ||
private readonly React?: typeof React | ||
|
||
constructor (react?: typeof React) { | ||
// Fetch React from the window object, if it exists | ||
const globalReact = typeof window !== 'undefined' && window.React | ||
|
||
this.name = 'react' | ||
this.lazy = !react && !globalReact | ||
|
||
if (!this.lazy) { | ||
this.React = react || globalReact as typeof React | ||
if (!this.React) throw new Error('@bugsnag/plugin-react reference to `React` was undefined') | ||
} | ||
} | ||
|
||
load (client: Client) { | ||
if (!this.lazy && this.React) { | ||
const ErrorBoundary = createClass(this.React, client) | ||
return { createErrorBoundary: () => ErrorBoundary } | ||
} | ||
|
||
const BugsnagPluginReactLazyInitializer = function () { | ||
throw new Error(`@bugsnag/plugin-react was used incorrectly. Valid usage is as follows: | ||
Pass React to the plugin constructor | ||
\`Bugsnag.start({ plugins: [new BugsnagPluginReact(React)] })\` | ||
and then call \`const ErrorBoundary = Bugsnag.getPlugin('react').createErrorBoundary()\` | ||
Or if React is not available until after Bugsnag has started, | ||
construct the plugin with no arguments | ||
\`Bugsnag.start({ plugins: [new BugsnagPluginReact()] })\`, | ||
then pass in React when available to construct your error boundary | ||
\`const ErrorBoundary = Bugsnag.getPlugin('react').createErrorBoundary(React)\``) | ||
} | ||
BugsnagPluginReactLazyInitializer.createErrorBoundary = (react: typeof React) => { | ||
if (!react) throw new Error('@bugsnag/plugin-react reference to `React` was undefined') | ||
return createClass(react, client) | ||
} | ||
return BugsnagPluginReactLazyInitializer | ||
} | ||
} | ||
|
||
export const formatComponentStack = (str: string) => { | ||
const lines = str.split(/\n/g) | ||
let ret = '' | ||
for (let line = 0, len = lines.length; line < len; line++) { | ||
if (lines[line].length) ret += `${ret.length ? '\n' : ''}${lines[line].trim()}` | ||
} | ||
return ret | ||
} | ||
|
||
const createClass = (react: typeof React, client: Client) => class BugsnagErrorBoundary extends React.Component<BugsnagErrorBoundaryProps, { errorState: null | { error: Error, info: ErrorInfo }}> { | ||
constructor (props: BugsnagErrorBoundaryProps) { | ||
super(props) | ||
this.state = { | ||
errorState: null | ||
} | ||
this.handleClearError = this.handleClearError.bind(this) | ||
} | ||
|
||
handleClearError () { | ||
this.setState({ errorState: null }) | ||
} | ||
|
||
componentDidCatch (error: Error, info: ErrorInfo) { | ||
const { onError } = this.props | ||
const handledState = { severity: 'error', unhandled: true, severityReason: { type: 'unhandledException' } } | ||
// @ts-ignore internal API | ||
const event = client.Event.create( | ||
error, | ||
true, | ||
handledState, | ||
1 | ||
) | ||
if (info && info.componentStack) info.componentStack = formatComponentStack(info.componentStack) | ||
event.addMetadata('react', info) | ||
client._notify(event, onError) | ||
this.setState({ errorState: { error, info } }) | ||
} | ||
|
||
render () { | ||
const { errorState } = this.state | ||
if (errorState) { | ||
const { FallbackComponent } = this.props | ||
if (FallbackComponent) return react.createElement(FallbackComponent, { ...errorState, clearError: this.handleClearError }) | ||
return null | ||
} | ||
return this.props.children | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
packages/plugin-react/src/test/__snapshots__/index.test.tsx.snap
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.