Skip to content

Commit

Permalink
refactor: ♻️ refactor browser package to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbenw committed Oct 3, 2024
1 parent c4e1afa commit 5893f77
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 114 deletions.
63 changes: 63 additions & 0 deletions packages/browser/local-packages.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
declare module '@bugsnag/core/config' {
export const schema: any
}

declare module '@bugsnag/plugin-window-onerror' {
export default function pluginWindowOnerror(): any
}

declare module '@bugsnag/plugin-window-unhandled-rejection' {
export default function pluginUnhandledRejection(): any
}

declare module '@bugsnag/plugin-app-duration' {
export default function pluginApp(): any
}

declare module '@bugsnag/plugin-browser-device' {
export default function pluginDevice(): any
}

declare module '@bugsnag/plugin-browser-context' {
export default function pluginContext(): any
}

declare module '@bugsnag/plugin-browser-request' {
export default function pluginRequest(): any
}

declare module '@bugsnag/plugin-simple-throttle' {
export default function pluginThrottle(): any
}

declare module '@bugsnag/plugin-console-breadcrumbs' {
export default function pluginConsoleBreadcrumbs(): any
}

declare module '@bugsnag/plugin-network-breadcrumbs' {
export default function pluginNetworkBreadcrumbs(): any
}

declare module '@bugsnag/plugin-navigation-breadcrumbs' {
export default function pluginNavigationBreadcrumbs(): any
}

declare module '@bugsnag/plugin-interaction-breadcrumbs' {
export default function pluginInteractionBreadcrumbs(): any
}

declare module '@bugsnag/plugin-inline-script-content' {
export default function pluginInlineScriptContent(): any
}

declare module '@bugsnag/plugin-browser-session' {
export default function pluginSession(): any
}

declare module '@bugsnag/plugin-client-ip' {
export default function pluginIp(): any
}

declare module '@bugsnag/plugin-strip-query-string' {
export default function pluginStripQueryString(): any
}
9 changes: 5 additions & 4 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@bugsnag/browser",
"version": "8.0.0",
"type": "module",
"main": "dist/bugsnag.js",
"types": "types/bugsnag.d.ts",
"description": "Bugsnag error reporter for browser JavaScript",
Expand All @@ -21,10 +22,10 @@
],
"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: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'",
"clean": "rm -rf dist/*",
"build": "npm run clean && npm run build:npm && npm run build:cdn",
"build:cdn": "rollup --config rollup.config.cdn.mjs",
"build:npm": "rollup --config rollup.config.npm.mjs",
"cdn-upload": "../../bin/cdn-upload dist/*"
},
"author": "Bugsnag",
Expand Down
53 changes: 53 additions & 0 deletions packages/browser/rollup.config.cdn.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import fs from 'fs'
import terser from '@rollup/plugin-terser'
import replace from '@rollup/plugin-replace'
import commonjs from '@rollup/plugin-commonjs'
import typescript from '@rollup/plugin-typescript'
import nodeResolve from '@rollup/plugin-node-resolve'

// the built files for the CDN go in the top-level 'build' directory so they
// can't accidentally be uploaded to NPM somehow
const buildDirectory = './dist'

if (!fs.existsSync(buildDirectory)) {
fs.mkdirSync(buildDirectory)
}

const packageJson = JSON.parse(fs.readFileSync('./package.json'))

const sharedOutputOptions = {
format: 'es',
name: 'Bugsnag',
generatedCode: {
preset: 'es2015',
},
sourcemap: true,
}

export default {
input: 'src/notifier.ts',
output: [
{
...sharedOutputOptions,
file: `${buildDirectory}/bugsnag.js`,
},
{
...sharedOutputOptions,
file: `${buildDirectory}/bugsnag.min.js`,
compact: true,
plugins: [terser({ ecma: 2015 })],
},
],
plugins: [
replace({
preventAssignment: true,
values: { __VERSION__: packageJson.version },
}),
typescript({
// don't output anything if there's a TS error
noEmitOnError: true,
}),
commonjs(),
nodeResolve({ browser: true }),
],
}
32 changes: 32 additions & 0 deletions packages/browser/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import createRollupConfig from '../../.rollup/index.mjs'

export default createRollupConfig({
internal: 'src/notifier.ts',
external: [
"@bugsnag/core/breadcrumb",
"@bugsnag/core/client",
"@bugsnag/core/config",
"@bugsnag/core/event",
"@bugsnag/core/lib/es-utils/map",
"@bugsnag/core/lib/es-utils/keys",
"@bugsnag/core/lib/es-utils/assign",
"@bugsnag/core/session",
"@bugsnag/delivery-x-domain-request",
"@bugsnag/delivery-xml-http-request",
"@bugsnag/plugin-app-duration",
"@bugsnag/plugin-browser-context",
"@bugsnag/plugin-browser-device",
"@bugsnag/plugin-browser-request",
"@bugsnag/plugin-browser-session",
"@bugsnag/plugin-client-ip",
"@bugsnag/plugin-console-breadcrumbs",
"@bugsnag/plugin-inline-script-content",
"@bugsnag/plugin-interaction-breadcrumbs",
"@bugsnag/plugin-navigation-breadcrumbs",
"@bugsnag/plugin-network-breadcrumbs",
"@bugsnag/plugin-simple-throttle",
"@bugsnag/plugin-strip-query-string",
"@bugsnag/plugin-window-onerror",
"@bugsnag/plugin-window-unhandled-rejection"
],
})
45 changes: 45 additions & 0 deletions packages/browser/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Config } from '@bugsnag/core'
import { schema } from '@bugsnag/core/config'
import assign from '@bugsnag/core/lib/es-utils/assign'
import map from '@bugsnag/core/lib/es-utils/map'

export interface BrowserConfig extends Config {
maxEvents?: number
collectUserIp?: boolean
generateAnonymousId?: boolean
trackInlineScripts?: boolean
}

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

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

const getPrefixedConsole = () => {
const logger: Record<string, () => void> = {}
const consoleLog = console.log
map(['debug', 'info', 'warn', 'error'], (method: ConsoleMethods) => {
const consoleMethod = console[method]
logger[method] = typeof consoleMethod === 'function'
? consoleMethod.bind(console, '[bugsnag]')
: consoleLog.bind(console, '[bugsnag]')
})
return logger
}
110 changes: 0 additions & 110 deletions packages/browser/src/notifier.js

This file was deleted.

Loading

0 comments on commit 5893f77

Please sign in to comment.