Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle context resolve options #8966

Merged
merged 2 commits into from Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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