Skip to content

Commit c674a14

Browse files
committed
fix: strip export keywords from browser-injected scripts
browserScript.ts reads compiled serialize.js and logUncaughtErrors.js at runtime and injects them into the browser via page.evaluate(). With ESM output, these files have `export` keywords which are invalid in non-module browser script context. Strip them on read. Assisted-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9dd9337 commit c674a14

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

packages/browser-logs/src/browserScript.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ import fs from 'fs';
22
import path from 'path';
33

44
const REGEXP_SOURCE_MAP = /\/\/# sourceMappingURL=.*/;
5+
const REGEXP_EXPORT = /^export /gm;
56

7+
// @ts-ignore import.meta.dirname works at runtime on Node 24
8+
const __dir = import.meta.dirname;
69
const serializeScript = fs
7-
.readFileSync(path.resolve(import.meta.dirname, 'serialize.js'), 'utf-8')
8-
.replace(REGEXP_SOURCE_MAP, '');
10+
.readFileSync(path.resolve(__dir, 'serialize.js'), 'utf-8')
11+
.replace(REGEXP_SOURCE_MAP, '')
12+
.replace(REGEXP_EXPORT, '');
913
const logUncaughtErrorsScript = fs
10-
.readFileSync(path.resolve(import.meta.dirname, 'logUncaughtErrors.js'), 'utf-8')
11-
.replace(REGEXP_SOURCE_MAP, '');
14+
.readFileSync(path.resolve(__dir, 'logUncaughtErrors.js'), 'utf-8')
15+
.replace(REGEXP_SOURCE_MAP, '')
16+
.replace(REGEXP_EXPORT, '');
1217

1318
/**
1419
* Create the browser script as an IIFE wrapper.

0 commit comments

Comments
 (0)