Skip to content
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

Merged
merged 2 commits into from May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 22 additions & 5 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -56,11 +56,14 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {

// potential dynamic template string
if (rawUrl[0] === '`' && rawUrl.includes('${')) {
let [pureUrl, queryString = ''] = rawUrl.split('?')
if (queryString) {
pureUrl += '`'
queryString = '?' + queryString.slice(0, -1)
}
const queryDelimiterIndex = getQueryDelimiterIndex(rawUrl)
const hasQueryDelimiter = queryDelimiterIndex !== -1
const pureUrl = hasQueryDelimiter
? rawUrl.slice(0, queryDelimiterIndex) + '`'
: rawUrl
const queryString = hasQueryDelimiter
? rawUrl.slice(queryDelimiterIndex, -1)
: ''
const ast = this.parse(pureUrl)
const templateLiteral = (ast as any).body[0].expression
if (templateLiteral.expressions.length) {
Expand Down Expand Up @@ -167,3 +170,17 @@ function buildGlobPattern(ast: any) {
}
return pattern
}

function getQueryDelimiterIndex(rawUrl: string): number {
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
Comment on lines +175 to +185
Copy link
Member

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.

}
11 changes: 11 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Expand Up @@ -335,6 +335,17 @@ test('new URL(`./${dynamic}?abc`, import.meta.url)', async () => {
)
})

test('new URL(`./${1 === 0 ? static : dynamic}?abc`, import.meta.url)', async () => {
expect(await page.textContent('.dynamic-import-meta-url-1-ternary')).toMatch(
isBuild ? 'data:image/png;base64' : '/foo/nested/icon.png?abc',
)
expect(await page.textContent('.dynamic-import-meta-url-2-ternary')).toMatch(
isBuild
? /\/foo\/assets\/asset-\w{8}\.png\?abc/
: '/foo/nested/asset.png?abc',
)
})

test('new URL(`non-existent`, import.meta.url)', async () => {
expect(await page.textContent('.non-existent-import-meta-url')).toMatch(
new URL('non-existent', page.url()).pathname,
Expand Down
21 changes: 21 additions & 0 deletions playground/assets/index.html
Expand Up @@ -231,6 +231,16 @@ <h2>new URL(`./${dynamic}?abc`, import.meta.url)</h2>
<code class="dynamic-import-meta-url-2-query"></code>
</p>

<h2>new URL(`./${1 === 0 ? static : dynamic}?abc`, import.meta.url)</h2>
<p>
<img class="dynamic-import-meta-url-img-1-ternary" />
<code class="dynamic-import-meta-url-1-ternary"></code>
</p>
<p>
<img class="dynamic-import-meta-url-img-2-ternary" />
<code class="dynamic-import-meta-url-2-ternary"></code>
</p>

<h2>new URL(`non-existent`, import.meta.url)</h2>
<p>
<code class="non-existent-import-meta-url"></code>
Expand Down Expand Up @@ -453,6 +463,17 @@ <h3>assets in noscript</h3>
testDynamicImportMetaUrlWithQuery('icon', 1)
testDynamicImportMetaUrlWithQuery('asset', 2)

function testDynamicImportMetaUrlWithTernaryOperator(name, i) {
// prettier-ignore
const metaUrl = new URL(`./nested/${1 === 0 ? 'failed' : name}.png?abc`, import.meta.url,)
text(`.dynamic-import-meta-url-${i}-ternary`, metaUrl)
document.querySelector(`.dynamic-import-meta-url-img-${i}-ternary`).src =
metaUrl
}

testDynamicImportMetaUrlWithTernaryOperator('icon', 1)
testDynamicImportMetaUrlWithTernaryOperator('asset', 2)

{
const name = 'test'
const js = new URL(`./nested/${name}.js`, import.meta.url).href
Expand Down