Skip to content

Commit d66ffd0

Browse files
sapphi-redcodepunkt
andauthoredJul 18, 2022
fix: prevent null pathname error (#9188)
Co-authored-by: Christoph Werner <codepunkt@users.noreply.github.com>
1 parent 31d3b70 commit d66ffd0

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed
 

‎packages/vite/src/node/server/moduleGraph.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { extname } from 'node:path'
2-
import { parse as parseUrl } from 'node:url'
32
import type { ModuleInfo, PartialResolvedId } from 'rollup'
43
import { isDirectCSSRequest } from '../plugins/css'
54
import {
@@ -237,10 +236,16 @@ export class ModuleGraph {
237236
url = removeImportQuery(removeTimestampQuery(url))
238237
const resolved = await this.resolveId(url, !!ssr)
239238
const resolvedId = resolved?.id || url
240-
const ext = extname(cleanUrl(resolvedId))
241-
const { pathname, search, hash } = parseUrl(url)
242-
if (ext && !pathname!.endsWith(ext)) {
243-
url = pathname + ext + (search || '') + (hash || '')
239+
if (
240+
url !== resolvedId &&
241+
!url.includes('\0') &&
242+
!url.startsWith(`virtual:`)
243+
) {
244+
const ext = extname(cleanUrl(resolvedId))
245+
const { pathname, search, hash } = new URL(url, 'relative://')
246+
if (ext && !pathname!.endsWith(ext)) {
247+
url = pathname + ext + search + hash
248+
}
244249
}
245250
return [url, resolvedId, resolved?.meta]
246251
}

‎playground/resolve/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ <h2>Monorepo linked dep</h2>
7676
<h2>Plugin resolved virtual file</h2>
7777
<p class="virtual"></p>
7878

79+
<h2>Plugin resolved virtual file (#9036)</h2>
80+
<p class="virtual-9036"></p>
81+
7982
<h2>Plugin resolved custom virtual file</h2>
8083
<p class="custom-virtual"></p>
8184

@@ -221,6 +224,9 @@ <h2>resolve package that contains # in path</h2>
221224
import { msg as virtualMsg } from '@virtual-file'
222225
text('.virtual', virtualMsg)
223226

227+
import { msg as virtualMsg9036 } from 'virtual:file-9036.js'
228+
text('.virtual-9036', virtualMsg9036)
229+
224230
import { msg as customVirtualMsg } from '@custom-virtual-file'
225231
text('.custom-virtual', customVirtualMsg)
226232

‎playground/resolve/vite.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const { normalizePath } = require('vite')
44
const virtualFile = '@virtual-file'
55
const virtualId = '\0' + virtualFile
66

7+
const virtualFile9036 = 'virtual:file-9036.js'
8+
const virtualId9036 = '\0' + virtualFile9036
9+
710
const customVirtualFile = '@custom-virtual-file'
811
const { a } = require('./config-dep')
912

@@ -44,6 +47,19 @@ module.exports = {
4447
}
4548
}
4649
},
50+
{
51+
name: 'virtual-module-9036',
52+
resolveId(id) {
53+
if (id === virtualFile9036) {
54+
return virtualId9036
55+
}
56+
},
57+
load(id) {
58+
if (id === virtualId9036) {
59+
return `export const msg = "[success] from virtual file #9036"`
60+
}
61+
}
62+
},
4763
{
4864
name: 'custom-resolve',
4965
resolveId(id) {

0 commit comments

Comments
 (0)
Please sign in to comment.