Skip to content

Commit

Permalink
fix(dev): bind plugin context functions (#14569)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: 翠 / green <green@sapphi.red>
  • Loading branch information
3 people authored Nov 28, 2023
1 parent 39f435d commit cb3243c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/vite/src/node/server/__tests__/pluginContainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cb3243c

Please sign in to comment.