pug-site-core 2.0.17 → 2.0.18

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/lib/devServer.js CHANGED
@@ -3,6 +3,7 @@ import useragent from "express-useragent";
3
3
  import ip from "ip";
4
4
  import fse from "fs-extra";
5
5
  import _ from "lodash";
6
+ import { createProxyMiddleware } from "http-proxy-middleware";
6
7
  import {
7
8
  getCompilePugFilter,
8
9
  getIdleProt,
@@ -48,6 +49,69 @@ function setupMiddleware(app) {
48
49
  app.use("/static", express.static(paths.template.static));
49
50
  app.use(express.static(paths.public));
50
51
  app.locals.basedir = paths.template.root;
52
+
53
+ // 设置代理
54
+ setupProxy(app);
55
+ }
56
+
57
+ // 配置代理中间件
58
+ function setupProxy(app) {
59
+ const proxyConfig = config.devServer.proxy || {};
60
+
61
+ // 如果没有配置proxy或配置为空对象,则不执行代理设置
62
+ if (!proxyConfig || Object.keys(proxyConfig).length === 0) {
63
+ return;
64
+ }
65
+
66
+ // 遍历代理配置
67
+ Object.keys(proxyConfig).forEach((context) => {
68
+ let options = proxyConfig[context];
69
+
70
+ // 如果配置是字符串,转换为对象格式
71
+ if (typeof options === "string") {
72
+ options = {
73
+ target: options,
74
+ changeOrigin: true,
75
+ };
76
+ }
77
+
78
+ // 检查是否有target配置
79
+ if (!options || !options.target) {
80
+ console.error(`错误: 代理配置路径 "${context}" 缺少必要的 "target" 选项`);
81
+ return; // 跳过此配置
82
+ }
83
+
84
+ try {
85
+ // 创建代理中间件
86
+ const proxy = createProxyMiddleware({
87
+ target: options.target,
88
+ changeOrigin: options.changeOrigin !== false,
89
+ pathRewrite: options.pathRewrite || {},
90
+ secure: options.secure !== false,
91
+ ws: options.ws !== false,
92
+ logLevel: options.logLevel || "warn",
93
+ onProxyReq: (proxyReq, req, res) => {
94
+ if (options.onProxyReq) {
95
+ options.onProxyReq(proxyReq, req, res);
96
+ }
97
+ console.log(`代理请求: ${req.method} ${req.path} -> ${options.target}`);
98
+ },
99
+ onError: (err, req, res) => {
100
+ console.error("代理错误:", err);
101
+ res.writeHead(500, {
102
+ "Content-Type": "text/plain",
103
+ });
104
+ res.end("代理请求错误: " + err);
105
+ },
106
+ });
107
+
108
+ // 应用代理中间件到应用
109
+ app.use(context, proxy);
110
+ console.log(`代理已配置: ${context} -> ${options.target}`);
111
+ } catch (error) {
112
+ console.error(`配置代理 ${context} 时出错:`, error.message);
113
+ }
114
+ });
51
115
  }
52
116
 
53
117
  function setupRoutes(app, wss) {
package/lib/translate.js CHANGED
@@ -47,7 +47,7 @@ const countryLanguageMap = {
47
47
  tw: "zh-TW",
48
48
  gb: "en",
49
49
  br: "pt",
50
- in: "hi"
50
+ in: "hi",
51
51
  };
52
52
 
53
53
  async function translateStr(str, targetLanguage) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pug-site-core",
3
- "version": "2.0.17",
3
+ "version": "2.0.18",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -25,6 +25,13 @@
25
25
  "express": "^4.19.2",
26
26
  "express-useragent": "^1.0.15",
27
27
  "fs-extra": "^11.2.0",
28
+ "glob": "^10.3.10",
29
+ "http-proxy-middleware": "^3.0.5",
30
+ "imagemin": "^8.0.1",
31
+ "imagemin-gifsicle": "^7.0.0",
32
+ "imagemin-mozjpeg": "^10.0.0",
33
+ "imagemin-pngquant": "^9.0.2",
34
+ "imagemin-svgo": "^10.0.1",
28
35
  "ip": "^2.0.1",
29
36
  "javascript-obfuscator": "^4.1.1",
30
37
  "jstransformer-autoprefixer": "^2.0.0",
@@ -36,22 +43,15 @@
36
43
  "pug": "^3.0.3",
37
44
  "tcp-port-used": "^1.0.2",
38
45
  "uglify-js": "^3.19.3",
39
- "ws": "^8.18.0",
40
- "glob": "^10.3.10",
41
- "imagemin": "^8.0.1",
42
- "imagemin-gifsicle": "^7.0.0",
43
- "imagemin-mozjpeg": "^10.0.0",
44
- "imagemin-pngquant": "^9.0.2",
45
- "imagemin-svgo": "^10.0.1"
46
+ "ws": "^8.18.0"
46
47
  },
47
48
  "license": "ISC",
48
- "description": "getFun生成函数时的bug修复",
49
+ "description": "增加本地server的代理功能",
49
50
  "files": [
50
51
  "lib/",
51
52
  "index.js"
52
53
  ],
53
54
  "exports": {
54
55
  ".": "./index.js"
55
- },
56
- "devDependencies": {}
56
+ }
57
57
  }