Skip to content
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

Refactor @bugsnag/browser #2252

Draft
wants to merge 12 commits into
base: integration/typescript
Choose a base branch
from
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ module.exports = {
'plugin-simple-throttle',
'plugin-console-breadcrumbs',
'plugin-browser-session'
]),
], {
setupFiles: ['<rootDir>/jest/setup/mockEventTarget.js']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See jsdom/jsdom#2156

This is needed to fix the TypeError: 'addEventListener' called on an object that is not a valid instance of EventTarget. seen in the integration tests following the rollup changes

}),
project('react native', [
'react-native',
'delivery-react-native',
Expand Down
40 changes: 40 additions & 0 deletions jest/setup/mockEventTarget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// copy the code from https://developer.mozilla.org/en-US/docs/Web/API/EventTarget#Simple_implementation_of_EventTarget
var EventTarget = function () {
this.listeners = {}
}

EventTarget.prototype.listeners = null
EventTarget.prototype.addEventListener = function (type, callback) {
if (!(type in this.listeners)) {
this.listeners[type] = []
}
this.listeners[type].push(callback)
}

EventTarget.prototype.removeEventListener = function (type, callback) {
if (!(type in this.listeners)) {
return
}
var stack = this.listeners[type]
for (var i = 0, l = stack.length; i < l; i++) {
if (stack[i] === callback) {
stack.splice(i, 1)
return
}
}
}

EventTarget.prototype.dispatchEvent = function (event) {
if (!(event.type in this.listeners)) {
return true
}
var stack = this.listeners[event.type].slice()

for (var i = 0, l = stack.length; i < l; i++) {
stack[i].call(this, event)
}
return !event.defaultPrevented
}

// make the EventTarget global
global.EventTarget = EventTarget
12 changes: 10 additions & 2 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"name": "@bugsnag/browser",
"version": "8.1.2",
"main": "dist/bugsnag.js",
"main": "dist/notifier.js",
"types": "types/bugsnag.d.ts",
"exports": {
".": {
"types": "./types/bugsnag.d.ts",
"default": "./dist/notifier.js",
"import": "./dist/notifier.mjs"
}
},
"description": "Bugsnag error reporter for browser JavaScript",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -22,7 +29,8 @@
"scripts": {
"size": "../../bin/size dist/bugsnag.min.js",
"clean": "rm -fr dist && mkdir dist",
"build": "npm run clean && npm run build:dist && npm run build:dist:min",
"build": "npm run clean && npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"build:dist": "cross-env NODE_ENV=production bash -c '../../bin/bundle src/notifier.js --standalone=Bugsnag | ../../bin/extract-source-map dist/bugsnag.js'",
"build:dist:min": "cross-env NODE_ENV=production bash -c '../../bin/bundle src/notifier.js --standalone=Bugsnag | ../../bin/minify dist/bugsnag.min.js'",
"cdn-upload": "../../bin/cdn-upload dist/*"
Expand Down
33 changes: 33 additions & 0 deletions packages/browser/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import createRollupConfig from "../../.rollup/index.mjs";

export default createRollupConfig({
input: "src/notifier.ts",
external: [
"@bugsnag/core/client",
"@bugsnag/core/event",
"@bugsnag/core/session",
"@bugsnag/core/breadcrumb",
"@bugsnag/core/config",
"@bugsnag/core/types",
"@bugsnag/core/lib/es-utils/map",
"@bugsnag/core/lib/es-utils/keys",
"@bugsnag/core/lib/es-utils/assign",
"@bugsnag/plugin-window-onerror",
"@bugsnag/plugin-window-unhandled-rejection",
"@bugsnag/plugin-app-duration",
"@bugsnag/plugin-browser-device",
"@bugsnag/plugin-browser-context",
"@bugsnag/plugin-browser-request",
"@bugsnag/plugin-simple-throttle",
"@bugsnag/plugin-console-breadcrumbs",
"@bugsnag/plugin-network-breadcrumbs",
"@bugsnag/plugin-navigation-breadcrumbs",
"@bugsnag/plugin-interaction-breadcrumbs",
"@bugsnag/plugin-inline-script-content",
"@bugsnag/plugin-browser-session",
"@bugsnag/plugin-client-ip",
"@bugsnag/plugin-strip-query-string",
"@bugsnag/delivery-x-domain-request",
"@bugsnag/delivery-xml-http-request"
],
});
35 changes: 0 additions & 35 deletions packages/browser/src/config.js

This file was deleted.

24 changes: 24 additions & 0 deletions packages/browser/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { schema } from '@bugsnag/core/config'
import assign from '@bugsnag/core/lib/es-utils/assign'
import getPrefixedConsole from './get-prefixed-console'

const config = {
releaseStage: assign({}, schema.releaseStage, {
defaultValue: () => {
if (/^localhost(:\d+)?$/.test(window.location.host)) return 'development'
return 'production'
}
}),
appType: assign({}, schema.appType, {
defaultValue: () => 'browser'
}),
logger: assign({}, schema.logger, {
defaultValue: () =>
// set logger based on browser capability
(typeof console !== 'undefined' && typeof console.debug === 'function')
? getPrefixedConsole()
: undefined
})
}

export default config
17 changes: 17 additions & 0 deletions packages/browser/src/get-prefixed-console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import map from '@bugsnag/core/lib/es-utils/map'

type LoggerMethod = 'debug' | 'info' | 'warn' | 'error'

const getPrefixedConsole = () => {
const logger: Record<string, unknown> = {}
const consoleLog = console.log
map(['debug', 'info', 'warn', 'error'], (method: LoggerMethod) => {
const consoleMethod = console[method]
logger[method] = typeof consoleMethod === 'function'
? consoleMethod.bind(console, '[bugsnag]')
: consoleLog.bind(console, '[bugsnag]')
})
return logger
}

export default getPrefixedConsole
2 changes: 0 additions & 2 deletions packages/browser/src/notifier.d.ts

This file was deleted.

110 changes: 0 additions & 110 deletions packages/browser/src/notifier.js

This file was deleted.

Loading
Loading