-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixImports.ts
32 lines (26 loc) · 1.11 KB
/
fixImports.ts
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
26
27
28
29
30
31
32
// NOTE: This script adds '.js' to the end of imports in the final build ./dist folder
// ! The backend will not build without corrected import statements
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from 'url';
const currentDirectory = path.dirname(fileURLToPath(import.meta.url));
const replaceImports = (directory: string): void => {
const files = fs.readdirSync(directory);
files.forEach(file => {
const filePath = path.join(directory, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
replaceImports(filePath);
} else if (filePath.endsWith('.js')) {
let content = fs.readFileSync(filePath, 'utf8');
// * This regex checks for relative paths that start with './' or '../'
content = content.replace(/import\s+(.+?)\s+from\s+['"](\..+?)['"]/g, (match, p1, p2) => {
if (!p2.endsWith('.js')) return `import ${p1} from '${p2}.js'`;
return match;
});
fs.writeFileSync(filePath, content, 'utf8');
}
});
};
const directoryPath = path.join(currentDirectory, '../dist');
replaceImports(directoryPath);