From 47121da1c440e3c31a0bed04c05f78098c90e1db Mon Sep 17 00:00:00 2001 From: Tim Kendrick Date: Fri, 22 Dec 2023 16:03:59 +0000 Subject: [PATCH 1/2] Enable source maps for debugging codemods --- packages/cli/codemods.vite.config.ts | 1 + packages/codemods/vite.config.ts | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/codemods.vite.config.ts b/packages/cli/codemods.vite.config.ts index 5d24f403..88f1a6fe 100644 --- a/packages/cli/codemods.vite.config.ts +++ b/packages/cli/codemods.vite.config.ts @@ -10,6 +10,7 @@ export default mergeConfig( defineConfig({ build: { outDir: resolve(__dirname, 'dist', 'node_modules', pkg.name), + sourcemap: false, }, }), ); diff --git a/packages/codemods/vite.config.ts b/packages/codemods/vite.config.ts index e4a5caff..ee4dc856 100644 --- a/packages/codemods/vite.config.ts +++ b/packages/codemods/vite.config.ts @@ -43,7 +43,6 @@ export default mergeConfig( formats: ['cjs'], fileName: '[name]', }, - sourcemap: false, }, }), ); From eacd3bfc9de8986b78eff9800d97ffaa00add4d5 Mon Sep 17 00:00:00 2001 From: Tim Kendrick Date: Fri, 22 Dec 2023 16:04:36 +0000 Subject: [PATCH 2/2] Serialize worker errors --- packages/worker-utils/lib.test.ts | 1 + packages/worker-utils/src/worker.ts | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/worker-utils/lib.test.ts b/packages/worker-utils/lib.test.ts index 705a6271..967f264e 100644 --- a/packages/worker-utils/lib.test.ts +++ b/packages/worker-utils/lib.test.ts @@ -7,5 +7,6 @@ test('module exports', () => { configureWorkerTask: lib.configureWorkerTask, createFsHelpers: lib.createFsHelpers, initTaskWorker: lib.initTaskWorker, + serializeWorkerError: lib.serializeWorkerError, }); }); diff --git a/packages/worker-utils/src/worker.ts b/packages/worker-utils/src/worker.ts index 60f882be..ba4a3378 100644 --- a/packages/worker-utils/src/worker.ts +++ b/packages/worker-utils/src/worker.ts @@ -42,7 +42,7 @@ export function initTaskWorker( runWorkerTask(task, data, runner) .then( (result) => ({ success: true as const, value: result }), - (error) => ({ success: false as const, error: error.message }), + (error) => ({ success: false as const, error: serializeWorkerError(error) }), ) .then((output) => channel.postMessage(output)); }); @@ -75,3 +75,25 @@ function runWorkerTask( return Promise.reject(error); } } + +export function serializeWorkerError(error: unknown): Error { + if (error instanceof Error) { + if (isSerializable(error)) return error; + const { name, message, stack, ...properties } = error; + return Object.assign(new Error(message), { + name, + stack, + ...properties, + }); + } + return new Error(String(error)); +} + +function isSerializable(value: unknown): boolean { + try { + structuredClone(value); + return true; + } catch { + return false; + } +}