nuxt-gin-tools 0.2.7 → 0.2.9
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 +7 -1
- package/src/nuxt-config.js +29 -20
package/package.json
CHANGED
package/src/nuxt-config.d.ts
CHANGED
|
@@ -25,6 +25,12 @@ export interface MyNuxtConfig {
|
|
|
25
25
|
* 例如,如果服务器端路由为 /api,则 serverBasePath 为 /api
|
|
26
26
|
*/
|
|
27
27
|
apiBasePath: string;
|
|
28
|
+
/**
|
|
29
|
+
* WebSocket 代理基础路径列表(可选)
|
|
30
|
+
* 例如:["/ws-go", "/socket"]
|
|
31
|
+
* 会同时注入 Nitro devProxy 与 Vite proxy,默认启用 ws。
|
|
32
|
+
*/
|
|
33
|
+
wsProxyBasePaths?: string[];
|
|
28
34
|
/**
|
|
29
35
|
* 服务器配置
|
|
30
36
|
* 包含服务器端的相关配置,如端口号、基础路径等
|
|
@@ -54,4 +60,4 @@ export interface MyNuxtConfig {
|
|
|
54
60
|
* });
|
|
55
61
|
* ```
|
|
56
62
|
*/
|
|
57
|
-
export function createDefaultConfig(
|
|
63
|
+
export function createDefaultConfig(config: MyNuxtConfig): NuxtConfig;
|
package/src/nuxt-config.js
CHANGED
|
@@ -24,7 +24,7 @@ exports.createDefaultConfig = createDefaultConfig;
|
|
|
24
24
|
* });
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
|
-
function createDefaultConfig({ serverConfig, apiBasePath }) {
|
|
27
|
+
function createDefaultConfig({ serverConfig, apiBasePath, wsProxyBasePaths = [], }) {
|
|
28
28
|
/**
|
|
29
29
|
* 处理基础路径,去除协议和域名部分,只保留路径部分
|
|
30
30
|
* 例如,将 "https://example.com/api-go" 转换为 "/api-go"
|
|
@@ -33,13 +33,37 @@ function createDefaultConfig({ serverConfig, apiBasePath }) {
|
|
|
33
33
|
const normalizedProxyBasePath = thisBasePath.endsWith("/")
|
|
34
34
|
? thisBasePath.slice(0, -1)
|
|
35
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
|
+
};
|
|
44
|
+
const buildTarget = (basePath) => `http://localhost:${serverConfig.ginPort}${basePath}`;
|
|
36
45
|
/**
|
|
37
46
|
* 目标服务器的 URL
|
|
38
47
|
* 格式为:http://localhost:ginPort/serverBasePath
|
|
39
48
|
* 其中,ginPort 是从 serverConfig 中获取的 Gin 服务器的端口号
|
|
40
49
|
* serverBasePath 是从 MyNuxtConfig 中获取的服务器基础路径
|
|
41
50
|
*/
|
|
42
|
-
const target =
|
|
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
|
+
}
|
|
43
67
|
return {
|
|
44
68
|
buildDir: "vue/.nuxt", // 设置构建目录为 "vue/.nuxt",表示 Nuxt 项目的构建输出将存放在该目录下
|
|
45
69
|
srcDir: "vue", // 设置源代码目录为 "vue",表示 Nuxt 项目的源代码将存放在该目录下
|
|
@@ -86,27 +110,12 @@ function createDefaultConfig({ serverConfig, apiBasePath }) {
|
|
|
86
110
|
// 设置输出目录为 "vue/.nuxt", 表示 Nitro 的构建输出
|
|
87
111
|
dir: "vue/.output",
|
|
88
112
|
},
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
[normalizedProxyBasePath]: {
|
|
92
|
-
// 目标服务器的 URL
|
|
93
|
-
target: target,
|
|
94
|
-
// 是否改变请求的源,设置为 true 可以避免跨域问题
|
|
95
|
-
changeOrigin: true,
|
|
96
|
-
// 启用 WebSocket 代理
|
|
97
|
-
ws: true,
|
|
98
|
-
},
|
|
99
|
-
},
|
|
113
|
+
// 定义代理规则,将匹配基础路径的请求代理到目标服务器
|
|
114
|
+
devProxy: proxyEntries,
|
|
100
115
|
},
|
|
101
116
|
vite: {
|
|
102
117
|
server: {
|
|
103
|
-
proxy:
|
|
104
|
-
[normalizedProxyBasePath]: {
|
|
105
|
-
target,
|
|
106
|
-
changeOrigin: true,
|
|
107
|
-
ws: true,
|
|
108
|
-
},
|
|
109
|
-
},
|
|
118
|
+
proxy: proxyEntries,
|
|
110
119
|
},
|
|
111
120
|
// 配置 Vite 插件
|
|
112
121
|
plugins: [],
|