-
Notifications
You must be signed in to change notification settings - Fork 255
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 #2289 from bugsnag/plat-13218
web worker: TypeScript and rollup
- Loading branch information
Showing
24 changed files
with
860 additions
and
913 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const babelConfig = require('../../babel.config.js') | ||
|
||
module.exports = babelConfig |
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 |
---|---|---|
|
@@ -3,9 +3,16 @@ | |
"version": "8.1.1", | ||
"description": "BugSnag error reporter for JavaScript web workers and service workers", | ||
"homepage": "https://www.bugsnag.com/", | ||
"main": "dist/bugsnag.web-worker.js", | ||
"module": "dist/bugsnag.web-worker.js", | ||
"types": "types/notifier.ts", | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "dist/types/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./dist/types/index.d.ts", | ||
"import": "./dist/index.js", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:bugsnag/bugsnag-js.git" | ||
|
@@ -14,8 +21,7 @@ | |
"access": "public" | ||
}, | ||
"files": [ | ||
"dist", | ||
"types" | ||
"dist" | ||
], | ||
"keywords": [ | ||
"worker", | ||
|
@@ -28,11 +34,8 @@ | |
], | ||
"scripts": { | ||
"clean": "rm -fr dist && mkdir dist", | ||
"build": "npm run clean && npm run build:dist && npm run build:dist:min", | ||
"build:dist": "webpack", | ||
"build:dist:min": "webpack --optimization-minimize --output-filename=bugsnag.web-worker.min.js", | ||
"build:dist:esm": "webpack --config esm.config.js", | ||
"build:dist:esm.min": "webpack --config esm.config.js --optimization-minimize --output-filename=bugsnag.web-worker.min.mjs", | ||
"build": "npm run clean && npm run build:npm", | ||
"build:npm": "rollup --config rollup.config.npm.mjs", | ||
"size": "../../bin/size dist/bugsnag.web-worker.min.js", | ||
"cdn-upload": "../../bin/cdn-upload dist/*" | ||
}, | ||
|
@@ -46,9 +49,15 @@ | |
"@bugsnag/plugin-client-ip": "^8.1.1", | ||
"@bugsnag/plugin-window-onerror": "^8.1.1", | ||
"@bugsnag/plugin-window-unhandled-rejection": "^8.1.1", | ||
"ts-loader": "^9.4.1", | ||
"typescript": "^4.9.3", | ||
"webpack": "^5.75.0", | ||
"webpack-cli": "^5.0.0" | ||
"@rollup/plugin-babel": "^6.0.4", | ||
"@rollup/plugin-commonjs": "^28.0.2", | ||
"@rollup/plugin-node-resolve": "^16.0.0", | ||
"@rollup/plugin-replace": "^6.0.2", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"rollup": "^4.31.0", | ||
"typescript": "^4.9.3" | ||
}, | ||
"dependencies": { | ||
"@bugsnag/core": "^8.1.1" | ||
} | ||
} |
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,69 @@ | ||
import babel from '@rollup/plugin-babel' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import nodeResolve from '@rollup/plugin-node-resolve' | ||
import replace from '@rollup/plugin-replace' | ||
import terser from '@rollup/plugin-terser' | ||
import typescript from '@rollup/plugin-typescript' | ||
import fs from 'fs' | ||
|
||
import createRollupConfig, { sharedOutput } from '../../.rollup/index.mjs' | ||
|
||
const packageJson = JSON.parse(fs.readFileSync('./package.json')) | ||
|
||
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' }), | ||
replace({ | ||
preventAssignment: true, | ||
values: { | ||
'process.env.NODE_ENV': JSON.stringify('production'), | ||
__BUGSNAG_NOTIFIER_VERSION__: JSON.stringify(packageJson.version), | ||
} | ||
}) | ||
] | ||
|
||
export default [ | ||
createRollupConfig({ | ||
input: 'src/index.ts', | ||
output: [ | ||
{ | ||
...sharedOutput, | ||
preserveModules: false, | ||
entryFileNames: '[name].js', | ||
format: 'esm' | ||
} | ||
], | ||
plugins | ||
}), | ||
createRollupConfig({ | ||
input: 'src/index-umd.ts', | ||
output: [ | ||
{ | ||
...sharedOutput, | ||
entryFileNames: 'bugsnag.web-worker.js', | ||
format: 'umd', | ||
name: 'Bugsnag' | ||
}, | ||
{ | ||
...sharedOutput, | ||
entryFileNames: 'bugsnag.web-worker.min.js', | ||
format: 'umd', | ||
compact: true, | ||
name: 'Bugsnag', | ||
plugins: [terser()] | ||
} | ||
], | ||
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,101 @@ | ||
/* eslint-env worker, serviceworker */ | ||
|
||
import delivery from '@bugsnag/delivery-fetch' | ||
import pluginClientIp from '@bugsnag/plugin-client-ip' | ||
import pluginWindowOnError from '@bugsnag/plugin-window-onerror' | ||
import pluginWindowUnhandledRejection from '@bugsnag/plugin-window-unhandled-rejection' | ||
|
||
// extend the base config schema with some browser-specific options | ||
import { schema as baseConfig } from '@bugsnag/core/config' | ||
import workerConfig from './config' | ||
import pluginBrowserDevice from '@bugsnag/plugin-browser-device' | ||
import pluginBrowserSession from '@bugsnag/plugin-browser-session' | ||
import pluginPreventDiscard from './prevent-discard' | ||
import ClientWithInternals from '@bugsnag/core/client' | ||
import type { Client, Config, BugsnagStatic } from '@bugsnag/core' | ||
import assign from '@bugsnag/core/lib/es-utils/assign' | ||
|
||
export interface WorkerConfig extends Config { | ||
collectUserIp?: boolean | ||
generateAnonymousId?: boolean | ||
} | ||
|
||
export interface WorkerBugsnagStatic extends BugsnagStatic { | ||
start(apiKeyOrOpts: string | WorkerConfig): Client | ||
createClient(apiKeyOrOpts: string | WorkerConfig): Client | ||
} | ||
|
||
const name = 'Bugsnag Web Worker' | ||
const url = 'https://github.com/bugsnag/bugsnag-js' | ||
// @ts-ignore | ||
const version = __BUGSNAG_NOTIFIER_VERSION__ | ||
|
||
// extend the base config schema with some worker-specific options | ||
const schema = assign({}, baseConfig, workerConfig) | ||
|
||
type WorkerClient = Partial<ClientWithInternals> & { | ||
_client: ClientWithInternals | null | ||
createClient: (opts?: Config) => ClientWithInternals | ||
start: (opts?: Config) => ClientWithInternals | ||
isStarted: () => boolean | ||
} | ||
|
||
const notifier: WorkerClient = { | ||
_client: null, | ||
createClient: (opts) => { | ||
// handle very simple use case where user supplies just the api key as a string | ||
if (typeof opts === 'string') opts = { apiKey: opts } | ||
if (!opts) opts = {} as unknown as Config | ||
|
||
const internalPlugins = [ | ||
pluginBrowserDevice(navigator, null), | ||
pluginBrowserSession, | ||
pluginClientIp, | ||
pluginPreventDiscard, | ||
pluginWindowOnError(self, 'worker onerror'), | ||
pluginWindowUnhandledRejection(self) | ||
] | ||
|
||
// configure a client with user supplied options | ||
const bugsnag = new ClientWithInternals(opts, schema, internalPlugins, { name, version, url }) | ||
|
||
bugsnag._setDelivery(client => delivery(client, self.fetch)) | ||
|
||
bugsnag._logger.debug('Loaded!') | ||
|
||
return bugsnag._config.autoTrackSessions | ||
? bugsnag.startSession() | ||
: bugsnag | ||
}, | ||
start: (opts) => { | ||
if (notifier._client) { | ||
notifier._client._logger.warn('Bugsnag.start() was called more than once. Ignoring.') | ||
return notifier._client | ||
} | ||
notifier._client = notifier.createClient(opts) | ||
return notifier._client | ||
}, | ||
isStarted: () => { | ||
return notifier._client != null | ||
} | ||
} | ||
|
||
type Method = keyof typeof ClientWithInternals.prototype | ||
|
||
// Add client functions to notifier | ||
(Object.getOwnPropertyNames(ClientWithInternals.prototype) as Method[]).forEach(method => { | ||
// skip private methods | ||
// @ts-ignore | ||
if (/^_/.test(method) || method === 'constructor') return | ||
notifier[method] = function () { | ||
if (!notifier._client) return console.log(`Bugsnag.${method}() was called before Bugsnag.start()`) | ||
notifier._client._depth += 1 | ||
const ret = notifier._client[method].apply(notifier._client, arguments) | ||
notifier._client._depth -= 1 | ||
return ret | ||
} | ||
}) | ||
|
||
const Bugsnag = notifier as WorkerBugsnagStatic | ||
|
||
export default Bugsnag |
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,26 @@ | ||
/* eslint-env worker, serviceworker */ | ||
|
||
import { schema } from '@bugsnag/core/config' | ||
import assign from '@bugsnag/core/lib/es-utils/assign' | ||
import getPrefixedConsole from './get-prefixed-console' | ||
|
||
export default { | ||
appType: { | ||
...schema.appType, | ||
defaultValue: () => 'workerjs' | ||
}, | ||
logger: assign({}, schema.logger, { | ||
defaultValue: () => | ||
(typeof console !== 'undefined' && typeof console.debug === 'function') | ||
? getPrefixedConsole() | ||
: undefined | ||
}), | ||
autoTrackSessions: { | ||
...schema.autoTrackSessions, | ||
defaultValue: () => false | ||
}, | ||
autoDetectErrors: { | ||
...schema.autoTrackSessions, | ||
defaultValue: () => false | ||
} | ||
} |
Oops, something went wrong.