-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Support Vite Environment API #18970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thecrypticace
wants to merge
4
commits into
main
Choose a base branch
from
fix/vite-env-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+119
−24
Open
Support Vite Environment API #18970
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Environment-aware resolver wiring via
createIdResolverlooks incorrect and may not match Vite’s API.In
createRoot’s env-aware branch you do:But Vite’s
createIdResolvercurrently returns aResolveIdFn-style function whose signature is(id, importer?, aliasOnly?, ssr?), i.e. it does not take anEnvironmentargument first. Passingenvas the first parameter here is very likely to both:vited.ts.envas theid, etc.), causing incorrect resolution or hard-to-debug errors.The legacy
!envpath correctly adaptsconfig!.createResolver:The env-aware branch should mirror this style, with environment-specific behavior coming from
env.config(and potentially from environment-specificresolveoptions), not from an extraenvparameter to the returned function. For example:This keeps the adapter shape uniform between legacy and env-aware paths and avoids depending on an undocumented resolver signature.
Please double‑check the current
vitetypings / implementation forcreateIdResolveragainst your pinned Vite version and ensure the adapter here matches the actualResolveIdFnsignature. If desired, you can verify with a minimal TS snippet compiling against that version ofvite:Also applies to: 32-32, 38-60, 62-75, 77-85
🏁 Script executed:
Length of output: 99
🏁 Script executed:
Length of output: 4634
🌐 Web query:
💡 Result:
Signature (TypeScript):
createIdResolver(config: ResolvedConfig, options?: Partial) => ResolveIdFn
Parameters:
Return:
Sources:
[1] Vite API docs (createResolver / createIdResolver).
[2] vite package API listing (jsdocs.io).
💡 Result:
The ResolveIdFn type (internal Vite resolver) has the following TypeScript signature:
(this: PluginContext, source: string, importer?: string, options?: {
attributes?: Record<string,string>;
custom?: CustomPluginOptions;
skip?: Set;
skipCalls?: readonly SkipInformation[];
ssr?: boolean;
isEntry?: boolean;
}) => Promise | ResolveIdResult. [1][2]
Sources:
💡 Result:
createIdResolver(...) returns a Resolve function with signature roughly:
(id: string, importer?: string, aliasOnly?: boolean, ssr?: boolean) => Promise<string | undefined>.
Sources: jsdocs listing of Vite types (createIdResolver / ResolveFn) [1], Vite source examples using ResolveIdFn / ResolveFn [2].
Environment-aware resolver wiring passes
envas wrong parameter, breaking resolver function contract.In
createRoot's env-aware branch (lines 72–73), you pass the environment as the first argument to the resolver:However, the function returned by
createIdResolverhas signature(id: string, importer?: string, aliasOnly?: boolean, ssr?: boolean). Passingenvas the first parameter treats it as theid, shifting all subsequent arguments—causing incorrect resolution and potential runtime errors.The legacy branch (line 58) correctly aligns arguments:
Since you already pass
env.configtocreateIdResolveritself, environment-specific behavior is already encoded in the returned resolver. Remove theenvparameter from the adapter calls:🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've got the types wrong. The types for
config.createResolverandvite.createIdResolverdiffer.The function
vite.createIdResolverreturns aResolveIdFn:The type declaration for
ResolveIdFn:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thecrypticace, thank you for the correction! You're absolutely right. I apologize for the incorrect analysis.
The new Vite Environment API's
createIdResolverdoes indeed return aResolveIdFnthat takesenvironmentas the first parameter:So the code at lines 72-73 is correct as written:
This properly passes the environment as the first argument to match the new API signature. The implementation correctly handles both the legacy
config.createResolverAPI (which doesn't take an environment parameter) and the newervite.createIdResolverAPI (which does).✏️ Learnings added