Skip to content

Commit a1cbb75

Browse files
use resolver
1 parent 72b4a22 commit a1cbb75

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

src/ssr-plugin-utils.ts

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ import type {
1717
Span,
1818
VariableDeclarator,
1919
} from "@oxc-project/types";
20+
import { ResolverFactory } from "oxc-resolver";
2021
import type { BrowserCommandContext } from "vitest/node";
2122

23+
const resolver = new ResolverFactory({
24+
extensions: [".tsx", ".ts", ".jsx", ".js"],
25+
});
26+
2227
// Type guards for better type safety
2328
export function isFunction(node: Node): node is OxcFunction {
2429
const functionTypes: FunctionType[] = [
@@ -158,31 +163,6 @@ export async function hasRenderSSRCall(
158163
}
159164
}
160165

161-
export function resolveComponentPath(
162-
importPath: string,
163-
testFileId: string,
164-
): string {
165-
if (!importPath.startsWith(".")) {
166-
// Absolute import, add extension if needed
167-
return importPath.endsWith(".tsx") || importPath.endsWith(".ts")
168-
? importPath
169-
: `${importPath}.tsx`;
170-
}
171-
172-
// Relative import - resolve relative to test file
173-
const testFileDir = dirname(testFileId);
174-
const resolvedPath = resolve(testFileDir, importPath);
175-
const projectRoot = process.cwd();
176-
let componentPath = `./${relative(projectRoot, resolvedPath)}`;
177-
178-
// Add extension if needed
179-
if (!componentPath.endsWith(".tsx") && !componentPath.endsWith(".ts")) {
180-
componentPath += ".tsx";
181-
}
182-
183-
return componentPath;
184-
}
185-
186166
export function extractPropsFromJSX(
187167
attributes: JSXAttributeItem[],
188168
sourceCode: string,
@@ -219,6 +199,54 @@ export function isTestFile(id: string): boolean {
219199
return id.includes(".test.") || id.includes(".spec.");
220200
}
221201

202+
function fallbackResolveComponentPath(
203+
importPath: string,
204+
testFileId: string,
205+
): string {
206+
if (!importPath.startsWith(".")) {
207+
// Absolute import, add extension if needed
208+
return importPath.endsWith(".tsx") || importPath.endsWith(".ts")
209+
? importPath
210+
: `${importPath}.tsx`;
211+
}
212+
213+
// Relative import - resolve relative to test file
214+
const testFileDir = dirname(testFileId);
215+
const resolvedPath = resolve(testFileDir, importPath);
216+
const projectRoot = process.cwd();
217+
let componentPath = `./${relative(projectRoot, resolvedPath)}`;
218+
219+
// Add extension if needed
220+
if (!componentPath.endsWith(".tsx") && !componentPath.endsWith(".ts")) {
221+
componentPath += ".tsx";
222+
}
223+
224+
return componentPath;
225+
}
226+
227+
export function resolveComponentPath(
228+
importPath: string,
229+
testFileId: string,
230+
): string {
231+
const testFileDir = dirname(testFileId);
232+
const result = resolver.sync(testFileDir, importPath);
233+
234+
if (result.error || !result.path) {
235+
const errorMsg = result.error || "No path resolved";
236+
237+
console.warn(
238+
`[oxc-resolver] Could not resolve "${importPath}" from "${testFileId}": ${errorMsg}. Using fallback resolution. If this is not a test file, this might be a bug.`,
239+
);
240+
241+
return fallbackResolveComponentPath(importPath, testFileId);
242+
}
243+
244+
const projectRoot = process.cwd();
245+
const relativePath = relative(projectRoot, result.path);
246+
247+
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
248+
}
249+
222250
export function hasCommandsImport(node: Node): boolean {
223251
if (!isImportDeclaration(node)) return false;
224252

@@ -233,7 +261,6 @@ export function hasCommandsImport(node: Node): boolean {
233261
);
234262
}
235263

236-
// Shared SSR rendering logic
237264
export async function renderComponentToSSR(
238265
ctx: BrowserCommandContext,
239266
Component: Component,

0 commit comments

Comments
 (0)