-
-
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
feat(define): don't modify string literals #9791
Conversation
@@ -37,4 +37,22 @@ describe('definePlugin', () => { | |||
'const isSSR = false;' | |||
) | |||
}) | |||
|
|||
test('ignores import\0.meta.env inside strings', async () => { |
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.
Vitest (or Vite) is replacing import.meta.env
with process.env
in unit tests, which coincidentally is the bug I'm trying to fix (so meta 🤣). I'm wondering if it's actually the same bug.
I inserted \0
in this test's strings to prevent that.
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.
This is such a deep level of programming irony. Got me laughing.
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 is because this PR doesn't strip literals in SSR here, which we may want to do as well. Otherwise we're using Vitest linked to local Vite, so theoretically this line should be fixed too.
Since we're already parsing in SSR dev (though I heard Anthony is experimenting something), another parse/tokenize here maybe isn't too bad, unless we could share a cache via this.parse
perhaps.
This fix is similar to #9621. But this using |
The difference with other places where we are using Another thought is if we should combine some of the plugins that depends on Let's add this one to be discussed with the team when we get the chance. |
The failures are unrelated to my changes, as they still occur with my changes removed. I just need to figure out the correct way to test these inputs.
We discussed this PR in today's meeting @tony19, and we think that we should redefine the Regarding the implementation, we discussed the performance implications and Evan had the idea to explore using Define from esbuild (directly in the esbuild plugin). So we should first look if that is a possible path forward (thus avoiding the tokenizer and regex scheme). |
@patak-dev Using define from esbuild would be a very good idea. Currently vite is not really consistent with esbuild behavior (although documentation says so). Vite generate definitions into a single file and use references as replacements, which means:
In my case, I use |
I don't know why, but with me, this bugs occurs only with debian/ubuntu, not macOS |
@patak-dev I tried using esbuild's vite/packages/vite/src/node/plugins/define.ts Lines 21 to 23 in 23c9259
This is the error I'm seeing:
I could workaround the error by removing those "invalid identifiers" from esbuild's config, and then just use our own string replacement mechanism (with |
I think we were using |
Description
The
define
andclient-inject
plugins indiscriminately replace strings within string literals, which can create invalid JavaScript syntax (e.g.,console.log("mode is process.env.NODE_ENV")
is transformed intoconsole.log("mode is "process.env.NODE_ENV""
). This PR strips the literals from the code string that the plugins scan, thereby leaving the literals unchanged, which is the same solution used in several other Vite plugins.fix #9790
fix #3304
fix #8663
fix #6295
fix #9829
Additional context
This fix is similar to #9621, which was closed due to a preference for a regex fix for performance.
I did experiment with tweaking the plugin's regex to exclude string literals, but it's not robust enough to cover multiline strings:
Plus, based on the description in #8054, it's impossible to handle this properly with regex.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).