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) {