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: module graph ensureEntryFromUrl based on id #9759

Merged
merged 1 commit into from Aug 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 18 additions & 6 deletions packages/vite/src/node/server/__tests__/moduleGraph.spec.ts
@@ -1,14 +1,10 @@
import { beforeEach, describe, expect, it } from 'vitest'
import { describe, expect, it } from 'vitest'
import { ModuleGraph } from '../moduleGraph'
let moduleGraph: ModuleGraph

describe('moduleGraph', () => {
describe('invalidateModule', () => {
beforeEach(() => {
moduleGraph = new ModuleGraph((id) => Promise.resolve({ id }))
})

it('removes an ssrError', async () => {
const moduleGraph = new ModuleGraph(async (url) => ({ id: url }))
const entryUrl = '/x.js'

const entryModule = await moduleGraph.ensureEntryFromUrl(entryUrl, false)
Expand All @@ -18,5 +14,21 @@ describe('moduleGraph', () => {
moduleGraph.invalidateModule(entryModule)
expect(entryModule.ssrError).toBe(null)
})

it('ensureEntryFromUrl should based on resolvedId', async () => {
const moduleGraph = new ModuleGraph(async (url) => {
if (url === '/xx.js') {
return { id: '/x.js' }
} else {
return { id: url }
}
})
const meta = { vite: 'test' }

const mod1 = await moduleGraph.ensureEntryFromUrl('/x.js', false)
mod1.meta = meta
const mod2 = await moduleGraph.ensureEntryFromUrl('/xx.js', false)
expect(mod2.meta).to.equal(meta)
})
})
})
7 changes: 6 additions & 1 deletion packages/vite/src/node/server/moduleGraph.ts
Expand Up @@ -185,7 +185,7 @@ export class ModuleGraph {
setIsSelfAccepting = true
): Promise<ModuleNode> {
const [url, resolvedId, meta] = await this.resolveUrl(rawUrl, ssr)
let mod = this.urlToModuleMap.get(url)
let mod = this.idToModuleMap.get(resolvedId)
if (!mod) {
mod = new ModuleNode(url, setIsSelfAccepting)
if (meta) mod.meta = meta
Expand All @@ -200,6 +200,11 @@ export class ModuleGraph {
}
fileMappedModules.add(mod)
}
// multiple urls can map to the same module and id, make sure we register
// the url to the existing module in that case
else if (!this.urlToModuleMap.has(url)) {
this.urlToModuleMap.set(url, mod)
}
return mod
}

Expand Down