Skip to content

Commit 17ffe48

Browse files
committed
update module resolution
1 parent 70aa9a7 commit 17ffe48

File tree

1 file changed

+21
-39
lines changed

1 file changed

+21
-39
lines changed

scripts/validate-platform-isolation-ts.js

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ const { getValidPlatforms, extractPlatformsFromFile } = require('./platform-util
4646

4747
const WORKSPACE_ROOT = path.join(__dirname, '..');
4848

49+
// Load tsconfig to get module resolution settings
50+
const tsconfigPath = path.join(WORKSPACE_ROOT, 'tsconfig.json');
51+
const tsconfigContent = fs.readFileSync(tsconfigPath, 'utf-8');
52+
const tsconfig = ts.parseConfigFileTextToJson(tsconfigPath, tsconfigContent).config;
53+
const compilerOptions = ts.convertCompilerOptionsFromJson(
54+
tsconfig.compilerOptions,
55+
WORKSPACE_ROOT
56+
).options;
57+
4958
// Load configuration
5059
const configPath = path.join(WORKSPACE_ROOT, '.platform-isolation.config.js');
5160
const config = fs.existsSync(configPath)
@@ -235,55 +244,28 @@ function extractImports(filePath) {
235244
}
236245

237246
/**
238-
* Resolve import path relative to current file
247+
* Resolve import path relative to current file using TypeScript's module resolution
239248
*/
240249
function resolveImportPath(importPath, currentFilePath) {
241250
// External imports (node_modules) - return as-is
242251
if (!importPath.startsWith('.') && !importPath.startsWith('/')) {
243252
return { isExternal: true, resolved: importPath };
244253
}
245254

246-
const currentDir = path.dirname(currentFilePath);
247-
let resolved = path.resolve(currentDir, importPath);
248-
249-
// Check if it's a directory - if so, look for index file
250-
if (fs.existsSync(resolved) && fs.statSync(resolved).isDirectory()) {
251-
const extensions = ['.ts', '.js', '.tsx', '.jsx'];
252-
for (const ext of extensions) {
253-
const indexFile = path.join(resolved, `index${ext}`);
254-
if (fs.existsSync(indexFile)) {
255-
return { isExternal: false, resolved: indexFile };
256-
}
257-
}
258-
// Directory exists but no index file found
259-
return { isExternal: false, resolved };
260-
}
261-
262-
// Check if file exists as-is (with extension already)
263-
if (fs.existsSync(resolved)) {
264-
return { isExternal: false, resolved };
265-
}
266-
267-
// Try different extensions
268-
const extensions = ['.ts', '.js', '.tsx', '.jsx'];
269-
for (const ext of extensions) {
270-
const withExt = resolved + ext;
271-
if (fs.existsSync(withExt)) {
272-
return { isExternal: false, resolved: withExt };
273-
}
274-
}
255+
// Use TypeScript's module resolution with settings from tsconfig
256+
const result = ts.resolveModuleName(
257+
importPath,
258+
currentFilePath,
259+
compilerOptions,
260+
ts.sys
261+
);
275262

276-
// Try index files (for cases where the directory doesn't exist yet)
277-
for (const ext of extensions) {
278-
const indexFile = path.join(resolved, `index${ext}`);
279-
if (fs.existsSync(indexFile)) {
280-
return { isExternal: false, resolved: indexFile };
281-
}
263+
if (result.resolvedModule) {
264+
return { isExternal: false, resolved: result.resolvedModule.resolvedFileName };
282265
}
283266

284-
// Return the resolved path even if it doesn't exist
285-
// (getSupportedPlatforms will handle it)
286-
return { isExternal: false, resolved };
267+
// If TypeScript can't resolve, throw an error
268+
throw new Error(`Cannot resolve import "${importPath}" from ${path.relative(WORKSPACE_ROOT, currentFilePath)}`);
287269
}
288270

289271
/**

0 commit comments

Comments
 (0)