From 85ab77a6ee40548395c421066a16498acf53c209 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 7 Jul 2022 17:26:49 +0800 Subject: [PATCH 1/2] fix: handle resolve custom --- .../vite/src/node/server/pluginContainer.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 0a708ed62c4e6c..fc1523fc7888a0 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -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, @@ -91,6 +92,7 @@ export interface PluginContainer { id: string, importer?: string, options?: { + custom?: CustomPluginOptions skip?: Set ssr?: boolean /** @@ -258,7 +260,11 @@ export async function createPluginContainer( async resolve( id: string, importer?: string, - options?: { skipSelf?: boolean } + options?: { + custom?: CustomPluginOptions + isEntry?: boolean + skipSelf?: boolean + } ) { let skip: Set | undefined if (options?.skipSelf && this._activePlugin) { @@ -266,6 +272,8 @@ export async function createPluginContainer( skip.add(this._activePlugin) } let out = await container.resolveId(id, importer, { + custom: options?.custom, + isEntry: !!options?.isEntry, skip, ssr: this.ssr, scan: this._scan @@ -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 @@ -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 From 6daca97548f67bd62bb2b7d0cf80f0f2ecd6a465 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 7 Jul 2022 17:36:52 +0800 Subject: [PATCH 2/2] chore: add test --- .../server/__tests__/pluginContainer.spec.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts index c6f81e9107ef0b..94acc70304e86e 100644 --- a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts +++ b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts @@ -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) + }) }) })