Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pluginContainer): run transform in this.load #14965

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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