From e41f7c3efaef589029bd947ff7c49eec080d9ab4 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Mon, 2 Sep 2024 11:56:01 +0300 Subject: [PATCH] ref: Fix up some tsconfig schenanigans (#512) This patch streamlines our `tsconfig`s across packages, always emitting `.d.ts` files (and only them) with using `src` as the root to avoid the `dist/src/something.d.ts` scenario entirely. --- .gitignore | 3 +- packages/astro/src/index.ts | 4 +-- packages/overlay/src/App.tsx | 4 +-- packages/overlay/src/index.tsx | 30 +++++++++---------- .../overlay/src/integrations/integration.ts | 8 ++--- .../sentry/data/sentryDataCache.spec.ts} | 6 ++-- .../integrations/sentry/index.spec.ts} | 8 ++--- .../overlay/src/integrations/sentry/index.ts | 18 ++++++----- .../integrations/sentry/utils/traces.spec.ts} | 6 ++-- packages/overlay/tsconfig.json | 6 ++-- packages/overlay/vitest.config.ts | 2 +- packages/sidecar/tsconfig.json | 6 ++-- packages/spotlight/tsconfig.json | 4 +-- packages/tsconfig/tsconfig.json | 7 +++-- 14 files changed, 58 insertions(+), 54 deletions(-) rename packages/overlay/{test/integrations/sentry/data/sentryDataCache.test.ts => src/integrations/sentry/data/sentryDataCache.spec.ts} (76%) rename packages/overlay/{test/integrations/sentry/index.test.ts => src/integrations/sentry/index.spec.ts} (95%) rename packages/overlay/{test/integrations/sentry/utils/traces.test.ts => src/integrations/sentry/utils/traces.spec.ts} (96%) diff --git a/.gitignore b/.gitignore index 84051deb..d8409581 100644 --- a/.gitignore +++ b/.gitignore @@ -38,4 +38,5 @@ sentry.properties junit.xml # Build artifacts -*.tgz \ No newline at end of file +*.tgz +tsconfig.tsbuildinfo diff --git a/packages/astro/src/index.ts b/packages/astro/src/index.ts index 4f200ed5..4b48bba8 100644 --- a/packages/astro/src/index.ts +++ b/packages/astro/src/index.ts @@ -2,8 +2,8 @@ import type { SpotlightInitOptions } from '@spotlightjs/spotlight/vite-plugin'; import type { AstroConfig, AstroIntegration } from 'astro'; import { buildServerSnippet } from './snippets'; -import path from 'path'; -import url from 'url'; +import path from 'node:path'; +import url from 'node:url'; import spotlight, { buildClientInit } from '@spotlightjs/spotlight/vite-plugin'; diff --git a/packages/overlay/src/App.tsx b/packages/overlay/src/App.tsx index 5be5a48f..a1266cdb 100644 --- a/packages/overlay/src/App.tsx +++ b/packages/overlay/src/App.tsx @@ -44,9 +44,9 @@ export default function App({ } } - const result: Record void> = Object.create(null); + const result: Record void> = Object.create(null); for (const [contentType, integrations] of contentTypeToIntegrations.entries()) { - const listener = (event: { data: string | Buffer }): void => { + const listener = (event: { data: string | Uint8Array }): void => { log(`Received new ${contentType} event`); for (const integration of integrations) { const newIntegrationData = integration.processEvent diff --git a/packages/overlay/src/index.tsx b/packages/overlay/src/index.tsx index e39abfa9..bfd4ad48 100644 --- a/packages/overlay/src/index.tsx +++ b/packages/overlay/src/index.tsx @@ -2,22 +2,22 @@ import fontStyles from '@fontsource/raleway/index.css?inline'; import { CONTEXT_LINES_ENDPOINT } from '@spotlightjs/sidecar/constants'; import { MemoryRouter } from 'react-router-dom'; import colors from 'tailwindcss/colors'; -import App from './App.tsx'; -import { DEFAULT_ANCHOR, DEFAULT_EXPERIMENTS, DEFAULT_SIDECAR_URL } from './constants.ts'; +import App from './App'; +import { DEFAULT_ANCHOR, DEFAULT_EXPERIMENTS, DEFAULT_SIDECAR_URL } from './constants'; import globalStyles from './index.css?inline'; -import { initIntegrations, type SpotlightContext } from './integrations/integration.ts'; -import { default as sentry } from './integrations/sentry/index.ts'; -import { off, on, trigger } from './lib/eventTarget.ts'; -import { activateLogger, log } from './lib/logger.ts'; -import { SpotlightContextProvider } from './lib/useSpotlightContext.tsx'; -import { React, ReactDOM } from './react-instance.tsx'; // Import specific exports -import type { SpotlightOverlayOptions, WindowWithSpotlight } from './types.ts'; - -export { default as console } from './integrations/console/index.ts'; -export { default as hydrationError } from './integrations/hydration-error/index.ts'; -export { default as sentry } from './integrations/sentry/index.ts'; -export { default as viteInspect } from './integrations/vite-inspect/index.ts'; -export type { SpotlightOverlayOptions, WindowWithSpotlight } from './types.ts'; +import { initIntegrations, type SpotlightContext } from './integrations/integration'; +import { default as sentry } from './integrations/sentry/index'; +import { off, on, trigger } from './lib/eventTarget'; +import { activateLogger, log } from './lib/logger'; +import { SpotlightContextProvider } from './lib/useSpotlightContext'; +import { React, ReactDOM } from './react-instance'; // Import specific exports +import type { SpotlightOverlayOptions, WindowWithSpotlight } from './types'; + +export { default as console } from './integrations/console/index'; +export { default as hydrationError } from './integrations/hydration-error/index'; +export { default as sentry } from './integrations/sentry/index'; +export { default as viteInspect } from './integrations/vite-inspect/index'; +export type { SpotlightOverlayOptions, WindowWithSpotlight } from './types'; export { CONTEXT_LINES_ENDPOINT, DEFAULT_ANCHOR, diff --git a/packages/overlay/src/integrations/integration.ts b/packages/overlay/src/integrations/integration.ts index 4dffdd3e..d279ec70 100644 --- a/packages/overlay/src/integrations/integration.ts +++ b/packages/overlay/src/integrations/integration.ts @@ -1,5 +1,5 @@ -import { type ComponentType } from 'react'; -import { type ExperimentsConfig, type NotificationCount } from '~/types'; +import type { ComponentType } from 'react'; +import type { ExperimentsConfig, NotificationCount } from '../types'; export type SpotlightContext = { open: (path: string | undefined) => void; @@ -114,7 +114,7 @@ export type RawEventContext = { * * Return the processed object or undefined if the event should be ignored. */ - data: string | Buffer; + data: string | Uint8Array; }; type TeardownFunction = () => void | Promise<() => void>; @@ -122,7 +122,7 @@ type TeardownFunction = () => void | Promise<() => void>; // export type IntegrationParameter = Array>; export async function initIntegrations( - integrations: Integration[] = [], + integrations: Integration[], context: SpotlightContext, ): Promise<[Integration[], TeardownFunction[]]> { if (!integrations) { diff --git a/packages/overlay/test/integrations/sentry/data/sentryDataCache.test.ts b/packages/overlay/src/integrations/sentry/data/sentryDataCache.spec.ts similarity index 76% rename from packages/overlay/test/integrations/sentry/data/sentryDataCache.test.ts rename to packages/overlay/src/integrations/sentry/data/sentryDataCache.spec.ts index c483efea..0e3e0329 100644 --- a/packages/overlay/test/integrations/sentry/data/sentryDataCache.test.ts +++ b/packages/overlay/src/integrations/sentry/data/sentryDataCache.spec.ts @@ -1,8 +1,8 @@ import { describe, expect, test } from 'vitest'; -import sentryDataCache from '~/integrations/sentry/data/sentryDataCache'; -import { processEnvelope } from '~/integrations/sentry/index'; +import { processEnvelope } from '../index'; +import sentryDataCache from './sentryDataCache'; -import fs from 'fs'; +import fs from 'node:fs'; describe('SentryDataCache', () => { // We need to refactor this to make it actually testable diff --git a/packages/overlay/test/integrations/sentry/index.test.ts b/packages/overlay/src/integrations/sentry/index.spec.ts similarity index 95% rename from packages/overlay/test/integrations/sentry/index.test.ts rename to packages/overlay/src/integrations/sentry/index.spec.ts index 692bd553..da6775f9 100644 --- a/packages/overlay/test/integrations/sentry/index.test.ts +++ b/packages/overlay/src/integrations/sentry/index.spec.ts @@ -1,9 +1,9 @@ import { describe, expect, test } from 'vitest'; -import { processEnvelope } from '~/integrations/sentry/index'; +import { processEnvelope } from './index'; -import { Event } from '@sentry/types'; -import fs from 'fs'; -import sentryDataCache from '~/integrations/sentry/data/sentryDataCache'; +import type { Event } from '@sentry/types'; +import fs from 'node:fs'; +import sentryDataCache from './data/sentryDataCache'; describe('Sentry Integration', () => { test('Process Envelope Empty', () => { diff --git a/packages/overlay/src/integrations/sentry/index.ts b/packages/overlay/src/integrations/sentry/index.ts index 843ea654..a5eeffef 100644 --- a/packages/overlay/src/integrations/sentry/index.ts +++ b/packages/overlay/src/integrations/sentry/index.ts @@ -117,7 +117,7 @@ function getLineEnd(data: Uint8Array): number { return end; } -function parseJSONFromBuffer(data: Uint8Array) { +function parseJSONFromBuffer(data: Uint8Array): object { return JSON.parse(new TextDecoder().decode(data)); } @@ -144,19 +144,21 @@ export function processEnvelope(rawEvent: RawEventContext) { while (buffer.length) { const itemHeader = parseJSONFromBuffer(readLine()) as EnvelopeItem[0]; const payloadLength = itemHeader.length; - let itemPayload = readLine(payloadLength != null ? payloadLength : undefined); + const itemPayloadRaw = readLine(payloadLength != null ? payloadLength : undefined); + let itemPayload: EnvelopeItem[1]; try { - itemPayload = parseJSONFromBuffer(itemPayload); + itemPayload = parseJSONFromBuffer(itemPayloadRaw); + // data sanitization + if (itemHeader.type) { + // @ts-expect-error ts(2339) -- We should really stop adding type to payloads + itemPayload.type = itemHeader.type; + } } catch (err) { + itemPayload = itemPayloadRaw; log(err); } - // data sanitization - if (itemHeader.type && typeof itemPayload === 'object') { - // @ts-expect-error -- Does not like assigning to `type` on random object - itemPayload.type = itemHeader.type; - } items.push([itemHeader, itemPayload] as EnvelopeItem); } diff --git a/packages/overlay/test/integrations/sentry/utils/traces.test.ts b/packages/overlay/src/integrations/sentry/utils/traces.spec.ts similarity index 96% rename from packages/overlay/test/integrations/sentry/utils/traces.test.ts rename to packages/overlay/src/integrations/sentry/utils/traces.spec.ts index d0d0c654..cd80c853 100644 --- a/packages/overlay/test/integrations/sentry/utils/traces.test.ts +++ b/packages/overlay/src/integrations/sentry/utils/traces.spec.ts @@ -1,7 +1,7 @@ import { describe, expect, test } from 'vitest'; -import { Span } from '~/integrations/sentry/types'; -import { groupSpans } from '~/integrations/sentry/utils/traces'; -import { generate_uuidv4 } from '~/lib/uuid'; +import { generate_uuidv4 } from '../../../lib/uuid'; +import type { Span } from '../types'; +import { groupSpans } from './traces'; function mockSpan({ duration, ...span }: Partial & { duration?: number } = {}): Span { const defaultTimestamp = new Date().getTime(); diff --git a/packages/overlay/tsconfig.json b/packages/overlay/tsconfig.json index 3f71a6e7..8a63f429 100644 --- a/packages/overlay/tsconfig.json +++ b/packages/overlay/tsconfig.json @@ -4,8 +4,10 @@ "baseUrl": ".", "paths": { "~/*": ["src/*"] - } + }, + "outDir": "dist", + "rootDir": "src" }, - "include": ["src", "test"], + "include": ["src"], "references": [{ "path": "./tsconfig.node.json" }] } diff --git a/packages/overlay/vitest.config.ts b/packages/overlay/vitest.config.ts index 71962fae..bb2023a1 100644 --- a/packages/overlay/vitest.config.ts +++ b/packages/overlay/vitest.config.ts @@ -12,7 +12,7 @@ export default defineConfig({ reporter: ['json'], }, globals: true, - include: ['./test/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], + include: ['./src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], watchExclude: ['.*\\/node_modules\\/.*', '.*\\/dist\\/.*'], }, resolve: { diff --git a/packages/sidecar/tsconfig.json b/packages/sidecar/tsconfig.json index dc2c9863..3f598a80 100644 --- a/packages/sidecar/tsconfig.json +++ b/packages/sidecar/tsconfig.json @@ -9,11 +9,9 @@ "esModuleInterop": false, "target": "esnext", "module": "nodenext", + /* paths */ "outDir": "dist", - "noEmit": false, - "allowImportingTsExtensions": false, - "declaration": true, - "emitDeclarationOnly": true + "rootDir": "src" }, "include": ["src/*"] } diff --git a/packages/spotlight/tsconfig.json b/packages/spotlight/tsconfig.json index 908d68ed..597e7ba5 100644 --- a/packages/spotlight/tsconfig.json +++ b/packages/spotlight/tsconfig.json @@ -3,9 +3,7 @@ "compilerOptions": { "baseUrl": ".", "skipLibCheck": true, - "emitDeclarationOnly": true, - "declaration": true, - "noEmit": false, + /* paths */ "outDir": "dist", "rootDir": "src" }, diff --git a/packages/tsconfig/tsconfig.json b/packages/tsconfig/tsconfig.json index a7fc6fbf..efce7fa8 100644 --- a/packages/tsconfig/tsconfig.json +++ b/packages/tsconfig/tsconfig.json @@ -5,13 +5,16 @@ "lib": ["ES2020", "DOM", "DOM.Iterable"], "module": "ESNext", "skipLibCheck": true, + "allowImportingTsExtensions": false, + /* cannot enable composite mode yet as vite clears output directory, requiring a tsc rebuild all the time */ + "composite": false, + "declaration": true, + "emitDeclarationOnly": true, /* Bundler mode */ "moduleResolution": "bundler", - "allowImportingTsExtensions": true, "resolveJsonModule": true, "isolatedModules": true, - "noEmit": true, "jsx": "react-jsx", /* Linting */