From c95a023459de2c798709ba39b43ee07a04014c0d Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Wed, 16 Oct 2024 11:52:13 +0100 Subject: [PATCH] refactor: :recycle: refactor plugin-app-duration to typescript es module --- packages/plugin-app-duration/package.json | 15 ++++++++++- .../plugin-app-duration/rollup.config.npm.mjs | 25 +++++++++++++++++++ .../{app.js => src/app-duration.ts} | 9 +++++-- .../{app.test.ts => app-duration.test.ts} | 4 +-- packages/plugin-app-duration/tsconfig.json | 8 ++++++ tsconfig.json | 1 - 6 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 packages/plugin-app-duration/rollup.config.npm.mjs rename packages/plugin-app-duration/{app.js => src/app-duration.ts} (51%) rename packages/plugin-app-duration/test/{app.test.ts => app-duration.test.ts} (94%) create mode 100644 packages/plugin-app-duration/tsconfig.json diff --git a/packages/plugin-app-duration/package.json b/packages/plugin-app-duration/package.json index 795268d42e..c00b48ebd1 100644 --- a/packages/plugin-app-duration/package.json +++ b/packages/plugin-app-duration/package.json @@ -1,7 +1,15 @@ { "name": "@bugsnag/plugin-app-duration", "version": "8.0.0", - "main": "app.js", + "main": "dist/app-duration.js", + "types": "dist/types/app-duration.d.ts", + "exports": { + ".": { + "types": "./dist/types/app-duration.d.ts", + "default": "./dist/app-duration.js", + "import": "./dist/app-duration.mjs" + } + }, "description": "@bugsnag/js plugin to set app duration in browsers and node", "homepage": "https://www.bugsnag.com/", "repository": { @@ -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-app-duration/rollup.config.npm.mjs b/packages/plugin-app-duration/rollup.config.npm.mjs new file mode 100644 index 0000000000..328994acc2 --- /dev/null +++ b/packages/plugin-app-duration/rollup.config.npm.mjs @@ -0,0 +1,25 @@ +import createRollupConfig from '../../.rollup/index.mjs' + +export default createRollupConfig({ + input: 'src/app-duration.ts', + output: [ + { + dir: `dist`, + entryFileNames: '[name].js', + format: 'cjs', + preserveModules: true, + generatedCode: { + preset: 'es2015', + } + }, + { + dir: `dist`, + entryFileNames: '[name].mjs', + format: 'esm', + preserveModules: true, + generatedCode: { + preset: 'es2015', + } + } + ] +}) diff --git a/packages/plugin-app-duration/app.js b/packages/plugin-app-duration/src/app-duration.ts similarity index 51% rename from packages/plugin-app-duration/app.js rename to packages/plugin-app-duration/src/app-duration.ts index 7d3c370bf3..7b96b7c9ba 100644 --- a/packages/plugin-app-duration/app.js +++ b/packages/plugin-app-duration/src/app-duration.ts @@ -1,15 +1,20 @@ +import { Plugin } from 'packages/core/types' + let appStart = new Date() const reset = () => { appStart = new Date() } -module.exports = { +const plugin: Plugin = { name: 'appDuration', load: client => { client.addOnError(event => { const now = new Date() - event.app.duration = now - appStart + event.app.duration = Number(now) - Number(appStart) + // @ts-expect-error second argument is private API }, true) return { reset } } } + +export default plugin diff --git a/packages/plugin-app-duration/test/app.test.ts b/packages/plugin-app-duration/test/app-duration.test.ts similarity index 94% rename from packages/plugin-app-duration/test/app.test.ts rename to packages/plugin-app-duration/test/app-duration.test.ts index 7f6911d16c..a8d234ad57 100644 --- a/packages/plugin-app-duration/test/app.test.ts +++ b/packages/plugin-app-duration/test/app-duration.test.ts @@ -1,4 +1,4 @@ -import plugin from '../app' +import plugin from '../src/app-duration' import Client from '@bugsnag/core/client' import Event from '@bugsnag/core/event' @@ -39,7 +39,7 @@ describe('plugin-app-duration', () => { } } - const result = plugin.load(client) + const result = plugin.load(client as unknown as Client) const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) diff --git a/packages/plugin-app-duration/tsconfig.json b/packages/plugin-app-duration/tsconfig.json new file mode 100644 index 0000000000..c76cdab7cd --- /dev/null +++ b/packages/plugin-app-duration/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*.ts"], + "compilerOptions": { + "target": "ES2020" + } +} + \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index ba66910c52..2253bf436b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -66,7 +66,6 @@ "packages/delivery-x-domain-request", "packages/delivery-xml-http-request", "packages/in-flight", - "packages/plugin-app-duration", "packages/plugin-aws-lambda", "packages/plugin-browser-context", "packages/plugin-browser-device",