From 2162ee8027ee1e27519512380a28ca068b54fd9d Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Tue, 10 Oct 2023 10:56:33 +0200 Subject: [PATCH 1/7] 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..6ad31fd789107a 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.prototype).equals(undefined) + expect(this.parse.prototype).equals(undefined) + expect(this.resolve.prototype).equals(undefined) + expect(this.addWatchFile.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) + }, + } + + 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..6374e44826c9f3 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 = {}) { From b3f125cfb4ea68c9688e90f8ef5f68639f0cb78e Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Mon, 27 Nov 2023 10:28:13 +0100 Subject: [PATCH 2/7] make all functions bound --- packages/vite/src/node/server/pluginContainer.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 6374e44826c9f3..b3b325c1c28d68 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -295,11 +295,16 @@ export async function createPluginContainer( this.load = this.load.bind(this) this.parse = this.parse.bind(this) this.resolve = this.resolve.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.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 = {}) { From 8278a27d7cb70a53dcef399717cbf4767bd8e42f Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Mon, 27 Nov 2023 10:30:40 +0100 Subject: [PATCH 3/7] test it --- .../vite/src/node/server/__tests__/pluginContainer.spec.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts index 6ad31fd789107a..e5ee0773992a42 100644 --- a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts +++ b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts @@ -21,11 +21,16 @@ describe('plugin container', () => { expect(this.load.prototype).equals(undefined) expect(this.parse.prototype).equals(undefined) expect(this.resolve.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.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) }, } From 0451150938edaa8abe2fb8a9e445af1c0a690aab Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Mon, 27 Nov 2023 10:45:07 +0100 Subject: [PATCH 4/7] 13 functions --- packages/vite/src/node/server/__tests__/pluginContainer.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts index e5ee0773992a42..f9ac5c3f4bef15 100644 --- a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts +++ b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts @@ -11,7 +11,7 @@ let moduleGraph: ModuleGraph describe('plugin container', () => { it('has bound methods', async () => { - expect.assertions(8) + expect.assertions(13) const entryUrl = '/x.js' const plugin: Plugin = { From 7ee50734eb2226ab8a0d220a2c1d4f7afb89b7ae Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Tue, 28 Nov 2023 10:40:29 +0100 Subject: [PATCH 5/7] Update packages/vite/src/node/server/pluginContainer.ts Co-authored-by: Bjorn Lu --- packages/vite/src/node/server/pluginContainer.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index b3b325c1c28d68..45b2b9ff360f8d 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -292,13 +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.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) From b1bc7b8b20732421044a2b761db2c774c6497887 Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Tue, 28 Nov 2023 10:41:39 +0100 Subject: [PATCH 6/7] fix missing function test --- .../vite/src/node/server/__tests__/pluginContainer.spec.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts index f9ac5c3f4bef15..bd3fef5fa97847 100644 --- a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts +++ b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts @@ -11,7 +11,7 @@ let moduleGraph: ModuleGraph describe('plugin container', () => { it('has bound methods', async () => { - expect.assertions(13) + expect.assertions(14) const entryUrl = '/x.js' const plugin: Plugin = { @@ -25,6 +25,7 @@ describe('plugin container', () => { 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) From 9335d0881519c9d225634c1313a32b637c166c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Tue, 28 Nov 2023 20:56:23 +0900 Subject: [PATCH 7/7] test: reorder method test --- packages/vite/src/node/server/__tests__/pluginContainer.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts index bd3fef5fa97847..7afae2a9ef3d66 100644 --- a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts +++ b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts @@ -18,9 +18,9 @@ describe('plugin container', () => { name: 'p1', load(id) { // bound functions and bound arrow functions do not have prototype - expect(this.load.prototype).equals(undefined) 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)