Skip to content

Commit 85efc89

Browse files
gingerbenwCopilot
andcommitted
fix browser build
Co-authored-by: Copilot <copilot@github.com>
1 parent 054c767 commit 85efc89

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

packages/browser/package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22
"name": "@bugsnag/browser",
33
"version": "8.8.1",
44
"main": "dist/index-cjs.js",
5-
"types": "dist/types/index-es.d.ts",
5+
"module": "dist/index-es.mjs",
6+
"types": "dist/types/index-cjs.d.cts",
67
"browser": "./dist/bugsnag.js",
78
"sideEffects": false,
9+
"exports": {
10+
".": {
11+
"types": {
12+
"import": "./dist/types/index-es.d.mts",
13+
"require": "./dist/types/index-cjs.d.cts"
14+
},
15+
"import": "./dist/index-es.mjs",
16+
"require": "./dist/index-cjs.js"
17+
}
18+
},
819
"description": "Bugsnag error reporter for browser JavaScript",
920
"homepage": "https://www.bugsnag.com/",
1021
"repository": {
@@ -22,6 +33,7 @@
2233
"clean": "rm -fr dist && mkdir dist",
2334
"prebuild": "npm run clean",
2435
"build": "rollup --config rollup.config.mjs",
36+
"postbuild": "node scripts/fix-cjs-types.js",
2537
"cdn-upload": "../../bin/cdn-upload dist/*"
2638
},
2739
"author": "Bugsnag",

packages/browser/rollup.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default [
9292
{
9393
...sharedOutput,
9494
preserveModules: false,
95-
entryFileNames: '[name].js',
95+
entryFileNames: '[name].mjs',
9696
format: 'esm'
9797
}
9898
],
@@ -107,6 +107,8 @@ export default [
107107
...sharedOutput,
108108
entryFileNames: '[name].js',
109109
format: 'cjs',
110+
exports: 'default',
111+
interop: 'compat'
110112
},
111113
],
112114
plugins,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Post-build script to create proper type declaration files
5+
* - CJS types with 'export =' syntax
6+
* - ESM types with .d.mts extension for proper module detection
7+
*/
8+
9+
const fs = require('fs');
10+
const path = require('path');
11+
12+
// Fix CommonJS types: change 'export default' to 'export ='
13+
const cjsSourceFile = path.join(__dirname, '../dist/types/index-cjs.d.ts');
14+
const cjsTargetFile = path.join(__dirname, '../dist/types/index-cjs.d.cts');
15+
16+
let cjsContent = fs.readFileSync(cjsSourceFile, 'utf8');
17+
cjsContent = cjsContent.replace(
18+
/export default (_default);/,
19+
'export = $1;'
20+
);
21+
fs.writeFileSync(cjsTargetFile, cjsContent);
22+
console.log('Created CommonJS type declaration: dist/types/index-cjs.d.cts');
23+
24+
// Fix ESM types: copy to .d.mts extension and add .js extensions to relative imports
25+
const esmSourceFile = path.join(__dirname, '../dist/types/index-es.d.ts');
26+
const esmTargetFile = path.join(__dirname, '../dist/types/index-es.d.mts');
27+
28+
let esmContent = fs.readFileSync(esmSourceFile, 'utf8');
29+
// Add .js extensions to relative imports for Node16 ESM resolution
30+
esmContent = esmContent.replace(/from '(\.\/.+?)';/g, "from '$1.js';");
31+
fs.writeFileSync(esmTargetFile, esmContent);
32+
console.log('Created ESM type declaration: dist/types/index-es.d.mts');

0 commit comments

Comments
 (0)