Skip to content

Commit

Permalink
fix(pluginContainer): run transform in this.load (#14965)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 13, 2023
1 parent 6a564fa commit 3f57b05
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
40 changes: 36 additions & 4 deletions packages/vite/src/node/server/__tests__/pluginContainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ describe('plugin container', () => {
},
load(id) {
if (id === entryUrl) {
const { meta } = this.getModuleInfo(entryUrl)
const { meta } = this.getModuleInfo(entryUrl) ?? {}
metaArray.push(meta)

return { code: 'export {}', meta: { x: 2 } }
}
},
transform(code, id) {
if (id === entryUrl) {
const { meta } = this.getModuleInfo(entryUrl)
const { meta } = this.getModuleInfo(entryUrl) ?? {}
metaArray.push(meta)

return { meta: { x: 3 } }
}
},
buildEnd() {
const { meta } = this.getModuleInfo(entryUrl)
const { meta } = this.getModuleInfo(entryUrl) ?? {}
metaArray.push(meta)
},
}
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('plugin container', () => {
name: 'p2',
load(id) {
if (id === entryUrl) {
const { meta } = this.getModuleInfo(entryUrl)
const { meta } = this.getModuleInfo(entryUrl) ?? {}
expect(meta).toEqual({ x: 1 })
return null
}
Expand Down Expand Up @@ -184,6 +184,38 @@ describe('plugin container', () => {
const result: any = await container.transform(loadResult.code, entryUrl)
expect(result.code).equals('2')
})

it('will load and transform the module', async () => {
const entryUrl = '/x.js'
const otherUrl = '/y.js'

const plugin: Plugin = {
name: 'p1',
resolveId(id) {
return id
},
load(id) {
if (id === entryUrl) return { code: '1' }
else if (id === otherUrl) return { code: '2', meta: { code: '2' } }
},
async transform(code, id) {
if (id === entryUrl) {
// NOTE: ModuleInfo.code not implemented, used `.meta.code` for now
return (await this.load({ id: otherUrl }))?.meta.code
} else if (id === otherUrl) {
return { code: '3', meta: { code: '3' } }
}
},
}

const container = await getPluginContainer({
plugins: [plugin],
})
await moduleGraph.ensureEntryFromUrl(entryUrl, false)
const loadResult: any = await container.load(entryUrl)
const result: any = await container.transform(loadResult.code, entryUrl)
expect(result.code).equals('3')
})
})
})

Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,13 @@ export async function createPluginContainer(
// but we can at least update the module info properties we support
updateModuleInfo(options.id, options)

await container.load(options.id, { ssr: this.ssr })
const loadResult = await container.load(options.id, { ssr: this.ssr })
const code =
typeof loadResult === 'object' ? loadResult?.code : loadResult
if (code != null) {
await container.transform(code, options.id, { ssr: this.ssr })
}

const moduleInfo = this.getModuleInfo(options.id)
// This shouldn't happen due to calling ensureEntryFromUrl, but 1) our types can't ensure that
// and 2) moduleGraph may not have been provided (though in the situations where that happens,
Expand Down

0 comments on commit 3f57b05

Please sign in to comment.