Improve TypeScript 4.7 NodeNext compatibility (#1721)

* update script to mass-update import/export statements

* rewrite imports on .d.ts files

* add idempotency comment
This commit is contained in:
Robin Malfait
2022-07-27 14:25:59 +02:00
committed by GitHub
parent 6ecd448c48
commit 5bfa3daac5
2 changed files with 10 additions and 1 deletions
+1
View File
@@ -46,6 +46,7 @@ wait
# Rewrite ESM imports 😤
$rewriteImports "$DST" '/**/*.js'
$rewriteImports "$DST" '/**/*.d.ts'
# Remove test related files
rm -rf `$resolver "$DST" '/**/*.{test,__mocks__,}.*'`
+9 -1
View File
@@ -8,7 +8,15 @@ console.time('Rewrote imports in')
fastGlob.sync([process.argv.slice(2).join('')]).forEach((file) => {
file = path.resolve(process.cwd(), file)
let content = fs.readFileSync(file, 'utf8')
let result = content.replace(/(import|export)([^"']*?)(["'])\.(.*?)\3;/g, '$1$2".$4.js";')
let result = content.replace(/(import|export)([^"']*?)(["'])\.(.*?)\3/g, (full, a, b, _, d) => {
// For idempotency reasons, if `.js` already exists, then we can skip this. This allows us to
// run this script over and over again without adding .js files every time.
if (d.endsWith('.js')) {
return full
}
return `${a}${b}'.${d}.js'`
})
if (result !== content) {
fs.writeFileSync(file, result, 'utf8')
}