Skip to content

Commit 3f57b05

Browse files
authored
fix(pluginContainer): run transform in this.load (#14965)
1 parent 6a564fa commit 3f57b05

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

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

+36-4
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ describe('plugin container', () => {
3232
},
3333
load(id) {
3434
if (id === entryUrl) {
35-
const { meta } = this.getModuleInfo(entryUrl)
35+
const { meta } = this.getModuleInfo(entryUrl) ?? {}
3636
metaArray.push(meta)
3737

3838
return { code: 'export {}', meta: { x: 2 } }
3939
}
4040
},
4141
transform(code, id) {
4242
if (id === entryUrl) {
43-
const { meta } = this.getModuleInfo(entryUrl)
43+
const { meta } = this.getModuleInfo(entryUrl) ?? {}
4444
metaArray.push(meta)
4545

4646
return { meta: { x: 3 } }
4747
}
4848
},
4949
buildEnd() {
50-
const { meta } = this.getModuleInfo(entryUrl)
50+
const { meta } = this.getModuleInfo(entryUrl) ?? {}
5151
metaArray.push(meta)
5252
},
5353
}
@@ -84,7 +84,7 @@ describe('plugin container', () => {
8484
name: 'p2',
8585
load(id) {
8686
if (id === entryUrl) {
87-
const { meta } = this.getModuleInfo(entryUrl)
87+
const { meta } = this.getModuleInfo(entryUrl) ?? {}
8888
expect(meta).toEqual({ x: 1 })
8989
return null
9090
}
@@ -184,6 +184,38 @@ describe('plugin container', () => {
184184
const result: any = await container.transform(loadResult.code, entryUrl)
185185
expect(result.code).equals('2')
186186
})
187+
188+
it('will load and transform the module', async () => {
189+
const entryUrl = '/x.js'
190+
const otherUrl = '/y.js'
191+
192+
const plugin: Plugin = {
193+
name: 'p1',
194+
resolveId(id) {
195+
return id
196+
},
197+
load(id) {
198+
if (id === entryUrl) return { code: '1' }
199+
else if (id === otherUrl) return { code: '2', meta: { code: '2' } }
200+
},
201+
async transform(code, id) {
202+
if (id === entryUrl) {
203+
// NOTE: ModuleInfo.code not implemented, used `.meta.code` for now
204+
return (await this.load({ id: otherUrl }))?.meta.code
205+
} else if (id === otherUrl) {
206+
return { code: '3', meta: { code: '3' } }
207+
}
208+
},
209+
}
210+
211+
const container = await getPluginContainer({
212+
plugins: [plugin],
213+
})
214+
await moduleGraph.ensureEntryFromUrl(entryUrl, false)
215+
const loadResult: any = await container.load(entryUrl)
216+
const result: any = await container.transform(loadResult.code, entryUrl)
217+
expect(result.code).equals('3')
218+
})
187219
})
188220
})
189221

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,13 @@ export async function createPluginContainer(
333333
// but we can at least update the module info properties we support
334334
updateModuleInfo(options.id, options)
335335

336-
await container.load(options.id, { ssr: this.ssr })
336+
const loadResult = await container.load(options.id, { ssr: this.ssr })
337+
const code =
338+
typeof loadResult === 'object' ? loadResult?.code : loadResult
339+
if (code != null) {
340+
await container.transform(code, options.id, { ssr: this.ssr })
341+
}
342+
337343
const moduleInfo = this.getModuleInfo(options.id)
338344
// This shouldn't happen due to calling ensureEntryFromUrl, but 1) our types can't ensure that
339345
// and 2) moduleGraph may not have been provided (though in the situations where that happens,

0 commit comments

Comments
 (0)