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:
@@ -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__,}.*'`
|
||||
|
||||
@@ -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')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user