-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix(html): support import.meta.env
define replacement without quotes
#13425
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1401d63
fix(html): import.meta.env replacement without quotes
sapphi-red 6f0fe03
fix: ignore non-string parsed values
sapphi-red dfc4a70
feat: add warning
sapphi-red 0c0eca9
fix: support literal by quote and template literal
sapphi-red 53cf4e5
feat: fallback
sapphi-red 233e2bf
refactor: use JSON.parse instead of extractStringLiteral
sapphi-red 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 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
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
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.
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.
Or maybe we should just support JSON.stringified strings?
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.
Yes, I think it is better we only support stringified object/strings. It isn't intuitive from a HTML-only perspective, but IMO it is better to be consistent between JS and HTML here.
I wonder if we really need to support
define
for HTML though, or we could remove this 958-964 and add a note in the docs if this is confusing for users 🤔In JS, it makes sense because we replace the whole
import.meta.env.VITE_STRING
But in HTML we have
%VITE_STRING%
. Supportingdefine
in HTML feels like formalizing that define forimport.meta.env.VITE_XXX
is not a replacement that happens before the regular env replacement, but a way to overwrite env variables in the config. If we would like this feature, maybe we should have a newenvOverrides
config option?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.
I use the
define
for HTML to have a document title from the first page load and reuse the same variable through my js code.If we don't support it, I would have to hard code the value in HTML and add the same value in a js constant.
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.
Non JSON.stringified strings break the code already here.
So I think it is fair to discard not stringified strings with a warning log maybe?
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.
I think this shows that supporting
define
in HTML isn't being used as intended though. We wanted to only add support for replacing env variables. And you are going through adefine
to override a non-existent env variable to be able to use this feature. I think it is better for you to use an inline plugin andtransformIndexHtml
.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.
I think we did for JS, it feels a bit of a stretch to me to also support it in HTML. But IIRC, there was a discussion about allowing
env
to be a config object to have these kind of features (env.declare
orenv.define
maybe)? Maybelooks intuitive to others. I'm fine if both you and @sapphi-red would like to move on with this PR, as we'll still need to do the breaking change to remove support for this.
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.
I think overloading
define
to extendimport.meta.env.*
is fine since it's not a common thing to do, but happy to discuss more about it if it's confusing. The HTML case is indeed a bit tricky but I think a fix like this could be good enough for now.EDIT: I didn't notice the PR is marked as a breaking change. I'm slightly leaning towards maybe not, depending on how safe we want to play it 😄
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.
I'm leaning towards applying this suggestion (#13425 (comment)).
We document the behavior as "Any properties in
import.meta.env
can be used in HTML files with a special%ENV_NAME%
syntax". I understood it to mean replacing%ENV_NAME%
with the value of the element inimport.meta.env
. So it's the value when executed by JS and not the value written in the source code. From that standpoint, we might say that this is not a breaking change.To be honest, a few month ago I didn't know that it works like that. I thought it's a simple replacement but if
import.meta.env.*
is defined it affectsimport.meta.env
too. (#12866 (comment), #13003)I think it's confusing that it does more than a simple replacement but I guess it's fine to have this behavior if we add a document about this behavior.
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.
What happens for
import.meta.env.*
that are numbers or booleans though. I think it makes sense to replace e.g.<p>%LEGACY%</p>
to<p>false</p>
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.
I was wondering whether it's confusing to allow that. But yeah, it would be better to have access to number/boolean
import.meta.env
s.