Skip to content

Commit

Permalink
fix: skip the plugin if it has been called before with the same id an…
Browse files Browse the repository at this point in the history
…d importer (vitejs#19016)
  • Loading branch information
sapphi-red authored Jan 2, 2025
1 parent 2b602e2 commit b178c90
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
57 changes: 57 additions & 0 deletions packages/vite/src/node/server/__tests__/pluginContainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,63 @@ describe('plugin container', () => {
const result = await environment.pluginContainer.resolveId('foo')
expect(result).toStrictEqual({ id: 'success' })
})

it('should skip the plugin if it has been called before with the same id and importer (1)', async () => {
const p1: Plugin = {
name: 'p1',
async resolveId(id, importer) {
return (
(await this.resolve(id.replace(/\/modified$/, ''), importer, {
skipSelf: true,
})) ?? 'success'
)
},
}
const p2: Plugin = {
name: 'p2',
async resolveId(id, importer) {
return await this.resolve(id + '/modified', importer, {
skipSelf: true,
})
},
}
const environment = await getDevEnvironment({ plugins: [p1, p2] })
const result = await environment.pluginContainer.resolveId('foo')
expect(result).toStrictEqual({ id: 'success' })
})

it('should skip the plugin if it has been called before with the same id and importer (2)', async () => {
const p1: Plugin = {
name: 'p1',
async resolveId(id, importer) {
return (
(await this.resolve(id.replace(/\/modified$/, ''), importer, {
skipSelf: true,
})) ?? 'failure1'
)
},
}
const p2: Plugin = {
name: 'p2',
async resolveId(id, importer) {
return await this.resolve(id + '/modified', importer, {
skipSelf: true,
})
},
}
const p3: Plugin = {
name: 'p3',
resolveId(id) {
if (id.endsWith('/modified')) {
return 'success'
}
return 'failure2'
},
}
const environment = await getDevEnvironment({ plugins: [p1, p2, p3] })
const result = await environment.pluginContainer.resolveId('foo')
expect(result).toStrictEqual({ id: 'success' })
})
})
})
})
Expand Down
32 changes: 24 additions & 8 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export type SkipInformation = {
id: string
importer: string | undefined
plugin: Plugin
called?: boolean
}

class EnvironmentPluginContainer {
Expand Down Expand Up @@ -364,7 +365,7 @@ class EnvironmentPluginContainer {

const mergedSkip = new Set<Plugin>(skip)
for (const call of skipCalls ?? []) {
if (call.id === rawId && call.importer === importer) {
if (call.called || (call.id === rawId && call.importer === importer)) {
mergedSkip.add(call.plugin)
}
}
Expand Down Expand Up @@ -576,13 +577,28 @@ class PluginContext implements Omit<RollupPluginContext, 'cache'> {
skipSelf?: boolean
},
) {
const skipCalls =
options?.skipSelf === false
? this._resolveSkipCalls
: [
...(this._resolveSkipCalls || []),
{ id, importer, plugin: this._plugin },
]
let skipCalls: readonly SkipInformation[] | undefined
if (options?.skipSelf === false) {
skipCalls = this._resolveSkipCalls
} else if (this._resolveSkipCalls) {
const skipCallsTemp = [...this._resolveSkipCalls]
const sameCallIndex = this._resolveSkipCalls.findIndex(
(c) =>
c.id === id && c.importer === importer && c.plugin === this._plugin,
)
if (sameCallIndex !== -1) {
skipCallsTemp[sameCallIndex] = {
...skipCallsTemp[sameCallIndex],
called: true,
}
} else {
skipCallsTemp.push({ id, importer, plugin: this._plugin })
}
skipCalls = skipCallsTemp
} else {
skipCalls = [{ id, importer, plugin: this._plugin }]
}

let out = await this._container.resolveId(id, importer, {
attributes: options?.attributes,
custom: options?.custom,
Expand Down

0 comments on commit b178c90

Please sign in to comment.