Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve drive relative path #9097

Merged
merged 2 commits into from
Jul 14, 2022
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
12 changes: 12 additions & 0 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '[success] absolute'
1 change: 1 addition & 0 deletions playground/resolve/drive-relative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '[success] drive relative'
7 changes: 7 additions & 0 deletions playground/resolve/index.html
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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