Skip to content

Commit 4a03a1c

Browse files
committed
feat(uni-app-x web): 修复ssr时依赖查找错误的Bug,增加page-meta用于设置head
1 parent 9876a96 commit 4a03a1c

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<template>
2+
<teleport to="head">
3+
<slot />
4+
</teleport>
5+
</template>
6+
<script>
7+
export default {
8+
name: 'PageMetaHead',
9+
}
10+
</script>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<view style="display: none;">
3+
<slot />
4+
</view>
5+
</template>
6+
<script>
7+
export default {
8+
name: 'PageMeta',
9+
setup(props, { emit }) {
10+
}
11+
}
12+
</script>

packages/uni-h5-vite/src/utils/ssr.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ const SSR_ALIAS: Record<string, string> = {
4444
'@dcloudio/uni-i18n': '@dcloudio/uni-i18n',
4545
'@dcloudio/uni-shared': '@dcloudio/uni-shared',
4646
}
47+
const SSR_DIST_X_ALIAS_KEY = ['uni-h5-vue', 'uni-h5']
4748
export const initSsrAliasOnce = once(() => {
4849
// 重写 package.json 的读取
4950
const oldJoin = path.join
51+
const oldReadFileSync = fs.readFileSync
5052
const alias = Object.keys(SSR_ALIAS).reduce((alias, key) => {
5153
const newKey = oldJoin('node_modules', key, 'package.json')
5254
if (key.endsWith('vue/server-renderer')) {
@@ -70,6 +72,37 @@ export const initSsrAliasOnce = once(() => {
7072
}
7173
return res
7274
}
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(/dist\//, 'dist-x/')
97+
}
98+
if (pkgData.main) {
99+
pkgData.main = pkgData.main.replace(/dist\//, 'dist-x/')
100+
}
101+
return JSON.stringify(pkgData, null, 2)
102+
}
103+
return res
104+
}
105+
}
73106
})
74107

75108
export function initSsrDefine(config: ResolvedConfig) {

0 commit comments

Comments
 (0)