Skip to content

Commit d4eca98

Browse files
sapphi-redbtea
andauthored
fix: keep ids for virtual modules as-is (#20808)
Co-authored-by: btea <[email protected]>
1 parent e670799 commit d4eca98

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

packages/vite/src/node/server/pluginContainer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
462462
}
463463

464464
if (id) {
465-
partial.id = isExternalUrl(id) ? id : normalizePath(id)
465+
partial.id = isExternalUrl(id) || id[0] === '\0' ? id : normalizePath(id)
466466
return partial as PartialResolvedId
467467
} else {
468468
return null

playground/resolve/__tests__/resolve.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ test('plugin resolved custom virtual file', async () => {
163163
expect(await page.textContent('.custom-virtual')).toMatch('[success]')
164164
})
165165

166+
test('virtual file with URL scheme should not be rewritten (#20803)', async () => {
167+
expect(await page.textContent('.virtual-url-scheme')).toMatch('[success]')
168+
})
169+
166170
test('resolve inline package', async () => {
167171
expect(await page.textContent('.inline-pkg')).toMatch('[success]')
168172
})

playground/resolve/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ <h2>Plugin resolved virtual file (#9036)</h2>
158158
<h2>Plugin resolved custom virtual file</h2>
159159
<p class="custom-virtual"></p>
160160

161+
<h2>Virtual file with URL scheme</h2>
162+
<p class="virtual-url-scheme"></p>
163+
161164
<h2>Inline package</h2>
162165
<p class="inline-pkg"></p>
163166

@@ -394,6 +397,9 @@ <h2>utf8-bom-package</h2>
394397
import { msg as customVirtualMsg } from '@custom-virtual-file'
395398
text('.custom-virtual', customVirtualMsg)
396399

400+
import { msg as virtualUrlSchemeMsg } from 'virtual-with-scheme'
401+
text('.virtual-url-scheme', virtualUrlSchemeMsg)
402+
397403
import { msg as inlineMsg } from './inline-package'
398404
text('.inline-pkg', inlineMsg)
399405

playground/resolve/vite.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const virtualId9036 = '\0' + virtualFile9036
1010

1111
const customVirtualFile = '@custom-virtual-file'
1212

13+
const virtualFileWithScheme = 'virtual-with-scheme'
14+
const virtualIdWithScheme = '\0https://example.com/virtual.js'
15+
1316
const generatedContentVirtualFile = '@generated-content-virtual-file'
1417
const generatedContentImports = [
1518
{
@@ -77,6 +80,19 @@ export default defineConfig({
7780
}
7881
},
7982
},
83+
{
84+
name: 'virtual-url-scheme-test',
85+
resolveId(id) {
86+
if (id === virtualFileWithScheme) {
87+
return virtualIdWithScheme
88+
}
89+
},
90+
load(id) {
91+
if (id === virtualIdWithScheme) {
92+
return `export const msg = "[success] from virtual file with URL scheme"`
93+
}
94+
},
95+
},
8096
{
8197
name: 'generated-content',
8298
resolveId(id) {

0 commit comments

Comments
 (0)