Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fully resolve type paths for auto-import declarations #1528

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import type { OnResolveResult, PartialMessage } from "esbuild";
import type { RouterMethod } from "h3";
import { globby } from "globby";
import {
lookupNodeModuleSubpath,
parseNodeModulePath,
resolvePath,
} from "mlly";
import { generateFSTree } from "./utils/tree";
import { getRollupConfig, RollupConfig } from "./rollup/config";
import { prettyPath, writeFile, isDirectory } from "./utils";
Expand Down Expand Up @@ -114,19 +119,49 @@

if (nitro.unimport) {
await nitro.unimport.init();
// TODO: fully resolve utils exported from `#imports`
autoImportExports = await nitro.unimport
.toExports(typesDir)
.then((r) => r.replace(/#internal\/nitro/g, runtimeDir));

const resolvedImportPathMap = new Map<string, string>();
const imports = await nitro.unimport
.getImports()
.then((r) => r.filter((i) => !i.type));

for (const i of imports) {
if (resolvedImportPathMap.has(i.from)) {
continue;
}
let path = resolveAlias(i.from, nitro.options.alias);
if (!isAbsolute(path)) {
const resolvedPath = await resolvePath(i.from, {
url: nitro.options.nodeModulesDirs,
}).catch(() => null);
if (resolvedPath) {
const { dir, name } = parseNodeModulePath(resolvedPath);
if (!dir || !name) {
path = resolvedPath;

Check warning on line 144 in src/build.ts

View check run for this annotation

Codecov / codecov/patch

src/build.ts#L144

Added line #L144 was not covered by tests
} else {
const subpath = await lookupNodeModuleSubpath(resolvedPath);
path = join(dir, name, subpath || "");
}
}
}
if (existsSync(path) && !isDirectory(path)) {
path = path.replace(/\.[a-z]+$/, "");

Check warning on line 152 in src/build.ts

View check run for this annotation

Codecov / codecov/patch

src/build.ts#L152

Added line #L152 was not covered by tests
}
if (isAbsolute(path)) {
path = relative(typesDir, path);
}
resolvedImportPathMap.set(i.from, path);
}

autoImportedTypes = [
(
await nitro.unimport.generateTypeDeclarations({
exportHelper: false,
resolvePath: (i) => {
if (i.from.startsWith("#internal/nitro")) {
return resolveAlias(i.from, nitro.options.alias);
}
return i.from;
},
resolvePath: (i) => resolvedImportPathMap.get(i.from) ?? i.from,
})
).trim(),
];
Expand Down