From b3677d90ca40174925d8e5553b1759629dbf78af Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Wed, 27 Nov 2024 17:12:33 +0000 Subject: [PATCH] refactor: :recycle: refactor plugin-strip-query-string to TypeScript and es modules --- .../plugin-strip-query-string/package.json | 17 +++++++++-- .../rollup.config.npm.mjs | 6 ++++ .../src/strip-query-string.ts | 29 +++++++++++++++++++ .../strip-query-string.js | 21 -------------- .../plugin-strip-query-string/tsconfig.json | 7 +++++ tsconfig.json | 1 - 6 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 packages/plugin-strip-query-string/rollup.config.npm.mjs create mode 100644 packages/plugin-strip-query-string/src/strip-query-string.ts delete mode 100644 packages/plugin-strip-query-string/strip-query-string.js create mode 100644 packages/plugin-strip-query-string/tsconfig.json diff --git a/packages/plugin-strip-query-string/package.json b/packages/plugin-strip-query-string/package.json index 9dec07209e..200730b7cc 100644 --- a/packages/plugin-strip-query-string/package.json +++ b/packages/plugin-strip-query-string/package.json @@ -1,7 +1,15 @@ { "name": "@bugsnag/plugin-strip-query-string", "version": "8.1.1", - "main": "strip-query-string.js", + "main": "dist/strip-query-string.js", + "types": "dist/types/strip-query-string.d.ts", + "exports": { + ".": { + "types": "./dist/types/strip-query-string.d.ts", + "default": "./dist/strip-query-string.js", + "import": "./dist/strip-query-string.mjs" + } + }, "description": "@bugsnag/js plugin to strip query string and document fragment from stackframe filenames", "homepage": "https://www.bugsnag.com/", "repository": { @@ -12,7 +20,7 @@ "access": "public" }, "files": [ - "*.js" + "dist" ], "author": "Bugsnag", "license": "MIT", @@ -21,5 +29,10 @@ }, "peerDependencies": { "@bugsnag/core": "^8.0.0" + }, + "scripts": { + "build": "npm run build:npm", + "build:npm": "rollup --config rollup.config.npm.mjs", + "clean": "rm -rf dist/*" } } diff --git a/packages/plugin-strip-query-string/rollup.config.npm.mjs b/packages/plugin-strip-query-string/rollup.config.npm.mjs new file mode 100644 index 0000000000..f98e9f2d76 --- /dev/null +++ b/packages/plugin-strip-query-string/rollup.config.npm.mjs @@ -0,0 +1,6 @@ +import createRollupConfig from "../../.rollup/index.mjs"; + +export default createRollupConfig({ + input: "src/strip-query-string.ts", + external: ["@bugsnag/core/lib/es-utils/map", "@bugsnag/core/lib/es-utils/reduce"] +}); diff --git a/packages/plugin-strip-query-string/src/strip-query-string.ts b/packages/plugin-strip-query-string/src/strip-query-string.ts new file mode 100644 index 0000000000..856328ec3c --- /dev/null +++ b/packages/plugin-strip-query-string/src/strip-query-string.ts @@ -0,0 +1,29 @@ +/* + * Remove query strings (and fragments) from stacktraces + */ +import { Plugin, Stackframe } from '@bugsnag/core' +import map from '@bugsnag/core/lib/es-utils/map' +import reduce from '@bugsnag/core/lib/es-utils/reduce' + +const strip = (str: any) => + typeof str === 'string' + ? str.replace(/\?.*$/, '').replace(/#.*$/, '') + : str + +interface ExtendedPlugin extends Plugin { + _strip: typeof strip +} + +const plugin: ExtendedPlugin = { + load: (client) => { + client.addOnError(event => { + const allFrames: Stackframe[] = reduce(event.errors, (accum, er) => accum.concat(er.stacktrace), []) + map(allFrames, frame => { + frame.file = strip(frame.file) + }) + }) + }, + _strip: strip +} + +export default plugin diff --git a/packages/plugin-strip-query-string/strip-query-string.js b/packages/plugin-strip-query-string/strip-query-string.js deleted file mode 100644 index c5c15b6836..0000000000 --- a/packages/plugin-strip-query-string/strip-query-string.js +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Remove query strings (and fragments) from stacktraces - */ -const map = require('@bugsnag/core/lib/es-utils/map') -const reduce = require('@bugsnag/core/lib/es-utils/reduce') - -module.exports = { - load: (client) => { - client.addOnError(event => { - const allFrames = reduce(event.errors, (accum, er) => accum.concat(er.stacktrace), []) - map(allFrames, frame => { - frame.file = strip(frame.file) - }) - }) - } -} - -const strip = module.exports._strip = str => - typeof str === 'string' - ? str.replace(/\?.*$/, '').replace(/#.*$/, '') - : str diff --git a/packages/plugin-strip-query-string/tsconfig.json b/packages/plugin-strip-query-string/tsconfig.json new file mode 100644 index 0000000000..9478c51300 --- /dev/null +++ b/packages/plugin-strip-query-string/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*.ts"], + "compilerOptions": { + "target": "ES2020" + } +} diff --git a/tsconfig.json b/tsconfig.json index 3f3c508742..4c498ea7d9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -74,7 +74,6 @@ "packages/plugin-koa", "packages/plugin-restify", "packages/node", - "packages/plugin-strip-query-string", "packages/plugin-strip-project-root", "packages/plugin-interaction-breadcrumbs", "packages/plugin-intercept",