Skip to content

Commit 258cdd6

Browse files
authored
fix: merge client and ssr values for pluginContainer.getModuleInfo (#18895)
1 parent 4727320 commit 258cdd6

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import type {
1313
* We are going to deprecate these types and we can try to use them back in the future.
1414
*/
1515

16+
// same default value of "moduleInfo.meta" as in Rollup
17+
const EMPTY_OBJECT = Object.freeze({})
18+
1619
export class ModuleNode {
1720
_moduleGraph: ModuleGraph
1821
_clientModule: EnvironmentModuleNode | undefined
@@ -76,6 +79,48 @@ export class ModuleNode {
7679
}
7780
return importedModules
7881
}
82+
_getModuleInfoUnion(prop: 'info'): ModuleInfo | undefined {
83+
const _clientValue = this._clientModule?.[prop]
84+
const _ssrValue = this._ssrModule?.[prop]
85+
86+
if (_clientValue == null && _ssrValue == null) return undefined
87+
88+
return new Proxy({} as any, {
89+
get: (_, key: string) => {
90+
// `meta` refers to `ModuleInfo.meta` so we refer to `this.meta` to
91+
// handle the object union between client and ssr
92+
if (key === 'meta') {
93+
return this.meta || EMPTY_OBJECT
94+
}
95+
if (_clientValue) {
96+
if (key in _clientValue) {
97+
return _clientValue[key as keyof ModuleInfo]
98+
}
99+
}
100+
if (_ssrValue) {
101+
if (key in _ssrValue) {
102+
return _ssrValue[key as keyof ModuleInfo]
103+
}
104+
}
105+
},
106+
})
107+
}
108+
_getModuleObjectUnion(prop: 'meta'): Record<string, any> | undefined {
109+
const _clientValue = this._clientModule?.[prop]
110+
const _ssrValue = this._ssrModule?.[prop]
111+
112+
if (_clientValue == null && _ssrValue == null) return undefined
113+
114+
const info: Record<string, any> = {}
115+
if (_ssrValue) {
116+
Object.assign(info, _ssrValue)
117+
}
118+
if (_clientValue) {
119+
Object.assign(info, _clientValue)
120+
}
121+
return info
122+
}
123+
79124
get url(): string {
80125
return this._get('url')
81126
}
@@ -97,11 +142,13 @@ export class ModuleNode {
97142
get type(): 'js' | 'css' {
98143
return this._get('type')
99144
}
145+
// `info` needs special care as it's defined as a proxy in `pluginContainer`,
146+
// so we also merge it as a proxy too
100147
get info(): ModuleInfo | undefined {
101-
return this._get('info')
148+
return this._getModuleInfoUnion('info')
102149
}
103150
get meta(): Record<string, any> | undefined {
104-
return this._get('meta')
151+
return this._getModuleObjectUnion('meta')
105152
}
106153
get importers(): Set<ModuleNode> {
107154
return this._getModuleSetUnion('importers')

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

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -982,14 +982,41 @@ class PluginContainer {
982982
}
983983

984984
getModuleInfo(id: string): ModuleInfo | null {
985-
return (
986-
(
987-
this.environments.client as DevEnvironment
988-
).pluginContainer.getModuleInfo(id) ||
989-
(this.environments.ssr as DevEnvironment).pluginContainer.getModuleInfo(
990-
id,
991-
)
992-
)
985+
const clientModuleInfo = (
986+
this.environments.client as DevEnvironment
987+
).pluginContainer.getModuleInfo(id)
988+
const ssrModuleInfo = (
989+
this.environments.ssr as DevEnvironment
990+
).pluginContainer.getModuleInfo(id)
991+
992+
if (clientModuleInfo == null && ssrModuleInfo == null) return null
993+
994+
return new Proxy({} as any, {
995+
get: (_, key: string) => {
996+
// `meta` refers to `ModuleInfo.meta` of both environments, so we also
997+
// need to merge it here
998+
if (key === 'meta') {
999+
const meta: Record<string, any> = {}
1000+
if (ssrModuleInfo) {
1001+
Object.assign(meta, ssrModuleInfo.meta)
1002+
}
1003+
if (clientModuleInfo) {
1004+
Object.assign(meta, clientModuleInfo.meta)
1005+
}
1006+
return meta
1007+
}
1008+
if (clientModuleInfo) {
1009+
if (key in clientModuleInfo) {
1010+
return clientModuleInfo[key as keyof ModuleInfo]
1011+
}
1012+
}
1013+
if (ssrModuleInfo) {
1014+
if (key in ssrModuleInfo) {
1015+
return ssrModuleInfo[key as keyof ModuleInfo]
1016+
}
1017+
}
1018+
},
1019+
})
9931020
}
9941021

9951022
get options(): InputOptions {

0 commit comments

Comments
 (0)