Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
id: {
exclude: [exactRegex(preloadHelperId), exactRegex(CLIENT_ENTRY)],
},
code: /new\s+URL.+import\.meta\.url/,
code: /new\s+URL.+import\.meta\.url/s,
},
async handler(code, id) {
let s: MagicString | undefined
Expand Down
10 changes: 10 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,16 @@ test("new URL(/* @vite-ignore */ 'non-existent', import.meta.url)", async () =>
)
})

test('new URL(..., import.meta.url) (multiline)', async () => {
const assetMatch = isBuild
? /\/foo\/bar\/assets\/asset-[-\w]{8}\.png/
: '/foo/bar/nested/asset.png'

expect(await page.textContent('.import-meta-url-multiline')).toMatch(
assetMatch,
)
})

test.runIf(isBuild)('manifest', async () => {
const manifest = readManifest('foo')
const entry = manifest['index.html']
Expand Down
6 changes: 6 additions & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ <h2>new URL(`${dynamic}`, import.meta.url)</h2>
<code class="dynamic-import-meta-url-all"></code>
</p>

<h2>Multiline new URL(..., import.meta.url)</h2>
<code class="import-meta-url-multiline"></code>

<h2>simple script tag import-expression</h2>
<code class="import-expression"></code>
<code class="obj-import-express"></code>
Expand Down Expand Up @@ -489,6 +492,7 @@ <h3>assets in template</h3>
import './css/css-url.css'
import './css/icons.css'
import './css/manual-chunks.css'
import { multilineUrl } from './multiline-import-meta-url.js'

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

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

text('.import-meta-url-multiline', multilineUrl)

function text(el, text) {
document.querySelector(el).textContent = text
}
Expand Down
8 changes: 8 additions & 0 deletions playground/assets/multiline-import-meta-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Test for multiline expressions
// This is a separate file to ensure the regex filter in assetImportMetaUrlPlugin
// correctly detects and processes multiline expressions that span multiple lines.
export const multilineUrl = new URL(
'./nested/asset.png',

import.meta.url,
)
Loading