Skip to content

Commit

Permalink
fix: handle context resolve options (#8966)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 7, 2022
1 parent 2185f72 commit 57c6c15
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
46 changes: 46 additions & 0 deletions packages/vite/src/node/server/__tests__/pluginContainer.spec.ts
Expand Up @@ -100,6 +100,52 @@ describe('plugin container', () => {

expect.assertions(1)
})

it('can pass custom resolve opts between plugins', async () => {
const entryUrl = '/x.js'

const plugin1: Plugin = {
name: 'p1',
resolveId(id) {
if (id === entryUrl) {
return this.resolve('foobar', 'notreal', {
custom: { p1: 'success' },
isEntry: true
})
}
}
}

const plugin2: Plugin = {
name: 'p2',
resolveId(id, importer, opts) {
if (id === 'foobar') {
expect(importer).toBe('notreal')
expect(opts).toEqual(
expect.objectContaining({
custom: { p1: 'success' },
isEntry: true
})
)
return entryUrl
}
},
load(id) {
if (id === entryUrl) {
return null
}
}
}

const container = await getPluginContainer({
plugins: [plugin1, plugin2]
})

await moduleGraph.ensureEntryFromUrl(entryUrl, false)
await container.load(entryUrl)

expect.assertions(2)
})
})
})

Expand Down
18 changes: 15 additions & 3 deletions packages/vite/src/node/server/pluginContainer.ts
Expand Up @@ -34,6 +34,7 @@ import { join, resolve } from 'node:path'
import { performance } from 'node:perf_hooks'
import { createRequire } from 'node:module'
import type {
CustomPluginOptions,
EmittedFile,
InputOptions,
LoadResult,
Expand Down Expand Up @@ -91,6 +92,7 @@ export interface PluginContainer {
id: string,
importer?: string,
options?: {
custom?: CustomPluginOptions
skip?: Set<Plugin>
ssr?: boolean
/**
Expand Down Expand Up @@ -258,14 +260,20 @@ export async function createPluginContainer(
async resolve(
id: string,
importer?: string,
options?: { skipSelf?: boolean }
options?: {
custom?: CustomPluginOptions
isEntry?: boolean
skipSelf?: boolean
}
) {
let skip: Set<Plugin> | undefined
if (options?.skipSelf && this._activePlugin) {
skip = new Set(this._resolveSkips)
skip.add(this._activePlugin)
}
let out = await container.resolveId(id, importer, {
custom: options?.custom,
isEntry: !!options?.isEntry,
skip,
ssr: this.ssr,
scan: this._scan
Expand Down Expand Up @@ -528,7 +536,6 @@ export async function createPluginContainer(
const skip = options?.skip
const ssr = options?.ssr
const scan = !!options?.scan
const isEntry = !!options?.isEntry
const ctx = new Context()
ctx.ssr = !!ssr
ctx._scan = scan
Expand All @@ -548,7 +555,12 @@ export async function createPluginContainer(
ctx as any,
rawId,
importer,
{ ssr, scan, isEntry }
{
custom: options?.custom,
isEntry: !!options?.isEntry,
ssr,
scan
}
)
if (!result) continue

Expand Down

0 comments on commit 57c6c15

Please sign in to comment.