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

Open
wants to merge 51 commits into
base: integration/typescript
Choose a base branch
from

Conversation

gingerbenw
Copy link
Member

@gingerbenw gingerbenw commented Nov 12, 2024

Goal

This PR converts the last of the internal browser plugins and the browser client to TypeScript, using Rollup to bundle both common js and es modules

Changeset

  • Convert browser package to TypeScript
  • Convert plugin-simple-throttle to TypeScript
  • Convert plugin-interaction-breadcrumbs to TypeScript
  • Convert plugin-navigation-breadcrumbs to TypeScript
  • Create browser bundle for both npm and cdn using rollup

Testing

Covered by existing test suite

@gingerbenw gingerbenw force-pushed the integration/typescript branch from 9a4591e to 7bbbff9 Compare December 5, 2024 16:04
@gingerbenw gingerbenw changed the title Plat 12163/browser Refactor @bugsnag/browser Dec 5, 2024
Copy link

github-actions bot commented Dec 10, 2024

@bugsnag/browser bundle size diff

Minified Minfied + Gzipped
Before 46.15 kB 13.86 kB
After 48.86 kB 15.14 kB
± ⚠️ +2,710 bytes ⚠️ +1,275 bytes

code coverage diff

<temporarily disabled>

Generated by 🚫 dangerJS against 3196415

jest.config.js Outdated
@@ -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

Comment on lines 34 to 35
"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'",
Copy link
Contributor

Choose a reason for hiding this comment

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

can these be removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

yep - and possibly the minification scripts, too?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not yet as they are being used for e.g. plugin-react and others

export default assign(Bugsnag, { Client, Event, Session, Breadcrumb })
export type { BrowserBugsnagStatic, BrowserConfig } from './bugsnag'

export type { Client, Event, Session, Breadcrumb, Plugin } from '@bugsnag/core'
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we would do export type * from '@bugsnag/core' but that's not supported with the current version of TypeScript we're using

Copy link
Contributor

Choose a reason for hiding this comment

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

TODO: Check what other types need exporting here

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually I don't think any types need exporting from cjs/umd entry points

import pluginNavigationBreadcrumbs from '@bugsnag/plugin-navigation-breadcrumbs'
import pluginInteractionBreadcrumbs from '@bugsnag/plugin-interaction-breadcrumbs'
// @ts-ignore
import pluginInlineScriptContent from '@bugsnag/plugin-inline-script-content'
Copy link
Contributor

Choose a reason for hiding this comment

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

I reverted the TS conversion of this plugin from this PR because it was broken


const sharedOutput = {
...commonSharedOutput,
strict: false, // 'use strict' in WebKit enables Tail Call Optimization, which breaks stack trace handling
Copy link
Contributor

Choose a reason for hiding this comment

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

There is one tail call in out stack and our depth handling does not handle it. With TCO enabled the stack frame for the tail call is removed, which then causes problems with stack processing leading to unexpected results (only in WebKit browsers since that's the only one supporting TCO):

Screenshot 2024-12-27 at 10 14 05

Dropping 'use strict' prevents TCO being enabled and resolves the issue

Dan Skinner added 2 commits December 27, 2024 10:38
"@rollup/plugin-node-resolve": "^16.0.0",
"@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-terser": "^0.4.4",
"tslib": "^2.8.1"
Copy link
Contributor

Choose a reason for hiding this comment

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

tslib is required for target: es3

@gingerbenw gingerbenw marked this pull request as ready for review January 6, 2025 10:54
Comment on lines +1 to +7
import Client from '@bugsnag/core/client'
// @ts-ignore
import Event from '@bugsnag/core/event'
// @ts-ignore
import Session from '@bugsnag/core/session'
// @ts-ignore
import Breadcrumb from '@bugsnag/core/breadcrumb'
Copy link
Member Author

Choose a reason for hiding this comment

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

Assuming these ignores will be removed once we've finished conversion of the core package

@@ -1,10 +1,13 @@
import { Plugin } from '@bugsnag/core'
import type ClientWithInternals from 'packages/core/client'
Copy link
Member Author

Choose a reason for hiding this comment

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

so this import is working fine?

Copy link
Contributor

@djskinner djskinner Jan 6, 2025

Choose a reason for hiding this comment

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

This one happens to work fine because that import path doesn't appear in the final generated types:

import { Plugin } from '@bugsnag/core';
declare const _default: (win?: Window & typeof globalThis) => Plugin;
export default _default;
//# sourceMappingURL=interaction-breadcrumbs.d.ts.map

@@ -23,7 +26,8 @@ module.exports = (win = window) => ({

const trim = /^\s*([^\s][\s\S]{0,139}[^\s])?\s*/

function getNodeText (el) {
// TODO: Fix Type
Copy link
Member Author

Choose a reason for hiding this comment

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

Want me to take some of these TODOs ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants