Skip to content

Commit 4727320

Browse files
authoredDec 10, 2024··
fix: this.resolve skipSelf should not skip for different id or import (#18903)
1 parent 924b352 commit 4727320

File tree

3 files changed

+80
-9
lines changed

3 files changed

+80
-9
lines changed
 

‎packages/vite/src/node/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export type {
145145
WebSocketClient,
146146
WebSocketCustomListener,
147147
} from './server/ws'
148-
export type { PluginContainer } from './server/pluginContainer'
148+
export type { SkipInformation, PluginContainer } from './server/pluginContainer'
149149
export type {
150150
EnvironmentModuleGraph,
151151
EnvironmentModuleNode,

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

+47
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,53 @@ describe('plugin container', () => {
213213
expect(result.code).equals('3')
214214
})
215215
})
216+
217+
describe('resolveId', () => {
218+
describe('skipSelf', () => {
219+
it('should skip the plugin itself when skipSelf is true', async () => {
220+
let calledCount = 0
221+
const plugin: Plugin = {
222+
name: 'p1',
223+
async resolveId(id, importer) {
224+
calledCount++
225+
if (calledCount <= 1) {
226+
return await this.resolve(id, importer, { skipSelf: true })
227+
}
228+
return id
229+
},
230+
}
231+
232+
const environment = await getDevEnvironment({ plugins: [plugin] })
233+
await environment.pluginContainer.resolveId('/x.js')
234+
expect(calledCount).toBe(1)
235+
})
236+
237+
it('should skip the plugin only when id and importer is same', async () => {
238+
const p1: Plugin = {
239+
name: 'p1',
240+
async resolveId(id, importer) {
241+
if (id === 'foo/modified') {
242+
return 'success'
243+
}
244+
return await this.resolve(id, importer, { skipSelf: true })
245+
},
246+
}
247+
const p2: Plugin = {
248+
name: 'p2',
249+
async resolveId(id, importer) {
250+
const resolved = await this.resolve(id + '/modified', importer, {
251+
skipSelf: true,
252+
})
253+
return resolved ?? 'failed'
254+
},
255+
}
256+
257+
const environment = await getDevEnvironment({ plugins: [p1, p2] })
258+
const result = await environment.pluginContainer.resolveId('foo')
259+
expect(result).toStrictEqual({ id: 'success' })
260+
})
261+
})
262+
})
216263
})
217264

218265
async function getDevEnvironment(

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

+32-8
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ export async function createEnvironmentPluginContainer(
144144
return container
145145
}
146146

147+
export type SkipInformation = {
148+
id: string
149+
importer: string | undefined
150+
plugin: Plugin
151+
}
152+
147153
class EnvironmentPluginContainer {
148154
private _pluginContextMap = new Map<Plugin, PluginContext>()
149155
private _resolvedRollupOptions?: InputOptions
@@ -336,7 +342,9 @@ class EnvironmentPluginContainer {
336342
options?: {
337343
attributes?: Record<string, string>
338344
custom?: CustomPluginOptions
345+
/** @deprecated use `skipCalls` instead */
339346
skip?: Set<Plugin>
347+
skipCalls?: readonly SkipInformation[]
340348
/**
341349
* @internal
342350
*/
@@ -349,17 +357,25 @@ class EnvironmentPluginContainer {
349357
await this._buildStartPromise
350358
}
351359
const skip = options?.skip
360+
const skipCalls = options?.skipCalls
352361
const scan = !!options?.scan
353362
const ssr = this.environment.config.consumer === 'server'
354-
const ctx = new ResolveIdContext(this, skip, scan)
363+
const ctx = new ResolveIdContext(this, skip, skipCalls, scan)
364+
365+
const mergedSkip = new Set<Plugin>(skip)
366+
for (const call of skipCalls ?? []) {
367+
if (call.id === rawId && call.importer === importer) {
368+
mergedSkip.add(call.plugin)
369+
}
370+
}
355371

356372
const resolveStart = debugResolve ? performance.now() : 0
357373
let id: string | null = null
358374
const partial: Partial<PartialResolvedId> = {}
359375
for (const plugin of this.getSortedPlugins('resolveId')) {
360376
if (this._closed && this.environment.config.dev.recoverable)
361377
throwClosedServerError()
362-
if (skip?.has(plugin)) continue
378+
if (mergedSkip?.has(plugin)) continue
363379

364380
ctx._plugin = plugin
365381

@@ -534,6 +550,7 @@ class PluginContext implements Omit<RollupPluginContext, 'cache'> {
534550
_activeId: string | null = null
535551
_activeCode: string | null = null
536552
_resolveSkips?: Set<Plugin>
553+
_resolveSkipCalls?: readonly SkipInformation[]
537554
meta: RollupPluginContext['meta']
538555
environment: Environment
539556

@@ -559,16 +576,19 @@ class PluginContext implements Omit<RollupPluginContext, 'cache'> {
559576
skipSelf?: boolean
560577
},
561578
) {
562-
let skip: Set<Plugin> | undefined
563-
if (options?.skipSelf !== false) {
564-
skip = new Set(this._resolveSkips)
565-
skip.add(this._plugin)
566-
}
579+
const skipCalls =
580+
options?.skipSelf === false
581+
? this._resolveSkipCalls
582+
: [
583+
...(this._resolveSkipCalls || []),
584+
{ id, importer, plugin: this._plugin },
585+
]
567586
let out = await this._container.resolveId(id, importer, {
568587
attributes: options?.attributes,
569588
custom: options?.custom,
570589
isEntry: !!options?.isEntry,
571-
skip,
590+
skip: this._resolveSkips,
591+
skipCalls,
572592
scan: this._scan,
573593
})
574594
if (typeof out === 'string') out = { id: out }
@@ -794,10 +814,12 @@ class ResolveIdContext extends PluginContext {
794814
constructor(
795815
container: EnvironmentPluginContainer,
796816
skip: Set<Plugin> | undefined,
817+
skipCalls: readonly SkipInformation[] | undefined,
797818
scan: boolean,
798819
) {
799820
super(null!, container)
800821
this._resolveSkips = skip
822+
this._resolveSkipCalls = skipCalls
801823
this._scan = scan
802824
}
803825
}
@@ -999,7 +1021,9 @@ class PluginContainer {
9991021
options?: {
10001022
attributes?: Record<string, string>
10011023
custom?: CustomPluginOptions
1024+
/** @deprecated use `skipCalls` instead */
10021025
skip?: Set<Plugin>
1026+
skipCalls?: readonly SkipInformation[]
10031027
ssr?: boolean
10041028
/**
10051029
* @internal

0 commit comments

Comments
 (0)
Please sign in to comment.