Skip to content

Commit

Permalink
fix: resolve drive relative path (#9097)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jul 14, 2022
1 parent 60fa6ba commit b393451
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -28,6 +28,7 @@ import {
isObject,
isPossibleTsOutput,
isTsRequest,
isWindows,
nestedResolveFrom,
normalizePath,
resolveFrom,
Expand Down Expand Up @@ -243,6 +244,17 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
}
}

// drive relative fs paths (only windows)
if (isWindows && id.startsWith('/')) {
const basedir = importer ? path.dirname(importer) : process.cwd()
const fsPath = path.resolve(basedir, id)
if ((res = tryFsResolve(fsPath, options))) {
isDebug &&
debug(`[drive-relative] ${colors.cyan(id)} -> ${colors.dim(res)}`)
return res
}
}

// absolute fs paths
if (
isNonDriveRelativeAbsolutePath(id) &&
Expand Down
10 changes: 9 additions & 1 deletion playground/resolve/__tests__/resolve.spec.ts
@@ -1,4 +1,4 @@
import { isBuild, page } from '~utils'
import { isBuild, isWindows, page } from '~utils'

test('bom import', async () => {
expect(await page.textContent('.utf8-bom')).toMatch('[success]')
Expand Down Expand Up @@ -75,6 +75,14 @@ test('filename with dot', async () => {
expect(await page.textContent('.dot')).toMatch('[success]')
})

test.runIf(isWindows)('drive-relative path', async () => {
expect(await page.textContent('.drive-relative')).toMatch('[success]')
})

test('absolute path', async () => {
expect(await page.textContent('.absolute')).toMatch('[success]')
})

test('browser field', async () => {
expect(await page.textContent('.browser')).toMatch('[success]')
})
Expand Down
1 change: 1 addition & 0 deletions playground/resolve/absolute.js
@@ -0,0 +1 @@
export default '[success] absolute'
1 change: 1 addition & 0 deletions playground/resolve/drive-relative.js
@@ -0,0 +1 @@
export default '[success] drive relative'
7 changes: 7 additions & 0 deletions playground/resolve/index.html
Expand Up @@ -55,6 +55,12 @@ <h2>
<h2>Resolve file name containing dot</h2>
<p class="dot">fail</p>

<h2>Resolve drive-relative path (Windows only)</h2>
<p class="drive-relative">fail</p>

<h2>Resolve absolute path</h2>
<p class="absolute">fail</p>

<h2>Browser Field</h2>
<p class="browser">fail</p>

Expand Down Expand Up @@ -89,6 +95,7 @@ <h2>resolve package that contains # in path</h2>
<p class="path-contains-sharp-symbol"></p>

<script type="module">
import '@generated-content-virtual-file'
function text(selector, text) {
document.querySelector(selector).textContent = text
}
Expand Down
43 changes: 43 additions & 0 deletions playground/resolve/vite.config.js
@@ -1,9 +1,26 @@
const path = require('node:path')
const { normalizePath } = require('vite')

const virtualFile = '@virtual-file'
const virtualId = '\0' + virtualFile

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

const generatedContentVirtualFile = '@generated-content-virtual-file'
const generatedContentImports = [
{
specifier: normalizePath(
path.resolve(__dirname, './drive-relative.js').replace(/^[a-zA-Z]:/, '')
),
elementQuery: '.drive-relative'
},
{
specifier: normalizePath(path.resolve(__dirname, './absolute.js')),
elementQuery: '.absolute'
}
]

module.exports = {
resolve: {
extensions: ['.mjs', '.js', '.es', '.ts'],
Expand Down Expand Up @@ -39,6 +56,32 @@ module.exports = {
return `export const msg = "[success] from custom virtual file"`
}
}
},
{
name: 'generated-content',
resolveId(id) {
if (id === generatedContentVirtualFile) {
return id
}
},
load(id) {
if (id === generatedContentVirtualFile) {
const tests = generatedContentImports
.map(
({ specifier, elementQuery }, i) =>
`import content${i} from ${JSON.stringify(specifier)}\n` +
`text(${JSON.stringify(elementQuery)}, content${i})`
)
.join('\n')

return (
'function text(selector, text) {\n' +
' document.querySelector(selector).textContent = text\n' +
'}\n\n' +
tests
)
}
}
}
],
optimizeDeps: {
Expand Down

0 comments on commit b393451

Please sign in to comment.