Skip to content

Commit

Permalink
fix: add explicit file extensions to annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett committed Jan 19, 2025
1 parent efd21a0 commit 117d097
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
"three": ">=0.128.0"
},
"scripts": {
"build": "rimraf dist && vite build && tsc --emitDeclarationOnly && copyfiles -u 1 \"src/**/*.d.ts\" dist && copyfiles package.json README.md LICENSE dist && json -I -f dist/package.json -e \"this.private=undefined;this.type=\\\"module\\\";\" && npm run patch-cts",
"patch-cts": "node -e \"require('shelljs').find('dist/**/*.d.ts').forEach(f=>{var f2=f.replace(/\\.ts$/,'.cts');require('fs').copyFileSync(f,f2);require('shelljs').sed('-i',/ from '(\\.[^']+)';$/,' from \\'\\$1.cts\\';',f2);require('shelljs').sed('-i',/^declare module '(\\.[^']+)'/,'declare module \\'\\$1.cts\\'',f2)})\"",
"build": "rimraf dist && vite build && tsc --emitDeclarationOnly && copyfiles -u 1 \"src/**/*.d.ts\" dist && copyfiles package.json README.md LICENSE dist && json -I -f dist/package.json -e \"this.private=undefined;this.type=\\\"module\\\";\" && node patch-ts.js",
"lint": "tsc --noEmit"
}
}
23 changes: 23 additions & 0 deletions patch-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const shell = require('shelljs')
const fs = require('fs')

const files = shell.find('dist/**/*.d.ts')

for (const file of files) {
const cts = file.replace('.d.ts', '.d.cts')

// Add type qualifiers in annotations
shell.sed('-i', /(import|export [*{])\s+(?!type\b)/, '$1 type ', file)
// Remove .js extensions
shell.sed('-i', /from '(\.[^.]+)\.js'/, "from '$1'", file)

fs.copyFileSync(file, cts)

// Add explicit .d.ts extensions for ESM
shell.sed('-i', / from '(\.[^']+)'/, " from '$1.d.ts'", file)
shell.sed('-i', /^declare module '(\.[^']+)'/, "declare module '$1.d.ts'", file)

// Add explicit .d.cts extensions for CJS
shell.sed('-i', / from '(\.[^']+)'/, " from '$1.d.cts'", cts)
shell.sed('-i', /^declare module '(\.[^']+)'/, "declare module '$1.d.cts'", cts)
}

0 comments on commit 117d097

Please sign in to comment.