chore: use relative imports with .ts

This commit is contained in:
Sainan 2025-08-25 06:50:48 +02:00
parent d7a93463c0
commit d0c7388a49
4 changed files with 41 additions and 11 deletions

2
package-lock.json generated
View File

@ -21,7 +21,7 @@
"mongoose": "^8.11.0",
"morgan": "^1.10.0",
"ncp": "^2.0.0",
"typescript": "^5.5",
"typescript": "^5.7",
"undici": "^7.10.0",
"warframe-public-export-plus": "^0.5.82",
"warframe-riven-info": "^0.1.2",

View File

@ -38,7 +38,7 @@
"mongoose": "^8.11.0",
"morgan": "^1.10.0",
"ncp": "^2.0.0",
"typescript": "^5.5",
"typescript": "^5.7",
"undici": "^7.10.0",
"warframe-public-export-plus": "^0.5.82",
"warframe-riven-info": "^0.1.2",

View File

@ -28,15 +28,44 @@ for (const file of files) {
continue;
}
const dir = path.dirname(file);
const fixedContent = content.replaceAll(/} from "([^"]+)";/g, (sub, importPath) => {
if (!importPath.startsWith("@/")) {
const fullImportPath = path.resolve(dir, importPath);
if (fs.existsSync(fullImportPath + ".ts")) {
const relative = path.relative(root, fullImportPath).replace(/\\/g, "/");
const fixedPath = "@/" + relative;
console.log(`${importPath} -> ${fixedPath}`);
return sub.split(importPath).join(fixedPath);
const fixedContent = content.replaceAll(/from "([^"]+)";/g, (sub, importPath) => {
if (importPath.startsWith("@/") || importPath.startsWith(".")) {
const base = importPath.startsWith("@/")
? path.join(root, importPath.slice(2))
: path.resolve(dir, importPath);
let target = base;
if (fs.existsSync(target)) {
const stat = fs.statSync(target);
if (stat.isDirectory()) {
if (fs.existsSync(path.join(target, "index.ts"))) {
target = path.join(target, "index.ts");
} else {
return sub;
}
} else {
const ext = path.extname(target);
if (!ext) {
target += ".ts";
}
}
} else if (fs.existsSync(target + ".ts")) {
target += ".ts";
} else if (fs.existsSync(path.join(target, "index.ts"))) {
target = path.join(target, "index.ts");
} else {
return sub;
}
let relative = path.relative(dir, target).replace(/\\/g, "/");
if (!path.extname(relative)) {
relative += ".ts";
}
if (!relative.startsWith(".")) {
relative = "./" + relative;
}
console.log(`${importPath} -> ${relative}`);
return sub.split(importPath).join(relative);
}
return sub;
});

View File

@ -35,7 +35,8 @@
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
"allowImportingTsExtensions": true,
"rewriteRelativeImportExtensions": true,
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */