Skip to content

Commit 9ccf142

Browse files
Copilotsapphi-red
andauthored
fix: support multiline new URL(..., import.meta.url) expressions (#20644)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: sapphi-red <[email protected]>
1 parent 731d3e6 commit 9ccf142

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

packages/vite/src/node/plugins/assetImportMetaUrl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
5656
id: {
5757
exclude: [exactRegex(preloadHelperId), exactRegex(CLIENT_ENTRY)],
5858
},
59-
code: /new\s+URL.+import\.meta\.url/,
59+
code: /new\s+URL.+import\.meta\.url/s,
6060
},
6161
async handler(code, id) {
6262
let s: MagicString | undefined

playground/assets/__tests__/assets.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,16 @@ test("new URL(/* @vite-ignore */ 'non-existent', import.meta.url)", async () =>
671671
)
672672
})
673673

674+
test('new URL(..., import.meta.url) (multiline)', async () => {
675+
const assetMatch = isBuild
676+
? /\/foo\/bar\/assets\/asset-[-\w]{8}\.png/
677+
: '/foo/bar/nested/asset.png'
678+
679+
expect(await page.textContent('.import-meta-url-multiline')).toMatch(
680+
assetMatch,
681+
)
682+
})
683+
674684
test.runIf(isBuild)('manifest', async () => {
675685
const manifest = readManifest('foo')
676686
const entry = manifest['index.html']

playground/assets/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ <h2>new URL(`${dynamic}`, import.meta.url)</h2>
377377
<code class="dynamic-import-meta-url-all"></code>
378378
</p>
379379

380+
<h2>Multiline new URL(..., import.meta.url)</h2>
381+
<code class="import-meta-url-multiline"></code>
382+
380383
<h2>simple script tag import-expression</h2>
381384
<code class="import-expression"></code>
382385
<code class="obj-import-express"></code>
@@ -489,6 +492,7 @@ <h3>assets in template</h3>
489492
import './css/css-url.css'
490493
import './css/icons.css'
491494
import './css/manual-chunks.css'
495+
import { multilineUrl } from './multiline-import-meta-url.js'
492496

493497
text('.base', `import.meta.${``}env.BASE_URL: ${import.meta.env.BASE_URL}`)
494498

@@ -709,6 +713,8 @@ <h3>assets in template</h3>
709713
text(`.dynamic-import-meta-url-all`, metaUrl)
710714
}
711715

716+
text('.import-meta-url-multiline', multilineUrl)
717+
712718
function text(el, text) {
713719
document.querySelector(el).textContent = text
714720
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test for multiline expressions
2+
// This is a separate file to ensure the regex filter in assetImportMetaUrlPlugin
3+
// correctly detects and processes multiline expressions that span multiple lines.
4+
export const multilineUrl = new URL(
5+
'./nested/asset.png',
6+
7+
import.meta.url,
8+
)

0 commit comments

Comments
 (0)