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): preserve hash/search of file url #19300

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
26 changes: 26 additions & 0 deletions packages/vite/src/node/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,24 @@ describe('file url', () => {
export default dep;
`
}
if (id === '\0virtual:test-dep/static-postfix') {
return `
import * as dep from ${JSON.stringify(fileUrl.href + '?query=test')};
export default dep;
`
}
if (id === '\0virtual:test-dep/non-static') {
return `
const dep = await import(/* @vite-ignore */ String(${JSON.stringify(fileUrl.href)}));
export default dep;
`
}
if (id === '\0virtual:test-dep/non-static-postfix') {
return `
const dep = await import(/* @vite-ignore */ String(${JSON.stringify(fileUrl.href + '?query=test')}));
export default dep;
`
}
},
},
],
Expand Down Expand Up @@ -114,6 +126,20 @@ describe('file url', () => {

const mod4 = await runner.import('virtual:test-dep/non-static')
expect(mod4.default).toBe(mod)

const mod5 = await runner.import(fileUrl.href + '?query=test')
expect(mod5).toEqual(mod)
expect(mod5).not.toBe(mod)

const mod6 = await runner.import('virtual:test-dep/static-postfix')
expect(mod6.default).toEqual(mod)
expect(mod6.default).not.toBe(mod)
expect(mod6.default).toBe(mod5)

const mod7 = await runner.import('virtual:test-dep/non-static-postfix')
expect(mod7.default).toEqual(mod)
expect(mod7.default).not.toBe(mod)
expect(mod7.default).toBe(mod5)
})

test('build', async () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,10 @@ export function resolvePlugin(
}
}

// file url as path
// file url to path with preserving hash/search
if (id.startsWith('file://')) {
id = fileURLToPath(id)
const { file, postfix } = splitFileAndPostfix(id)
id = fileURLToPath(file) + postfix
}

// drive relative fs paths (only windows)
Expand Down
Loading