Skip to content

Commit

Permalink
fix: module graph ensureEntryFromUrl based on id (#9759)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Aug 20, 2022
1 parent 3a586e4 commit 01857af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
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

0 comments on commit 01857af

Please sign in to comment.