@@ -44,9 +44,11 @@ const SSR_ALIAS: Record<string, string> = {
44
44
'@dcloudio/uni-i18n' : '@dcloudio/uni-i18n' ,
45
45
'@dcloudio/uni-shared' : '@dcloudio/uni-shared' ,
46
46
}
47
+ const SSR_DIST_X_ALIAS_KEY = [ 'uni-h5-vue' , 'uni-h5' ]
47
48
export const initSsrAliasOnce = once ( ( ) => {
48
49
// 重写 package.json 的读取
49
50
const oldJoin = path . join
51
+ const oldReadFileSync = fs . readFileSync
50
52
const alias = Object . keys ( SSR_ALIAS ) . reduce ( ( alias , key ) => {
51
53
const newKey = oldJoin ( 'node_modules' , key , 'package.json' )
52
54
if ( key . endsWith ( 'vue/server-renderer' ) ) {
@@ -70,6 +72,37 @@ export const initSsrAliasOnce = once(() => {
70
72
}
71
73
return res
72
74
}
75
+ if ( process . env . UNI_APP_X === 'true' ) {
76
+ // @ts -expect-error
77
+ fs . readFileSync = ( ...args : any [ ] ) => {
78
+ // @ts -expect-error
79
+ const res = oldReadFileSync . apply ( fs , args )
80
+ if (
81
+ ! (
82
+ args . length === 2 &&
83
+ typeof args [ 0 ] === 'string' &&
84
+ ( args [ 1 ] === 'utf-8' || args [ 1 ] === 'utf8' )
85
+ )
86
+ ) {
87
+ return res
88
+ }
89
+ const path = normalizePath ( args [ 0 ] )
90
+ const shouldRedirectToDistX = SSR_DIST_X_ALIAS_KEY . some ( ( key ) =>
91
+ path . endsWith ( key + '/package.json' )
92
+ )
93
+ if ( shouldRedirectToDistX ) {
94
+ const pkgData = JSON . parse ( res as string )
95
+ if ( pkgData . module ) {
96
+ pkgData . module = pkgData . module . replace ( / d i s t \/ / , 'dist-x/' )
97
+ }
98
+ if ( pkgData . main ) {
99
+ pkgData . main = pkgData . main . replace ( / d i s t \/ / , 'dist-x/' )
100
+ }
101
+ return JSON . stringify ( pkgData , null , 2 )
102
+ }
103
+ return res
104
+ }
105
+ }
73
106
} )
74
107
75
108
export function initSsrDefine ( config : ResolvedConfig ) {
0 commit comments