Skip to content

Commit

Permalink
bind plugin context functions
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Oct 10, 2023
1 parent a3b6d8d commit b4f4b1f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
26 changes: 26 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,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))
Expand Down
8 changes: 8 additions & 0 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down

0 comments on commit b4f4b1f

Please sign in to comment.