feat: better config __dirname support#8442
Merged
Merged
Conversation
sapphi-red
commented
Jun 2, 2022
|
|
||
| ::: tip NOTE | ||
| Vite will replace `__filename`, `__dirname`, and `import.meta.url` in config files and its deps. Using these as variable names or passing as a parameter to a function with string double quote (example `console.log`) will result in an error: | ||
| Vite will inject `__filename`, `__dirname` in config files and its deps. Declaring these variables at top level will result in an error: |
Member
Author
There was a problem hiding this comment.
In CJS, the following code fails, so it is not a special thing.
const __dirname = ''But here it also happens with ESM. This is the reason I left this note.
Also I didn't mention import.meta.url because __vite_injected_original_import_meta_url will rarely conflict.
patak-cat
approved these changes
Jun 2, 2022
bluwy
reviewed
Jun 5, 2022
bluwy
approved these changes
Jun 8, 2022
This was referenced Mar 25, 2026
This was referenced Apr 2, 2026
This was referenced Apr 18, 2026
This was referenced Apr 27, 2026
This was referenced May 4, 2026
This was referenced May 17, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR changes the way to implement
__dirname/__filename/import.meta.urlsupport in config files.Before this PR, those values were replaced. But now it is injected at each files.
This will avoid some strange errors.
I tested this with the files below.
refs: #7846 (comment)
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).