From b4f4b1fd9185594e1edbed3ab02bcf607edc41f6 Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Tue, 10 Oct 2023 10:56:33 +0200 Subject: [PATCH] bind plugin context functions --- .../server/__tests__/pluginContainer.spec.ts | 26 +++++++++++++++++++ .../vite/src/node/server/pluginContainer.ts | 8 ++++++ 2 files changed, 34 insertions(+) diff --git a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts index a20fc919f7fa10..038c8ad5016a02 100644 --- a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts +++ b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts @@ -10,6 +10,32 @@ let resolveId: (id: string) => any let moduleGraph: ModuleGraph describe('plugin container', () => { + it('has bound methods', async () => { + expect.assertions(8) + const entryUrl = '/x.js' + + const plugin: Plugin = { + name: 'p1', + load(id) { + // bound functions and bound arrow functions do not have prototype + expect(this.load.prototpe).equals(undefined) + expect(this.parse.prototpe).equals(undefined) + expect(this.resolve.prototpe).equals(undefined) + expect(this.addWatchFile.prototpe).equals(undefined) + expect(this.setAssetSource.prototpe).equals(undefined) + expect(this.getFileName.prototpe).equals(undefined) + expect(this.warn.prototpe).equals(undefined) + expect(this.error.prototpe).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 76985d93b0cf5d..133f9b6f59609b 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -292,6 +292,14 @@ export async function createPluginContainer( constructor(initialPlugin?: Plugin) { this._activePlugin = initialPlugin || null + this.load = this.load.bind(this); + this.parse = this.parse.bind(this); + this.resolve = this.resolve.bind(this); + this.addWatchFile = this.addWatchFile.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); } parse(code: string, opts: any = {}) {