提交 d3002b70 编写于 作者: fxy060608's avatar fxy060608

fix(ssr): 兼容 vite@5

上级 9acfd139
const fs = require('fs')
const path = require('path')
const express = require('express')
async function createServer() {
const resolve = (p) => path.resolve(__dirname, p)
const template = fs.readFileSync(resolve('dist/client/index.html'), 'utf-8')
const manifest = require('./dist/client/ssr-manifest.json')
const app = express()
app.use(require('compression')())
app.use(
require('serve-static')(resolve('dist/client'), {
index: false,
})
)
app.use('*', async (req, res) => {
try {
const url = req.originalUrl
const render = require('./dist/server/entry-server.js').render
const [appHtml, preloadLinks, appContext] = await render(url, manifest)
const html = template
.replace(`<!--preload-links-->`, preloadLinks)
.replace(`<!--app-html-->`, appHtml)
.replace(`<!--app-context-->`, appContext)
res
.status(200)
.set({
'Content-Type': 'text/html',
})
.end(html)
} catch (e) {
res.status(500).end(e.stack)
}
})
return app
}
createServer().then((app) =>
app.listen(3000, () => {
console.log('http://localhost:3000')
})
)
......@@ -5,7 +5,7 @@ const template = fs.readFileSync(
path.join(__dirname, './server/index.html'),
'utf-8'
)
const manifest = require('./server/ssr-manifest.json')
const manifest = require('./client/.vite/ssr-manifest.json')
const render = require('./server/entry-server.js').render
exports.main = async (event) => {
......
......@@ -3,7 +3,7 @@ import debug from 'debug'
import type { Plugin } from 'vite'
import { resolveBuiltIn } from '@dcloudio/uni-cli-shared'
import { ownerModuleName } from '../utils'
import { isSSR, ownerModuleName } from '../utils'
const debugResolve = debug('uni:resolve')
......@@ -12,7 +12,7 @@ export function uniResolveIdPlugin(): Plugin {
return {
name: 'uni:h5-resolve-id',
enforce: 'pre',
config() {
configResolved(config) {
resolveCache[ownerModuleName] = resolveBuiltIn(
path.join(
ownerModuleName,
......@@ -28,10 +28,21 @@ export function uniResolveIdPlugin(): Plugin {
)
)
},
resolveId(id) {
resolveId(id, _, options) {
if (id === 'vue') {
id = '@dcloudio/uni-h5-vue'
}
if (isSSR(options)) {
if (id === '@dcloudio/uni-h5-vue') {
return resolveBuiltIn(
path.join(
'@dcloudio/uni-h5-vue',
(process.env.UNI_APP_X === 'true' ? 'dist-x' : 'dist') +
`/vue.runtime.cjs.js`
)
)
}
}
const cache = resolveCache[id]
if (cache) {
debugResolve('cache', id, cache)
......
......@@ -38,14 +38,8 @@ export function uniSSRPlugin(): Plugin {
alias: [
{
find: 'vue/server-renderer',
replacement: resolveBuiltIn('@vue/server-renderer'),
},
{
find: 'vue',
replacement: resolveBuiltIn(
'@dcloudio/uni-h5-vue/' +
(process.env.UNI_APP_X === 'true' ? 'dist-x' : 'dist') +
'/vue.runtime.esm.js'
replacement: path.dirname(
resolveBuiltIn('@vue/server-renderer')
),
},
],
......
......@@ -6285,7 +6285,9 @@ Server rendered element contains more child nodes than client vdom.`
if (props) {
{
for (const key in props) {
if (propHasMismatch(el, key, props[key], vnode, parentComponent)) {
if (// fixed by xxxxxx 暂时不对比style,因为服务器style会rpx2unit,而客户端是在setStyle的时候处理的,此时还没有格式化
// 后续考虑调整客户端rpx2unit的时机,提前到vnode处
key !== "style" && propHasMismatch(el, key, props[key], vnode, parentComponent)) {
hasMismatch = true;
}
if (forcePatch && (key.endsWith("value") || key === "indeterminate") || shared.isOn(key) && !shared.isReservedProp(key) || // force hydrate v-bind with .prop modifiers
......
......@@ -6293,7 +6293,9 @@ Server rendered element contains more child nodes than client vdom.`
if (props) {
if (!!(process.env.NODE_ENV !== "production") || forcePatch || !optimized || patchFlag & (16 | 32)) {
for (const key in props) {
if (!!(process.env.NODE_ENV !== "production") && propHasMismatch(el, key, props[key], vnode, parentComponent)) {
if (!!(process.env.NODE_ENV !== "production") && // fixed by xxxxxx 暂时不对比style,因为服务器style会rpx2unit,而客户端是在setStyle的时候处理的,此时还没有格式化
// 后续考虑调整客户端rpx2unit的时机,提前到vnode处
key !== "style" && propHasMismatch(el, key, props[key], vnode, parentComponent)) {
hasMismatch = true;
}
if (forcePatch && (key.endsWith("value") || key === "indeterminate") || isOn(key) && !isReservedProp(key) || // force hydrate v-bind with .prop modifiers
......
......@@ -6241,7 +6241,9 @@ Server rendered element contains more child nodes than client vdom.`
if (props) {
{
for (const key in props) {
if (propHasMismatch(el, key, props[key], vnode, parentComponent)) {
if (// fixed by xxxxxx 暂时不对比style,因为服务器style会rpx2unit,而客户端是在setStyle的时候处理的,此时还没有格式化
// 后续考虑调整客户端rpx2unit的时机,提前到vnode处
key !== "style" && propHasMismatch(el, key, props[key], vnode, parentComponent)) {
hasMismatch = true;
}
if (forcePatch && (key.endsWith("value") || key === "indeterminate") || shared.isOn(key) && !shared.isReservedProp(key) || // force hydrate v-bind with .prop modifiers
......
......@@ -6249,7 +6249,9 @@ Server rendered element contains more child nodes than client vdom.`
if (props) {
if (!!(process.env.NODE_ENV !== "production") || forcePatch || !optimized || patchFlag & (16 | 32)) {
for (const key in props) {
if (!!(process.env.NODE_ENV !== "production") && propHasMismatch(el, key, props[key], vnode, parentComponent)) {
if (!!(process.env.NODE_ENV !== "production") && // fixed by xxxxxx 暂时不对比style,因为服务器style会rpx2unit,而客户端是在setStyle的时候处理的,此时还没有格式化
// 后续考虑调整客户端rpx2unit的时机,提前到vnode处
key !== "style" && propHasMismatch(el, key, props[key], vnode, parentComponent)) {
hasMismatch = true;
}
if (forcePatch && (key.endsWith("value") || key === "indeterminate") || isOn(key) && !isReservedProp(key) || // force hydrate v-bind with .prop modifiers
......
......@@ -46,6 +46,12 @@ export async function buildSSR(options: CliOptions) {
process.env.UNI_INPUT_DIR,
'entry-server.js'
)
// 强制 cjs 输出
ssrBuildServerOptions.rollupOptions = {
output: {
format: 'cjs',
},
}
ssrBuildServerOptions.outDir = process.env.UNI_OUTPUT_DIR
process.env.UNI_SSR_CLIENT = ''
process.env.UNI_SSR_SERVER = 'true'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册