Skip to content

Commit 3d81250

Browse files
committed
feat: add preserveSymlinks option
1 parent aa1e5f9 commit 3d81250

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

.changeset/polite-dodos-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"resolve-sync": minor
3+
---
4+
5+
Add preserveSymlinks option.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ A partial filesystem interface used by the resolver. If running in node, and not
136136
- `readPkg(file: string): unknown` – reads and parses a JSON file (e.g., `package.json`).
137137
- `realpath?(file: string): string` – optionally resolves symlinks or returns the canonical path.
138138

139+
### `preserveSymlinks?: boolean`
140+
141+
For use with the `--preserve-symlinks` flag in Node.js, this option is `false` by default. If set to `true`, the resolver will return the symlinked path instead of resolving it to its real path.
142+
139143
## Examples
140144

141145
### Basic usage (in Node.js)

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ export interface ResolveOptions {
88
fields?: string[];
99
require?: boolean;
1010
browser?: boolean;
11-
external?: (id: string) => boolean;
1211
conditions?: string[];
12+
external?: (id: string) => boolean;
13+
preserveSymlinks?: boolean;
1314
fs?: {
1415
isFile(file: string): boolean;
1516
readPkg(file: string): unknown;
@@ -56,9 +57,9 @@ export function resolveSync(id: string, opts: ResolveOptions): string | false {
5657

5758
function toContext(opts: ResolveOptions): ResolveContext {
5859
const fs = opts.fs || defaultFS;
59-
const realpath = fs.realpath || identity;
60+
const realpath = (!opts.preserveSymlinks && fs.realpath) || identity;
6061
const root = toPosix(opts.root || "/");
61-
const from = toPosix(realpath(opts.from));
62+
const from = toPosix(opts.from);
6263
const fromDir = dirname(from);
6364
const browser = !!opts.browser;
6465
const require = !!opts.require;

0 commit comments

Comments
 (0)