Skip to content

Commit cb3243c

Browse files
patricklxbluwysapphi-red
authored
fix(dev): bind plugin context functions (#14569)
Co-authored-by: Bjorn Lu <[email protected]> Co-authored-by: 翠 / green <[email protected]>
1 parent 39f435d commit cb3243c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

packages/vite/src/node/server/__tests__/pluginContainer.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@ let resolveId: (id: string) => any
1010
let moduleGraph: ModuleGraph
1111

1212
describe('plugin container', () => {
13+
it('has bound methods', async () => {
14+
expect.assertions(14)
15+
const entryUrl = '/x.js'
16+
17+
const plugin: Plugin = {
18+
name: 'p1',
19+
load(id) {
20+
// bound functions and bound arrow functions do not have prototype
21+
expect(this.parse.prototype).equals(undefined)
22+
expect(this.resolve.prototype).equals(undefined)
23+
expect(this.load.prototype).equals(undefined)
24+
expect(this.getModuleInfo.prototype).equals(undefined)
25+
expect(this.getModuleIds.prototype).equals(undefined)
26+
expect(this.addWatchFile.prototype).equals(undefined)
27+
expect(this.getWatchFiles.prototype).equals(undefined)
28+
expect(this.emitFile.prototype).equals(undefined)
29+
expect(this.setAssetSource.prototype).equals(undefined)
30+
expect(this.getFileName.prototype).equals(undefined)
31+
expect(this.warn.prototype).equals(undefined)
32+
expect(this.error.prototype).equals(undefined)
33+
expect(this.debug.prototype).equals(undefined)
34+
expect(this.info.prototype).equals(undefined)
35+
},
36+
}
37+
38+
const container = await getPluginContainer({
39+
plugins: [plugin],
40+
})
41+
42+
await container.load(entryUrl)
43+
})
44+
1345
describe('getModuleInfo', () => {
1446
beforeEach(() => {
1547
moduleGraph = new ModuleGraph((id) => resolveId(id))

packages/vite/src/node/server/pluginContainer.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,20 @@ export async function createPluginContainer(
299299

300300
constructor(initialPlugin?: Plugin) {
301301
this._activePlugin = initialPlugin || null
302+
this.parse = this.parse.bind(this)
303+
this.resolve = this.resolve.bind(this)
304+
this.load = this.load.bind(this)
305+
this.getModuleInfo = this.getModuleInfo.bind(this)
306+
this.getModuleIds = this.getModuleIds.bind(this)
307+
this.addWatchFile = this.addWatchFile.bind(this)
308+
this.getWatchFiles = this.getWatchFiles.bind(this)
309+
this.emitFile = this.emitFile.bind(this)
310+
this.setAssetSource = this.setAssetSource.bind(this)
311+
this.getFileName = this.getFileName.bind(this)
312+
this.warn = this.warn.bind(this)
313+
this.error = this.error.bind(this)
314+
this.debug = this.debug.bind(this)
315+
this.info = this.info.bind(this)
302316
}
303317

304318
parse(code: string, opts: any) {

0 commit comments

Comments
 (0)