-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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(assetImportMetaUrl): allow ternary operator in template literal urls #13121
Conversation
Run & review this pull request in StackBlitz Codeflow. |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
).