Skip to content

Commit 3fadb46

Browse files
gingerbenwCopilot
andcommitted
move 'fix cjs types' script to a rollup plugin
Co-authored-by: Copilot <copilot@github.com>
1 parent 85efc89 commit 3fadb46

3 files changed

Lines changed: 47 additions & 35 deletions

File tree

packages/browser/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"clean": "rm -fr dist && mkdir dist",
3434
"prebuild": "npm run clean",
3535
"build": "rollup --config rollup.config.mjs",
36-
"postbuild": "node scripts/fix-cjs-types.js",
3736
"cdn-upload": "../../bin/cdn-upload dist/*"
3837
},
3938
"author": "Bugsnag",

packages/browser/rollup.config.mjs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,45 @@ import replace from '@rollup/plugin-replace'
55
import terser from '@rollup/plugin-terser'
66
import typescript from '@rollup/plugin-typescript'
77
import fs from 'fs'
8+
import path from 'path'
9+
10+
// Plugin to fix TypeScript declaration files for proper module resolution
11+
function fixDeclarations(options = {}) {
12+
const { format, declarationExt } = options
13+
14+
return {
15+
name: 'fix-declarations',
16+
writeBundle() {
17+
const typesDir = path.join(process.cwd(), 'dist/types')
18+
19+
if (format === 'esm' && declarationExt === '.d.mts') {
20+
// Fix ESM declarations: rename to .d.mts and add .js extensions
21+
const sourceFile = path.join(typesDir, 'index-es.d.ts')
22+
const targetFile = path.join(typesDir, 'index-es.d.mts')
23+
24+
if (fs.existsSync(sourceFile)) {
25+
let content = fs.readFileSync(sourceFile, 'utf8')
26+
// Add .js extensions to relative imports for Node16 ESM resolution
27+
content = content.replace(/from '(\.\/.+?)';/g, "from '$1.js';")
28+
fs.writeFileSync(targetFile, content)
29+
console.log('Created ESM type declaration: dist/types/index-es.d.mts')
30+
}
31+
} else if (format === 'cjs' && declarationExt === '.d.cts') {
32+
// Fix CJS declarations: rename to .d.cts and change export default to export =
33+
const sourceFile = path.join(typesDir, 'index-cjs.d.ts')
34+
const targetFile = path.join(typesDir, 'index-cjs.d.cts')
35+
36+
if (fs.existsSync(sourceFile)) {
37+
let content = fs.readFileSync(sourceFile, 'utf8')
38+
// Transform 'export default' to 'export ='
39+
content = content.replace(/export default (_default);/, 'export = $1;')
40+
fs.writeFileSync(targetFile, content)
41+
console.log('Created CommonJS type declaration: dist/types/index-cjs.d.cts')
42+
}
43+
}
44+
}
45+
}
46+
}
847

948
const packageJson = JSON.parse(fs.readFileSync('./package.json'))
1049

@@ -96,7 +135,10 @@ export default [
96135
format: 'esm'
97136
}
98137
],
99-
plugins,
138+
plugins: [
139+
...plugins,
140+
fixDeclarations({ format: 'esm', declarationExt: '.d.mts' })
141+
],
100142
treeshake
101143
},
102144
{
@@ -111,7 +153,10 @@ export default [
111153
interop: 'compat'
112154
},
113155
],
114-
plugins,
156+
plugins: [
157+
...plugins,
158+
fixDeclarations({ format: 'cjs', declarationExt: '.d.cts' })
159+
],
115160
treeshake
116161
},
117162
{

packages/browser/scripts/fix-cjs-types.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)