-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix-imports.ps1
More file actions
25 lines (19 loc) · 1.05 KB
/
Copy pathfix-imports.ps1
File metadata and controls
25 lines (19 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Fix ES module imports by adding .js extensions
$files = Get-ChildItem -Path "_src" -Filter "*.ts" -Recurse | Where-Object { $_.Name -notlike "*.d.ts" }
foreach ($file in $files) {
$content = Get-Content $file.FullName -Raw
$originalContent = $content
# Fix relative imports that don't end in .js
# Pattern 1: from "./something" -> from "./something.js"
$content = $content -replace 'from\s+([''"])(\./[^''"]+?)(?<!\.js)(\1)', 'from $1$2.js$3'
# Pattern 2: from "../something" -> from "../something.js"
$content = $content -replace 'from\s+([''"])(\.\.?/[^''"]+?)(?<!\.js)(\1)', 'from $1$2.js$3'
# Fix directory imports to include /index.js
$content = $content -replace 'from\s+([''"])(\.\.?/\w+(?:/\w+)*)/index\.js\.js(\1)', 'from $1$2/index.js$3'
$content = $content -replace '(from\s+[''"]\.\.?/[^''"]+?)\.js\.js([''"])', '$1.js$2'
if ($content -ne $originalContent) {
Set-Content $file.FullName -Value $content -NoNewline
Write-Host "Fixed: $($file.FullName)"
}
}
Write-Host "Done!"