@@ -5,6 +5,45 @@ import replace from '@rollup/plugin-replace'
55import terser from '@rollup/plugin-terser'
66import typescript from '@rollup/plugin-typescript'
77import 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 ( / f r o m ' ( \. \/ .+ ?) ' ; / 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 ( / e x p o r t d e f a u l t ( _ d e f a u l t ) ; / , '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
948const 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 {
0 commit comments