next.js
2fdb231e - [turbopack] Avoid returning multiple requests when performing file extension substitution (#86520)

Commit
67 days ago
[turbopack] Avoid returning multiple requests when performing file extension substitution (#86520) If we modify user patterns with filename extension alternates, we should ensure we only produce one result. e.g. given ``` src/ foo.js foo.ts foo.json ``` If `fullySpecified=false` then `import foo` should produce `foo.js`, not `foo.js` and `foo.json` similarly if your tsconfig sets `"moduleResolution":"nodeNext"` and you import `foo.js` it should produce `foo.ts`, but not `[foo.ts, foo.js]`. The core problem is that we expand a user supplied pattern with alternates and then we need to only produce matches that 'correspond' to the original pattern. To accomplish this I implemented an 'undo stack' so that when we examine a match produced by `read_matches` we can transform the request key back into something that matches the original pattern. This neatly solves the issue of accidentally producing multiple matches with the same base filename. Consider the pattern `./foo.js`, we expand the pattern to `./foo.{ts,tsx,js}` and then produce two matches `[(./foo.ts, foo.ts), (./foo.js, foo.js)]` our undo stack will transform the `./foo.ts` key back into `./foo.js` and then the alternate merging logic will pick the first one for each key! The nice thing about the 'key' based approach is that it does the right thing for dynamic requests as well. Finally, the new 'undo stack' based approach is much more composable than the old 'sequence of conditions' based approach so now it is trivial to produce all matches in parallel.
Author
Parents
Loading