Skip to content

Commit 7cec965

Browse files
committed
chore: Resolve relative module references
Adds a small rollup plugin to rename relative module references to 'posthog-js'. This is correct due to the fact that we use rollup-plugin-dts to combine all our type files by entrypoint, but it does not support relative modules.
1 parent d209241 commit 7cec965

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

packages/browser/rollup.config.mjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,33 @@ const entrypointTargets = entrypoints.map((file) => {
365365
}
366366
})
367367

368+
/**
369+
* rollup-plugin-dts doesn't resolve `declare module` augmentations that target
370+
* modules being inlined. Relative paths like `'../types'` become dangling in
371+
* the bundled output. This plugin repoints them to the package name so
372+
* TypeScript's module augmentation merging works correctly for consumers.
373+
*/
374+
function fixDtsModuleAugmentation() {
375+
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
376+
const pkgDir = path.resolve('.')
377+
378+
return {
379+
name: 'fix-dts-module-augmentation',
380+
generateBundle(options, bundle) {
381+
const outputDir = options.dir ? path.resolve(options.dir) : path.dirname(path.resolve(options.file))
382+
for (const chunk of Object.values(bundle)) {
383+
if (!chunk.code) continue
384+
const chunkPath = path.resolve(outputDir, chunk.fileName)
385+
const modulePath = `${pkg.name}/${path.relative(pkgDir, chunkPath).replace(/\.d\.ts$/, '')}`
386+
chunk.code = chunk.code.replace(
387+
/declare module ['"]\.\.?\/[^'"]+['"]/g,
388+
`declare module '${modulePath}'`
389+
)
390+
}
391+
},
392+
}
393+
}
394+
368395
const typeTargets = entrypoints
369396
.filter((file) => file.endsWith('.es.ts'))
370397
.map((file) => {
@@ -383,6 +410,7 @@ const typeTargets = entrypoints
383410
dts({
384411
exclude: [],
385412
}),
413+
fixDtsModuleAugmentation(),
386414
],
387415
}
388416
})

0 commit comments

Comments
 (0)