Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: prevent null pathname error (#9188)
Co-authored-by: Christoph Werner <codepunkt@users.noreply.github.com>
  • Loading branch information
sapphi-red and codepunkt committed Jul 18, 2022
1 parent 31d3b70 commit d66ffd0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/vite/src/node/server/moduleGraph.ts
@@ -1,5 +1,4 @@
import { extname } from 'node:path'
import { parse as parseUrl } from 'node:url'
import type { ModuleInfo, PartialResolvedId } from 'rollup'
import { isDirectCSSRequest } from '../plugins/css'
import {
Expand Down Expand Up @@ -237,10 +236,16 @@ export class ModuleGraph {
url = removeImportQuery(removeTimestampQuery(url))
const resolved = await this.resolveId(url, !!ssr)
const resolvedId = resolved?.id || url
const ext = extname(cleanUrl(resolvedId))
const { pathname, search, hash } = parseUrl(url)
if (ext && !pathname!.endsWith(ext)) {
url = pathname + ext + (search || '') + (hash || '')
if (
url !== resolvedId &&
!url.includes('\0') &&
!url.startsWith(`virtual:`)
) {
const ext = extname(cleanUrl(resolvedId))
const { pathname, search, hash } = new URL(url, 'relative://')
if (ext && !pathname!.endsWith(ext)) {
url = pathname + ext + search + hash
}
}
return [url, resolvedId, resolved?.meta]
}
Expand Down
6 changes: 6 additions & 0 deletions playground/resolve/index.html
Expand Up @@ -76,6 +76,9 @@ <h2>Monorepo linked dep</h2>
<h2>Plugin resolved virtual file</h2>
<p class="virtual"></p>

<h2>Plugin resolved virtual file (#9036)</h2>
<p class="virtual-9036"></p>

<h2>Plugin resolved custom virtual file</h2>
<p class="custom-virtual"></p>

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

import { msg as virtualMsg9036 } from 'virtual:file-9036.js'
text('.virtual-9036', virtualMsg9036)

import { msg as customVirtualMsg } from '@custom-virtual-file'
text('.custom-virtual', customVirtualMsg)

Expand Down
16 changes: 16 additions & 0 deletions playground/resolve/vite.config.js
Expand Up @@ -4,6 +4,9 @@ const { normalizePath } = require('vite')
const virtualFile = '@virtual-file'
const virtualId = '\0' + virtualFile

const virtualFile9036 = 'virtual:file-9036.js'
const virtualId9036 = '\0' + virtualFile9036

const customVirtualFile = '@custom-virtual-file'
const { a } = require('./config-dep')

Expand Down Expand Up @@ -44,6 +47,19 @@ module.exports = {
}
}
},
{
name: 'virtual-module-9036',
resolveId(id) {
if (id === virtualFile9036) {
return virtualId9036
}
},
load(id) {
if (id === virtualId9036) {
return `export const msg = "[success] from virtual file #9036"`
}
}
},
{
name: 'custom-resolve',
resolveId(id) {
Expand Down

0 comments on commit d66ffd0

Please sign in to comment.