@lark-apaas/coding-templates 0.1.43-alpha.1 → 0.1.43-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,5 +1,56 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
|
+
import type { IncomingMessage, ServerResponse } from 'http'
|
|
2
3
|
import { defineConfig } from '@lark-apaas/coding-vite-preset'
|
|
4
|
+
import type { Plugin, ViteDevServer } from 'vite'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* dev 模式 base-path normalize 中间件
|
|
8
|
+
*
|
|
9
|
+
* 沙箱预览网关把流量原样转给 vite(保留 /app/:appId 前缀)。浏览器首次访问的
|
|
10
|
+
* URL 形如 `/app/app_xxx?sso_token=...`,pathname 缺末尾 /。
|
|
11
|
+
*
|
|
12
|
+
* vite 8 的 baseMiddleware 对"缺末尾 /"的请求不再像 vite 5 那样自动 301,
|
|
13
|
+
* 而是直接返回 404 警告页 "The server is configured with a public base URL..."。
|
|
14
|
+
*
|
|
15
|
+
* 这里做 internal rewrite(不发 301,避免跟沙箱网关形成 round-trip 死循环):
|
|
16
|
+
* 在 vite 内置 baseMiddleware **之前** unshift 一个中间件,把 req.url 改写
|
|
17
|
+
* 成带尾 /,下游 baseMiddleware 继续 strip base → SPA fallback。
|
|
18
|
+
* 浏览器全程不感知 redirect,地址栏可保留缺尾 / 形态(BrowserRouter basename
|
|
19
|
+
* 处理路由)。
|
|
20
|
+
*
|
|
21
|
+
* 必须 stack.unshift 而非 server.middlewares.use(...),否则会排到内置
|
|
22
|
+
* baseMiddleware 之后失效。
|
|
23
|
+
*
|
|
24
|
+
* TODO: 这段已同步到 @lark-apaas/coding-vite-preset,待该包发布并被本模板
|
|
25
|
+
* 依赖升级拉到后,可以删除此 plugin。保留是为了让现存沙箱(用着老 preset
|
|
26
|
+
* 的应用)也能立即生效。
|
|
27
|
+
*/
|
|
28
|
+
const baseNormalizePlugin: Plugin = {
|
|
29
|
+
name: 'base-path-normalize',
|
|
30
|
+
configureServer(server: ViteDevServer) {
|
|
31
|
+
const base = (process.env.CLIENT_BASE_PATH || '').replace(/\/+$/, '')
|
|
32
|
+
if (!base) return
|
|
33
|
+
const stack = (
|
|
34
|
+
server.middlewares as unknown as {
|
|
35
|
+
stack: Array<{ route: string; handle: unknown }>
|
|
36
|
+
}
|
|
37
|
+
).stack
|
|
38
|
+
if (!Array.isArray(stack)) return
|
|
39
|
+
stack.unshift({
|
|
40
|
+
route: '',
|
|
41
|
+
handle: (req: IncomingMessage, _res: ServerResponse, next: () => void) => {
|
|
42
|
+
const url = req.url || ''
|
|
43
|
+
const qIdx = url.indexOf('?')
|
|
44
|
+
const pathname = qIdx >= 0 ? url.slice(0, qIdx) : url
|
|
45
|
+
const search = qIdx >= 0 ? url.slice(qIdx) : ''
|
|
46
|
+
if (pathname === base) {
|
|
47
|
+
req.url = base + '/' + search
|
|
48
|
+
}
|
|
49
|
+
next()
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
},
|
|
53
|
+
}
|
|
3
54
|
|
|
4
55
|
export default defineConfig({
|
|
5
56
|
root: path.resolve(__dirname, 'client'),
|
|
@@ -9,4 +60,5 @@ export default defineConfig({
|
|
|
9
60
|
'@shared': path.resolve(__dirname, 'shared'),
|
|
10
61
|
},
|
|
11
62
|
},
|
|
63
|
+
plugins: [baseNormalizePlugin],
|
|
12
64
|
})
|