nuxt-gin-tools 0.2.9 → 0.2.11
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 +1 -1
- package/src/nuxt-config.d.ts +0 -6
- package/src/nuxt-config.js +19 -49
package/package.json
CHANGED
package/src/nuxt-config.d.ts
CHANGED
|
@@ -19,12 +19,6 @@ export interface ServerConfigJson {
|
|
|
19
19
|
ginPort: number;
|
|
20
20
|
}
|
|
21
21
|
export interface MyNuxtConfig {
|
|
22
|
-
/**
|
|
23
|
-
* 服务器基础路径
|
|
24
|
-
* 用于指定服务器端路由的基础路径
|
|
25
|
-
* 例如,如果服务器端路由为 /api,则 serverBasePath 为 /api
|
|
26
|
-
*/
|
|
27
|
-
apiBasePath: string;
|
|
28
22
|
/**
|
|
29
23
|
* WebSocket 代理基础路径列表(可选)
|
|
30
24
|
* 例如:["/ws-go", "/socket"]
|
package/src/nuxt-config.js
CHANGED
|
@@ -24,46 +24,18 @@ exports.createDefaultConfig = createDefaultConfig;
|
|
|
24
24
|
* });
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
|
-
function createDefaultConfig({ serverConfig
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
?
|
|
35
|
-
:
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return "/";
|
|
40
|
-
if (trimmed === "/")
|
|
41
|
-
return "/";
|
|
42
|
-
return trimmed.endsWith("/") ? trimmed.slice(0, -1) : trimmed;
|
|
43
|
-
};
|
|
44
|
-
const buildTarget = (basePath) => `http://localhost:${serverConfig.ginPort}${basePath}`;
|
|
45
|
-
/**
|
|
46
|
-
* 目标服务器的 URL
|
|
47
|
-
* 格式为:http://localhost:ginPort/serverBasePath
|
|
48
|
-
* 其中,ginPort 是从 serverConfig 中获取的 Gin 服务器的端口号
|
|
49
|
-
* serverBasePath 是从 MyNuxtConfig 中获取的服务器基础路径
|
|
50
|
-
*/
|
|
51
|
-
const target = buildTarget(normalizedProxyBasePath);
|
|
52
|
-
const proxyEntries = {
|
|
53
|
-
[normalizedProxyBasePath]: {
|
|
54
|
-
target,
|
|
55
|
-
changeOrigin: true,
|
|
56
|
-
ws: true,
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
for (const rawPath of wsProxyBasePaths) {
|
|
60
|
-
const normalizedPath = normalizeBasePath(rawPath);
|
|
61
|
-
proxyEntries[normalizedPath] = {
|
|
62
|
-
target: buildTarget(normalizedPath),
|
|
63
|
-
changeOrigin: true,
|
|
64
|
-
ws: true,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
27
|
+
function createDefaultConfig({ serverConfig }) {
|
|
28
|
+
// Proxy target should point to backend origin only.
|
|
29
|
+
// The matched route prefix (e.g. /api-go/v1, /ws-go) is preserved by proxy itself.
|
|
30
|
+
// If target also appends basePath, final upstream path becomes duplicated and can
|
|
31
|
+
// break websocket upgrades with ECONNRESET.
|
|
32
|
+
const buildTarget = () => `http://127.0.0.1:${serverConfig.ginPort}`;
|
|
33
|
+
const normalizedBaseUrl = serverConfig.baseUrl.endsWith("/")
|
|
34
|
+
? serverConfig.baseUrl.slice(0, -1)
|
|
35
|
+
: serverConfig.baseUrl;
|
|
36
|
+
const escapedBaseUrl = normalizedBaseUrl.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
37
|
+
// 匹配所有“不以 baseUrl 开头”的路径,例如 baseUrl=/vue 时会匹配 /api、/ws、/health 等。
|
|
38
|
+
const nonBaseUrlProxyPattern = `^/(?!${escapedBaseUrl.slice(1)}(?:/|$)).*`;
|
|
67
39
|
return {
|
|
68
40
|
buildDir: "vue/.nuxt", // 设置构建目录为 "vue/.nuxt",表示 Nuxt 项目的构建输出将存放在该目录下
|
|
69
41
|
srcDir: "vue", // 设置源代码目录为 "vue",表示 Nuxt 项目的源代码将存放在该目录下
|
|
@@ -105,17 +77,15 @@ function createDefaultConfig({ serverConfig, apiBasePath, wsProxyBasePaths = [],
|
|
|
105
77
|
maxAge: "1728000",
|
|
106
78
|
},
|
|
107
79
|
},
|
|
108
|
-
nitro: {
|
|
109
|
-
output: {
|
|
110
|
-
// 设置输出目录为 "vue/.nuxt", 表示 Nitro 的构建输出
|
|
111
|
-
dir: "vue/.output",
|
|
112
|
-
},
|
|
113
|
-
// 定义代理规则,将匹配基础路径的请求代理到目标服务器
|
|
114
|
-
devProxy: proxyEntries,
|
|
115
|
-
},
|
|
116
80
|
vite: {
|
|
117
81
|
server: {
|
|
118
|
-
proxy:
|
|
82
|
+
proxy: {
|
|
83
|
+
[nonBaseUrlProxyPattern]: {
|
|
84
|
+
target: buildTarget(),
|
|
85
|
+
changeOrigin: true,
|
|
86
|
+
ws: true,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
119
89
|
},
|
|
120
90
|
// 配置 Vite 插件
|
|
121
91
|
plugins: [],
|