Skip to content

Commit

Permalink
fix(resolve): preserve hash/search of file url (#19300)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Feb 3, 2025
1 parent e8c783f commit d1e1b24
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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)
})

describe('environment builtins', () => {
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 @@ -327,9 +327,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

0 comments on commit d1e1b24

Please sign in to comment.