fix(assetImportMetaUrl): allow ternary operator in template literal urls#13121
fix(assetImportMetaUrl): allow ternary operator in template literal urls#13121patak-cat merged 2 commits intovitejs:mainfrom
Conversation
|
|
| let bracketsStack = 0 | ||
| for (let i = 0; i < rawUrl.length; i++) { | ||
| if (rawUrl[i] === '{') { | ||
| bracketsStack++ | ||
| } else if (rawUrl[i] === '}') { | ||
| bracketsStack-- | ||
| } else if (rawUrl[i] === '?' && bracketsStack === 0) { | ||
| return i | ||
| } | ||
| } | ||
| return -1 |
There was a problem hiding this comment.
I think this may be better than a regex here. I was thinking we could check for ${ but this may be enough.
There was a problem hiding this comment.
Because we are parsing the url by this.parse later, I wonder if we can get the index during that. But given that this is a regression, I think it's fine to merge this for now.
BTW how does this work when a variable exists after ? (e.g. new URL(`./foo?bar=${bar}`, import.meta.url))?
Good point. This one would detect the |
Description
The recent fix to query parameters in template literals (#13034) broke some imports in our project which use ternary operators in template literal strings. This change keeps the fix to query parameters while allowing ternary operators by finding the first '?' character that is not in between curly brackets.
This was valid before:
But now breaks the build because a split is performed on the '?' in the ternary operator.
Additional context
We could simply move the ternary operator outside of the template literal, which fixes our issues. As the change in #13034 might affect others, this fix can prevent the need for code refactoring due to a regression.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).