nuxt-gin-tools 0.2.10 → 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 +14 -48
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,50 +24,18 @@ exports.createDefaultConfig = createDefaultConfig;
|
|
|
24
24
|
* });
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
|
-
function createDefaultConfig({ serverConfig
|
|
28
|
-
/**
|
|
29
|
-
* 处理基础路径,去除协议和域名部分,只保留路径部分
|
|
30
|
-
* 例如,将 "https://example.com/api-go" 转换为 "/api-go"
|
|
31
|
-
*/
|
|
32
|
-
const thisBasePath = apiBasePath.replace(/^https?:[/]{2}[^/]+/, "");
|
|
33
|
-
const normalizedProxyBasePath = thisBasePath.endsWith("/")
|
|
34
|
-
? thisBasePath.slice(0, -1)
|
|
35
|
-
: thisBasePath;
|
|
36
|
-
const normalizeBasePath = (rawPath) => {
|
|
37
|
-
const trimmed = rawPath.replace(/^https?:[/]{2}[^/]+/, "");
|
|
38
|
-
if (!trimmed)
|
|
39
|
-
return "/";
|
|
40
|
-
if (trimmed === "/")
|
|
41
|
-
return "/";
|
|
42
|
-
return trimmed.endsWith("/") ? trimmed.slice(0, -1) : trimmed;
|
|
43
|
-
};
|
|
27
|
+
function createDefaultConfig({ serverConfig }) {
|
|
44
28
|
// Proxy target should point to backend origin only.
|
|
45
29
|
// The matched route prefix (e.g. /api-go/v1, /ws-go) is preserved by proxy itself.
|
|
46
30
|
// If target also appends basePath, final upstream path becomes duplicated and can
|
|
47
31
|
// break websocket upgrades with ECONNRESET.
|
|
48
32
|
const buildTarget = () => `http://127.0.0.1:${serverConfig.ginPort}`;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const target = buildTarget();
|
|
56
|
-
const proxyEntries = {
|
|
57
|
-
[normalizedProxyBasePath]: {
|
|
58
|
-
target,
|
|
59
|
-
changeOrigin: true,
|
|
60
|
-
ws: true,
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
for (const rawPath of wsProxyBasePaths) {
|
|
64
|
-
const normalizedPath = normalizeBasePath(rawPath);
|
|
65
|
-
proxyEntries[normalizedPath] = {
|
|
66
|
-
target: buildTarget(),
|
|
67
|
-
changeOrigin: true,
|
|
68
|
-
ws: true,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
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)}(?:/|$)).*`;
|
|
71
39
|
return {
|
|
72
40
|
buildDir: "vue/.nuxt", // 设置构建目录为 "vue/.nuxt",表示 Nuxt 项目的构建输出将存放在该目录下
|
|
73
41
|
srcDir: "vue", // 设置源代码目录为 "vue",表示 Nuxt 项目的源代码将存放在该目录下
|
|
@@ -109,17 +77,15 @@ function createDefaultConfig({ serverConfig, apiBasePath, wsProxyBasePaths = [],
|
|
|
109
77
|
maxAge: "1728000",
|
|
110
78
|
},
|
|
111
79
|
},
|
|
112
|
-
nitro: {
|
|
113
|
-
output: {
|
|
114
|
-
// 设置输出目录为 "vue/.nuxt", 表示 Nitro 的构建输出
|
|
115
|
-
dir: "vue/.output",
|
|
116
|
-
},
|
|
117
|
-
// 定义代理规则,将匹配基础路径的请求代理到目标服务器
|
|
118
|
-
devProxy: proxyEntries,
|
|
119
|
-
},
|
|
120
80
|
vite: {
|
|
121
81
|
server: {
|
|
122
|
-
proxy:
|
|
82
|
+
proxy: {
|
|
83
|
+
[nonBaseUrlProxyPattern]: {
|
|
84
|
+
target: buildTarget(),
|
|
85
|
+
changeOrigin: true,
|
|
86
|
+
ws: true,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
123
89
|
},
|
|
124
90
|
// 配置 Vite 插件
|
|
125
91
|
plugins: [],
|