Skip to content

Commit b178c90

Browse files
authored
fix: skip the plugin if it has been called before with the same id and importer (#19016)
1 parent 2b602e2 commit b178c90

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

packages/vite/src/node/server/__tests__/pluginContainer.spec.ts

+57
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,63 @@ describe('plugin container', () => {
258258
const result = await environment.pluginContainer.resolveId('foo')
259259
expect(result).toStrictEqual({ id: 'success' })
260260
})
261+
262+
it('should skip the plugin if it has been called before with the same id and importer (1)', async () => {
263+
const p1: Plugin = {
264+
name: 'p1',
265+
async resolveId(id, importer) {
266+
return (
267+
(await this.resolve(id.replace(/\/modified$/, ''), importer, {
268+
skipSelf: true,
269+
})) ?? 'success'
270+
)
271+
},
272+
}
273+
const p2: Plugin = {
274+
name: 'p2',
275+
async resolveId(id, importer) {
276+
return await this.resolve(id + '/modified', importer, {
277+
skipSelf: true,
278+
})
279+
},
280+
}
281+
const environment = await getDevEnvironment({ plugins: [p1, p2] })
282+
const result = await environment.pluginContainer.resolveId('foo')
283+
expect(result).toStrictEqual({ id: 'success' })
284+
})
285+
286+
it('should skip the plugin if it has been called before with the same id and importer (2)', async () => {
287+
const p1: Plugin = {
288+
name: 'p1',
289+
async resolveId(id, importer) {
290+
return (
291+
(await this.resolve(id.replace(/\/modified$/, ''), importer, {
292+
skipSelf: true,
293+
})) ?? 'failure1'
294+
)
295+
},
296+
}
297+
const p2: Plugin = {
298+
name: 'p2',
299+
async resolveId(id, importer) {
300+
return await this.resolve(id + '/modified', importer, {
301+
skipSelf: true,
302+
})
303+
},
304+
}
305+
const p3: Plugin = {
306+
name: 'p3',
307+
resolveId(id) {
308+
if (id.endsWith('/modified')) {
309+
return 'success'
310+
}
311+
return 'failure2'
312+
},
313+
}
314+
const environment = await getDevEnvironment({ plugins: [p1, p2, p3] })
315+
const result = await environment.pluginContainer.resolveId('foo')
316+
expect(result).toStrictEqual({ id: 'success' })
317+
})
261318
})
262319
})
263320
})

packages/vite/src/node/server/pluginContainer.ts

+24-8
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export type SkipInformation = {
148148
id: string
149149
importer: string | undefined
150150
plugin: Plugin
151+
called?: boolean
151152
}
152153

153154
class EnvironmentPluginContainer {
@@ -364,7 +365,7 @@ class EnvironmentPluginContainer {
364365

365366
const mergedSkip = new Set<Plugin>(skip)
366367
for (const call of skipCalls ?? []) {
367-
if (call.id === rawId && call.importer === importer) {
368+
if (call.called || (call.id === rawId && call.importer === importer)) {
368369
mergedSkip.add(call.plugin)
369370
}
370371
}
@@ -576,13 +577,28 @@ class PluginContext implements Omit<RollupPluginContext, 'cache'> {
576577
skipSelf?: boolean
577578
},
578579
) {
579-
const skipCalls =
580-
options?.skipSelf === false
581-
? this._resolveSkipCalls
582-
: [
583-
...(this._resolveSkipCalls || []),
584-
{ id, importer, plugin: this._plugin },
585-
]
580+
let skipCalls: readonly SkipInformation[] | undefined
581+
if (options?.skipSelf === false) {
582+
skipCalls = this._resolveSkipCalls
583+
} else if (this._resolveSkipCalls) {
584+
const skipCallsTemp = [...this._resolveSkipCalls]
585+
const sameCallIndex = this._resolveSkipCalls.findIndex(
586+
(c) =>
587+
c.id === id && c.importer === importer && c.plugin === this._plugin,
588+
)
589+
if (sameCallIndex !== -1) {
590+
skipCallsTemp[sameCallIndex] = {
591+
...skipCallsTemp[sameCallIndex],
592+
called: true,
593+
}
594+
} else {
595+
skipCallsTemp.push({ id, importer, plugin: this._plugin })
596+
}
597+
skipCalls = skipCallsTemp
598+
} else {
599+
skipCalls = [{ id, importer, plugin: this._plugin }]
600+
}
601+
586602
let out = await this._container.resolveId(id, importer, {
587603
attributes: options?.attributes,
588604
custom: options?.custom,

0 commit comments

Comments
 (0)