nuxt-gin-tools 0.2.6 → 0.2.8

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/index.js CHANGED
@@ -1,5 +1,38 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
3
36
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
37
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
38
  };
@@ -8,7 +41,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
8
41
  // 导入构建和打包功能
9
42
  const pack_1 = __importDefault(require("./src/pack"));
10
43
  // 导入开发模式功能
11
- const develop_1 = __importDefault(require("./src/develop"));
44
+ const develop_1 = __importStar(require("./src/develop"));
12
45
  // 导入API生成功能
13
46
  const api_generate_1 = __importDefault(require("./src/api-generate"));
14
47
  // 导入安装后处理功能
@@ -30,6 +63,14 @@ switch (args[0]) {
30
63
  // 启动开发模式
31
64
  (0, develop_1.default)();
32
65
  break;
66
+ case "dev:nuxt":
67
+ // 仅启动 Nuxt 开发模式
68
+ (0, develop_1.developNuxt)();
69
+ break;
70
+ case "dev:go":
71
+ // 仅启动 Go 开发模式
72
+ (0, develop_1.developGo)();
73
+ break;
33
74
  case "build":
34
75
  // 执行构建和打包操作
35
76
  (0, pack_1.default)();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-gin-tools",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "This project is used as a dependency for [nuxt-gin-starter](https://github.com/RapboyGao/nuxt-gin-starter.git)",
5
5
  "bin": {
6
6
  "nuxt-gin": "index.js"
package/src/develop.d.ts CHANGED
@@ -6,4 +6,16 @@
6
6
  * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
7
7
  */
8
8
  export declare function develop(): Promise<void>;
9
+ /**
10
+ * 仅启动 Nuxt 开发服务(带 nuxt 标签输出)。
11
+ *
12
+ * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
13
+ */
14
+ export declare function developNuxt(): Promise<void>;
15
+ /**
16
+ * 仅启动 Go 开发监听流程。
17
+ *
18
+ * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
19
+ */
20
+ export declare function developGo(): Promise<void>;
9
21
  export default develop;
package/src/develop.js CHANGED
@@ -13,6 +13,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.develop = develop;
16
+ exports.developNuxt = developNuxt;
17
+ exports.developGo = developGo;
16
18
  const concurrently_1 = __importDefault(require("concurrently"));
17
19
  const fs_extra_1 = require("fs-extra");
18
20
  const path_1 = require("path");
@@ -22,6 +24,37 @@ const dev_go_1 = require("./dev-go");
22
24
  const utils_1 = require("./utils");
23
25
  const cwd = process.cwd();
24
26
  const serverConfig = (0, fs_extra_1.readJSONSync)((0, path_1.join)(cwd, "server.config.json"));
27
+ function prepareDevelop() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ const cleanupBeforeDevelop = serverConfig.cleanupBeforeDevelop === true;
30
+ if (cleanupBeforeDevelop) {
31
+ yield (0, cleanup_1.default)();
32
+ yield (0, postinstall_1.default)();
33
+ return;
34
+ }
35
+ if (!(0, fs_extra_1.existsSync)((0, path_1.join)(cwd, "vue/.nuxt")) || !(0, fs_extra_1.existsSync)((0, path_1.join)(cwd, "go.sum"))) {
36
+ yield (0, cleanup_1.default)();
37
+ yield (0, postinstall_1.default)();
38
+ }
39
+ });
40
+ }
41
+ function runNuxtDev() {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ yield (0, concurrently_1.default)([
44
+ {
45
+ command: `npx nuxt dev --port=${serverConfig.nuxtPort} --host`,
46
+ name: "nuxt",
47
+ prefixColor: "blue",
48
+ },
49
+ ]).result;
50
+ });
51
+ }
52
+ function runGoDev() {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ (0, fs_extra_1.ensureDirSync)((0, path_1.join)(cwd, ".build/.server"));
55
+ yield (0, dev_go_1.startGoDev)();
56
+ });
57
+ }
25
58
  /**
26
59
  * 启动本地开发环境。
27
60
  *
@@ -31,35 +64,43 @@ const serverConfig = (0, fs_extra_1.readJSONSync)((0, path_1.join)(cwd, "server.
31
64
  */
32
65
  function develop() {
33
66
  return __awaiter(this, void 0, void 0, function* () {
34
- const cleanupBeforeDevelop = serverConfig.cleanupBeforeDevelop === true;
35
67
  const killPortBeforeDevelop = serverConfig.killPortBeforeDevelop !== false;
36
- // 如果配置为开发前清理,则直接执行清理和安装后处理
37
- if (cleanupBeforeDevelop) {
38
- yield (0, cleanup_1.default)();
39
- yield (0, postinstall_1.default)();
40
- }
41
- else {
42
- // 否则仅在关键依赖缺失时执行
43
- if (!(0, fs_extra_1.existsSync)((0, path_1.join)(cwd, "vue/.nuxt")) || !(0, fs_extra_1.existsSync)((0, path_1.join)(cwd, "go.sum"))) {
44
- yield (0, cleanup_1.default)();
45
- yield (0, postinstall_1.default)();
46
- }
47
- }
68
+ yield prepareDevelop();
48
69
  // 在开发前确保占用端口被释放
49
70
  if (killPortBeforeDevelop) {
50
71
  (0, utils_1.killPorts)([serverConfig.ginPort, serverConfig.nuxtPort]);
51
72
  }
52
- (0, fs_extra_1.ensureDirSync)((0, path_1.join)(cwd, ".build/.server"));
53
- // Nuxt 保持在 concurrently 中运行,统一复用 nuxt 标签输出。
54
- const nuxtTask = (0, concurrently_1.default)([
55
- {
56
- command: `npx nuxt dev --port=${serverConfig.nuxtPort} --host`,
57
- name: "nuxt",
58
- prefixColor: "blue",
59
- },
60
- ]).result;
61
- // Go 侧直接调用本地 dev-go 逻辑,避免额外 shell 路径和引号问题。
62
- yield Promise.all([(0, dev_go_1.startGoDev)(), nuxtTask]);
73
+ yield Promise.all([runGoDev(), runNuxtDev()]);
74
+ });
75
+ }
76
+ /**
77
+ * 仅启动 Nuxt 开发服务(带 nuxt 标签输出)。
78
+ *
79
+ * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
80
+ */
81
+ function developNuxt() {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const killPortBeforeDevelop = serverConfig.killPortBeforeDevelop !== false;
84
+ yield prepareDevelop();
85
+ if (killPortBeforeDevelop) {
86
+ (0, utils_1.killPorts)([serverConfig.nuxtPort]);
87
+ }
88
+ yield runNuxtDev();
89
+ });
90
+ }
91
+ /**
92
+ * 仅启动 Go 开发监听流程。
93
+ *
94
+ * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
95
+ */
96
+ function developGo() {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ const killPortBeforeDevelop = serverConfig.killPortBeforeDevelop !== false;
99
+ yield prepareDevelop();
100
+ if (killPortBeforeDevelop) {
101
+ (0, utils_1.killPorts)([serverConfig.ginPort]);
102
+ }
103
+ yield runGoDev();
63
104
  });
64
105
  }
65
106
  exports.default = develop;
@@ -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,67 @@ export interface MyNuxtConfig {
54
60
  * });
55
61
  * ```
56
62
  */
57
- export function createDefaultConfig({ serverConfig, apiBasePath }: MyNuxtConfig): NuxtConfig;
63
+ export function createDefaultConfig(config: MyNuxtConfig): NuxtConfig;
64
+ srcDir: string;
65
+ ssr: boolean;
66
+ /**
67
+ * 配置实验性功能
68
+ * 禁用 payloadExtraction 功能,该功能可能用于提取页面的有效负载数据
69
+ * 这里禁用它可能是为了避免某些兼容性问题或特定的项目需求
70
+ */
71
+ experimental: {
72
+ payloadExtraction: boolean;
73
+ };
74
+ devtools: {
75
+ timeline: {
76
+ enabled: boolean;
77
+ };
78
+ };
79
+ /**
80
+ * 配置应用的基础 URL
81
+ * 从 server.config.json 文件中获取 baseUrl 作为应用的基础 URL
82
+ * 这个基础 URL 将用于构建应用的路由和请求地址
83
+ */
84
+ app: {
85
+ baseURL: string;
86
+ };
87
+ /**
88
+ * 配置开发服务器的端口号
89
+ * 从 server.config.json 文件中获取 nuxtPort 作为开发服务器的端口号
90
+ * 启动开发服务器时,将使用该端口号进行监听
91
+ */
92
+ devServer: {
93
+ port: number;
94
+ cors: {
95
+ origin: string;
96
+ methods: string[];
97
+ allowHeaders: string[];
98
+ credentials: boolean;
99
+ maxAge: string;
100
+ };
101
+ };
102
+ nitro: {
103
+ output: {
104
+ dir: string;
105
+ };
106
+ devProxy: Record<string, {
107
+ target: string;
108
+ changeOrigin: boolean;
109
+ ws: boolean;
110
+ }>;
111
+ };
112
+ vite: {
113
+ server: {
114
+ proxy: Record<string, {
115
+ target: string;
116
+ changeOrigin: boolean;
117
+ ws: boolean;
118
+ }>;
119
+ };
120
+ plugins: never[];
121
+ esbuild: {
122
+ jsxFactory: string;
123
+ jsxFragment: string;
124
+ };
125
+ };
126
+ };
@@ -24,19 +24,46 @@ 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"
31
31
  */
32
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
+ };
44
+ const buildTarget = (basePath) => `http://localhost:${serverConfig.ginPort}${basePath}`;
33
45
  /**
34
46
  * 目标服务器的 URL
35
47
  * 格式为:http://localhost:ginPort/serverBasePath
36
48
  * 其中,ginPort 是从 serverConfig 中获取的 Gin 服务器的端口号
37
49
  * serverBasePath 是从 MyNuxtConfig 中获取的服务器基础路径
38
50
  */
39
- const target = `http://localhost:${serverConfig.ginPort}${thisBasePath}`;
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
+ }
40
67
  return {
41
68
  buildDir: "vue/.nuxt", // 设置构建目录为 "vue/.nuxt",表示 Nuxt 项目的构建输出将存放在该目录下
42
69
  srcDir: "vue", // 设置源代码目录为 "vue",表示 Nuxt 项目的源代码将存放在该目录下
@@ -83,17 +110,13 @@ function createDefaultConfig({ serverConfig, apiBasePath }) {
83
110
  // 设置输出目录为 "vue/.nuxt", 表示 Nitro 的构建输出
84
111
  dir: "vue/.output",
85
112
  },
86
- devProxy: {
87
- // 定义代理规则,将匹配 thisBasePath 的请求代理到目标服务器
88
- [thisBasePath]: {
89
- // 目标服务器的 URL
90
- target: target,
91
- // 是否改变请求的源,设置为 true 可以避免跨域问题
92
- changeOrigin: true,
93
- },
94
- },
113
+ // 定义代理规则,将匹配基础路径的请求代理到目标服务器
114
+ devProxy: proxyEntries,
95
115
  },
96
116
  vite: {
117
+ server: {
118
+ proxy: proxyEntries,
119
+ },
97
120
  // 配置 Vite 插件
98
121
  plugins: [],
99
122
  // 配置 esbuild 编译器的选项