-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: loader: typescript module resolver not resolving to source path …
…of symlinked module
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
|
||
/** | ||
* In case any directory in the path contains symlinks, this function attempts to detect that resolves the original path. | ||
* | ||
* Example Input: /Users/alessio/Documents/GitHub/payload-3.0-alpha-demo/node_modules/tailwindcss/resolveConfig.js | ||
* The "tailwindcss" in this example is a symlinked directory. | ||
* | ||
* Example Output: /Users/alessio/Documents/GitHub/payload-3.0-alpha-demo/node_modules/.pnpm/tailwindcss@3.4.3/node_modules/tailwindcss/resolveConfig.js | ||
*/ | ||
export async function resolveOriginalPath(filePath: string) { | ||
try { | ||
// Normalize and split the path | ||
const parts = path.resolve(filePath).split(path.sep) | ||
|
||
let currentPath = '/' | ||
// skip the first slash | ||
for (const part of parts.slice(1)) { | ||
currentPath = path.join(currentPath, part) | ||
|
||
// Check if the current path component is a symlink | ||
const stats = await fs.lstat(currentPath) | ||
if (stats.isSymbolicLink()) { | ||
// Resolve the symlink | ||
const resolvedLink = await fs.readlink(currentPath) | ||
currentPath = path.join(path.dirname(currentPath), resolvedLink) | ||
} | ||
} | ||
|
||
return currentPath | ||
} catch (error) { | ||
console.error('Error resolving path:', error) | ||
} | ||
} |