From cb3243c86ebc6bd7212620db27d50ea2a2bee460 Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Tue, 28 Nov 2023 13:26:44 +0100 Subject: [PATCH] fix(dev): bind plugin context functions (#14569) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bjorn Lu Co-authored-by: 翠 / green --- .../server/__tests__/pluginContainer.spec.ts | 32 +++++++++++++++++++ .../vite/src/node/server/pluginContainer.ts | 14 ++++++++ 2 files 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 070dedd2acb463..33243331f35da2 100644 --- a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts +++ b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts @@ -10,6 +10,38 @@ let resolveId: (id: string) => any let moduleGraph: ModuleGraph describe('plugin container', () => { + it('has bound methods', async () => { + expect.assertions(14) + const entryUrl = '/x.js' + + const plugin: Plugin = { + name: 'p1', + load(id) { + // bound functions and bound arrow functions do not have prototype + expect(this.parse.prototype).equals(undefined) + expect(this.resolve.prototype).equals(undefined) + expect(this.load.prototype).equals(undefined) + expect(this.getModuleInfo.prototype).equals(undefined) + expect(this.getModuleIds.prototype).equals(undefined) + expect(this.addWatchFile.prototype).equals(undefined) + expect(this.getWatchFiles.prototype).equals(undefined) + expect(this.emitFile.prototype).equals(undefined) + expect(this.setAssetSource.prototype).equals(undefined) + expect(this.getFileName.prototype).equals(undefined) + expect(this.warn.prototype).equals(undefined) + expect(this.error.prototype).equals(undefined) + expect(this.debug.prototype).equals(undefined) + expect(this.info.prototype).equals(undefined) + }, + } + + const container = await getPluginContainer({ + plugins: [plugin], + }) + + await container.load(entryUrl) + }) + describe('getModuleInfo', () => { beforeEach(() => { moduleGraph = new ModuleGraph((id) => resolveId(id)) diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index bf8c6337f8bc49..5e62f00a2e1156 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -299,6 +299,20 @@ export async function createPluginContainer( constructor(initialPlugin?: Plugin) { this._activePlugin = initialPlugin || null + this.parse = this.parse.bind(this) + this.resolve = this.resolve.bind(this) + this.load = this.load.bind(this) + this.getModuleInfo = this.getModuleInfo.bind(this) + this.getModuleIds = this.getModuleIds.bind(this) + this.addWatchFile = this.addWatchFile.bind(this) + this.getWatchFiles = this.getWatchFiles.bind(this) + this.emitFile = this.emitFile.bind(this) + this.setAssetSource = this.setAssetSource.bind(this) + this.getFileName = this.getFileName.bind(this) + this.warn = this.warn.bind(this) + this.error = this.error.bind(this) + this.debug = this.debug.bind(this) + this.info = this.info.bind(this) } parse(code: string, opts: any) {