@gylautorun/dev-proxy-cookie 1.0.0 → 1.0.1

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/dist/index.mjs CHANGED
@@ -118,6 +118,102 @@ function watchCookieFile(cookieFile, onCookieChange, onError) {
118
118
  return watcher;
119
119
  }
120
120
 
121
+ // src/utils/env-detector.ts
122
+ function isProductionValue(value) {
123
+ const productionValues = [
124
+ "production",
125
+ // 生产环境
126
+ "prod",
127
+ // 生产环境
128
+ "prd",
129
+ // 生产环境
130
+ "release",
131
+ // 发布环境
132
+ "staging",
133
+ // 预发布环境
134
+ "uat"
135
+ // 预发布环境
136
+ ];
137
+ return productionValues.includes(value.toLowerCase().trim());
138
+ }
139
+ function detectProductionEnvironment(customEnvs = [], debug = false, loggerPrefix = "[env-detector]") {
140
+ const env = process.env;
141
+ if (customEnvs.length > 0) {
142
+ for (const envName of customEnvs) {
143
+ const envValue = env[envName];
144
+ if (envValue && isProductionValue(envValue)) {
145
+ if (debug) {
146
+ console.log(`${loggerPrefix} Detected production via custom env: ${envName}=${envValue}`);
147
+ }
148
+ return true;
149
+ }
150
+ }
151
+ }
152
+ const commonEnvNames = [
153
+ "NODE_ENV",
154
+ "BUILD_MODE",
155
+ "VUE_APP_ENV",
156
+ "VITE_NODE_ENV",
157
+ "WEBPACK_MODE",
158
+ "CI_ENV",
159
+ "APP_ENV",
160
+ "ENV",
161
+ "DEPLOY_ENV",
162
+ "RUN_MODE"
163
+ ];
164
+ for (const envName of commonEnvNames) {
165
+ const envValue = env[envName];
166
+ if (envValue && isProductionValue(envValue)) {
167
+ if (debug) {
168
+ console.log(`${loggerPrefix} Detected production via env: ${envName}=${envValue}`);
169
+ }
170
+ return true;
171
+ }
172
+ }
173
+ if (env.CI === "true" || env.CI === "1" || env.CI === "yes") {
174
+ if (debug) {
175
+ console.log(`${loggerPrefix} Detected production via CI env`);
176
+ }
177
+ return true;
178
+ }
179
+ if (env.npm_lifecycle_event) {
180
+ const lifecycleEvent = env.npm_lifecycle_event.toLowerCase();
181
+ if (lifecycleEvent.includes("build") || lifecycleEvent.includes("prod") || lifecycleEvent.includes("prd") || lifecycleEvent.includes("release")) {
182
+ if (debug) {
183
+ console.log(`${loggerPrefix} Detected production via lifecycle event: ${env.npm_lifecycle_event}`);
184
+ }
185
+ return true;
186
+ }
187
+ }
188
+ const processArgs = process.argv.join("").toLowerCase();
189
+ if (processArgs.includes("build") || processArgs.includes("production") || processArgs.includes("--mode=production") || processArgs.includes("--prod") || processArgs.includes("--release")) {
190
+ if (debug) {
191
+ console.log(`${loggerPrefix} Detected production via process arguments`);
192
+ }
193
+ return true;
194
+ }
195
+ return false;
196
+ }
197
+ function shouldEnableWatch(watch, customEnvs = [], debug = false, loggerPrefix = "[env-detector]") {
198
+ if (typeof watch === "boolean") {
199
+ if (debug && !watch) {
200
+ console.log(`${loggerPrefix} Watch disabled by user setting`);
201
+ }
202
+ return watch;
203
+ }
204
+ const isProduction = detectProductionEnvironment(customEnvs, debug, loggerPrefix);
205
+ if (isProduction) {
206
+ if (debug) {
207
+ console.log(`${loggerPrefix} Auto-detected production mode - disabling watch`);
208
+ }
209
+ return false;
210
+ }
211
+ if (debug) {
212
+ console.log(`${loggerPrefix} Auto-detected development mode - enabling watch`);
213
+ }
214
+ return true;
215
+ }
216
+
121
217
  // src/proxy/apply-dev-cookie-header.ts
122
218
  function applyDevCookieHeader(proxyReq, cookie) {
123
219
  if (!cookie) return;
@@ -390,13 +486,29 @@ var AutoProxyCookie = class {
390
486
  }
391
487
  }
392
488
  startFileWatch() {
393
- this.watcher = watchCookieFile(
394
- this.options.cookieFile,
395
- this.handleCookieChange,
396
- (error) => {
397
- this.log("error", "[AutoProxyCookie] File watch error:", error.message);
489
+ let shouldWatch;
490
+ if (this.options.isDev !== void 0) {
491
+ shouldWatch = this.options.isDev;
492
+ if (this.options.debug) {
493
+ console.log(`[AutoProxyCookie] isDev=${this.options.isDev}, ${shouldWatch ? "enabling" : "disabling"} watch`);
398
494
  }
399
- );
495
+ } else {
496
+ shouldWatch = true;
497
+ if (this.options.debug) {
498
+ console.log("[AutoProxyCookie] Default behavior: enabling watch (dev mode)");
499
+ }
500
+ }
501
+ if (shouldWatch) {
502
+ this.watcher = watchCookieFile(
503
+ this.options.cookieFile,
504
+ this.handleCookieChange,
505
+ (error) => {
506
+ this.log("error", "[AutoProxyCookie] File watch error:", error.message);
507
+ }
508
+ );
509
+ } else if (this.options.debug) {
510
+ console.log("[AutoProxyCookie] File watch disabled");
511
+ }
400
512
  }
401
513
  stop() {
402
514
  if (this.watcher) {
@@ -421,6 +533,7 @@ function createAutoProxyCookie(options) {
421
533
  function viteAutoProxyCookie(options) {
422
534
  const {
423
535
  name = "vite-auto-proxy-cookie",
536
+ isDev,
424
537
  ...autoProxyOptions
425
538
  } = options;
426
539
  let autoProxy = null;
@@ -431,7 +544,8 @@ function viteAutoProxyCookie(options) {
431
544
  autoProxy = createAutoProxyCookie({
432
545
  ...autoProxyOptions,
433
546
  debug: options.debug ?? false,
434
- autoRestart: options.autoRestart ?? true
547
+ autoRestart: options.autoRestart ?? true,
548
+ isDev
435
549
  });
436
550
  try {
437
551
  await autoProxy.setup(server);
@@ -450,7 +564,7 @@ function viteAutoProxyCookie(options) {
450
564
 
451
565
  // src/proxy/vite-cookie-plugin.ts
452
566
  function viteDevProxyCookie(options) {
453
- const { cookieFile, debug = false, onCookieChange } = options;
567
+ const { cookieFile, debug = false, onCookieChange, watch = "auto", isDev } = options;
454
568
  let watcher = null;
455
569
  let currentCookie = "";
456
570
  const cookieReader = new CookieReader({ cookieFile });
@@ -474,17 +588,30 @@ function viteDevProxyCookie(options) {
474
588
  } else {
475
589
  console.warn("[vite-dev-proxy-cookie] Could not access middleware stack, cookie injection disabled");
476
590
  }
477
- watcher = watchCookieFile(
478
- cookieFile,
479
- (newCookie) => {
480
- currentCookie = newCookie;
481
- onCookieChange?.(newCookie);
482
- console.log("[vite-dev-proxy-cookie] Cookie changed, please restart server for full effect");
483
- },
484
- (error) => {
485
- console.error("[vite-dev-proxy-cookie] Watch error:", error.message);
591
+ let shouldWatch;
592
+ if (isDev !== void 0) {
593
+ shouldWatch = isDev;
594
+ if (debug) {
595
+ console.log(`[vite-dev-proxy-cookie] isDev=${isDev}, ${shouldWatch ? "enabling" : "disabling"} watch`);
486
596
  }
487
- );
597
+ } else {
598
+ shouldWatch = shouldEnableWatch(watch, [], debug, "[vite-dev-proxy-cookie]");
599
+ }
600
+ if (shouldWatch) {
601
+ watcher = watchCookieFile(
602
+ cookieFile,
603
+ (newCookie) => {
604
+ currentCookie = newCookie;
605
+ onCookieChange?.(newCookie);
606
+ console.log("[vite-dev-proxy-cookie] Cookie changed, please restart server for full effect");
607
+ },
608
+ (error) => {
609
+ console.error("[vite-dev-proxy-cookie] Watch error:", error.message);
610
+ }
611
+ );
612
+ } else if (debug) {
613
+ console.log("[vite-dev-proxy-cookie] File watch disabled");
614
+ }
488
615
  },
489
616
  closeBundle() {
490
617
  watcher?.stop();
@@ -527,9 +654,23 @@ function createVueProxyConfig(target, options = {}) {
527
654
  return config;
528
655
  }
529
656
  function createFileCookieGetter(cookieFile, options = {}) {
530
- const { watch = true, debug = false } = options;
657
+ const {
658
+ watch = "auto",
659
+ debug = false,
660
+ productionEnvs = [],
661
+ isDev
662
+ } = options;
531
663
  const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) });
532
- if (watch) {
664
+ let shouldWatch;
665
+ if (isDev !== void 0) {
666
+ shouldWatch = isDev;
667
+ if (debug) {
668
+ console.log(`[CookieFile] isDev=${isDev}, ${shouldWatch ? "enabling" : "disabling"} watch`);
669
+ }
670
+ } else {
671
+ shouldWatch = shouldEnableWatch(watch, productionEnvs, debug, "[CookieFile]");
672
+ }
673
+ if (shouldWatch) {
533
674
  watchCookieFile(
534
675
  path4.resolve(cookieFile),
535
676
  (newCookie) => {
@@ -541,6 +682,8 @@ function createFileCookieGetter(cookieFile, options = {}) {
541
682
  console.error("[CookieFile] Watch error:", error.message);
542
683
  }
543
684
  );
685
+ } else if (debug) {
686
+ console.log("[CookieFile] File watch disabled");
544
687
  }
545
688
  return () => reader.readCookie();
546
689
  }
@@ -602,6 +745,8 @@ function detectViteVersion() {
602
745
  function createDevProxyCookie(options) {
603
746
  const {
604
747
  mode = "auto",
748
+ watch = "auto",
749
+ isDev,
605
750
  ...restOptions
606
751
  } = options;
607
752
  const version = detectViteVersion();
@@ -612,7 +757,9 @@ function createDevProxyCookie(options) {
612
757
  return viteDevProxyCookie({
613
758
  cookieFile: options.cookieFile,
614
759
  debug: options.debug,
615
- onCookieChange: options.onCookieChange
760
+ onCookieChange: options.onCookieChange,
761
+ watch,
762
+ isDev
616
763
  });
617
764
  }
618
765
  const pluginOptions = {
@@ -643,8 +790,11 @@ export {
643
790
  createDevProxyCookie,
644
791
  createFileCookieGetter,
645
792
  createVueProxyConfig,
793
+ detectProductionEnvironment,
646
794
  getViteMajorVersion,
647
795
  getViteVersion,
796
+ isProductionValue,
797
+ shouldEnableWatch,
648
798
  viteAutoProxyCookie,
649
799
  viteDevProxyCookie,
650
800
  watchCookieFile
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gylautorun/dev-proxy-cookie",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "开发环境代理Cookie注入工具,支持文件监听自动重载和自动代理",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/proxy/core.ts CHANGED
@@ -60,6 +60,15 @@ export interface AutoProxyCookieOptions {
60
60
  cookiePathRewrite?: false | string | { [oldPath: string]: string };
61
61
  headers?: { [header: string]: string };
62
62
  hooks?: ProxyHooks;
63
+ /**
64
+ * 是否为开发环境(优先级最高)
65
+ * 设置此参数后,将直接决定是否启用文件监听:
66
+ * - true: 启用监听(开发模式)
67
+ * - false: 禁用监听(生产模式)
68
+ *
69
+ * 使用示例: isDev: process.env.NODE_ENV === 'development'
70
+ */
71
+ isDev?: boolean;
63
72
  }
64
73
 
65
74
  export class AutoProxyCookie {
@@ -368,13 +377,34 @@ export class AutoProxyCookie {
368
377
  }
369
378
 
370
379
  private startFileWatch(): void {
371
- this.watcher = watchCookieFile(
372
- this.options.cookieFile,
373
- this.handleCookieChange,
374
- (error) => {
375
- this.log('error', '[AutoProxyCookie] File watch error:', error.message);
380
+ // 判断是否应该启用监听
381
+ // isDev 参数优先级最高
382
+ let shouldWatch: boolean;
383
+
384
+ if (this.options.isDev !== undefined) {
385
+ shouldWatch = this.options.isDev;
386
+ if (this.options.debug) {
387
+ console.log(`[AutoProxyCookie] isDev=${this.options.isDev}, ${shouldWatch ? 'enabling' : 'disabling'} watch`);
376
388
  }
377
- );
389
+ } else {
390
+ // 默认启用监听(因为 AutoProxyCookie 主要用于开发环境)
391
+ shouldWatch = true;
392
+ if (this.options.debug) {
393
+ console.log('[AutoProxyCookie] Default behavior: enabling watch (dev mode)');
394
+ }
395
+ }
396
+
397
+ if (shouldWatch) {
398
+ this.watcher = watchCookieFile(
399
+ this.options.cookieFile,
400
+ this.handleCookieChange,
401
+ (error) => {
402
+ this.log('error', '[AutoProxyCookie] File watch error:', error.message);
403
+ }
404
+ );
405
+ } else if (this.options.debug) {
406
+ console.log('[AutoProxyCookie] File watch disabled');
407
+ }
378
408
  }
379
409
 
380
410
  stop(): void {
@@ -32,11 +32,30 @@ export interface UnifiedProxyCookieOptions {
32
32
  includePaths?: string[];
33
33
  onCookieChange?: (cookie: string) => void;
34
34
  mode?: 'auto' | 'proxy' | 'cookie';
35
+ /**
36
+ * 是否监听文件变化
37
+ * - true: 始终监听
38
+ * - false: 从不监听
39
+ * - 'auto': 根据环境自动判断(默认)
40
+ * @default 'auto'
41
+ */
42
+ watch?: boolean | 'auto';
43
+ /**
44
+ * 是否为开发环境(优先级最高)
45
+ * 设置此参数后,将直接决定是否启用文件监听:
46
+ * - true: 启用监听(开发模式)
47
+ * - false: 禁用监听(生产模式)
48
+ *
49
+ * 使用示例: isDev: process.env.NODE_ENV === 'development'
50
+ */
51
+ isDev?: boolean;
35
52
  }
36
53
 
37
54
  export function createDevProxyCookie(options: UnifiedProxyCookieOptions): Plugin {
38
55
  const {
39
56
  mode = 'auto',
57
+ watch = 'auto',
58
+ isDev,
40
59
  ...restOptions
41
60
  } = options;
42
61
 
@@ -51,6 +70,8 @@ export function createDevProxyCookie(options: UnifiedProxyCookieOptions): Plugin
51
70
  cookieFile: options.cookieFile,
52
71
  debug: options.debug,
53
72
  onCookieChange: options.onCookieChange,
73
+ watch,
74
+ isDev,
54
75
  });
55
76
  }
56
77
 
@@ -1,15 +1,32 @@
1
1
  import type { Plugin, ViteDevServer } from 'vite';
2
- import { CookieReader, CookieWatcher, watchCookieFile } from '../utils';
2
+ import { CookieReader, watchCookieFile, shouldEnableWatch } from '../utils';
3
3
 
4
4
  export interface ViteDevProxyCookieOptions {
5
5
  cookieFile: string;
6
6
  debug?: boolean;
7
7
  onCookieChange?: (cookie: string) => void;
8
+ /**
9
+ * 是否监听文件变化
10
+ * - true: 始终监听
11
+ * - false: 从不监听
12
+ * - 'auto': 根据环境自动判断(默认)
13
+ * @default 'auto'
14
+ */
15
+ watch?: boolean | 'auto';
16
+ /**
17
+ * 是否为开发环境(优先级最高)
18
+ * 设置此参数后,将直接决定是否启用文件监听:
19
+ * - true: 启用监听(开发模式)
20
+ * - false: 禁用监听(生产模式)
21
+ *
22
+ * 使用示例: isDev: process.env.NODE_ENV === 'development'
23
+ */
24
+ isDev?: boolean;
8
25
  }
9
26
 
10
27
  export function viteDevProxyCookie(options: ViteDevProxyCookieOptions): Plugin {
11
- const { cookieFile, debug = false, onCookieChange } = options;
12
- let watcher: CookieWatcher | null = null;
28
+ const { cookieFile, debug = false, onCookieChange, watch = 'auto', isDev } = options;
29
+ let watcher: any = null;
13
30
  let currentCookie: string = '';
14
31
 
15
32
  const cookieReader = new CookieReader({ cookieFile });
@@ -40,21 +57,38 @@ export function viteDevProxyCookie(options: ViteDevProxyCookieOptions): Plugin {
40
57
  console.warn('[vite-dev-proxy-cookie] Could not access middleware stack, cookie injection disabled');
41
58
  }
42
59
 
43
- watcher = watchCookieFile(
44
- cookieFile,
45
- (newCookie) => {
46
- currentCookie = newCookie;
47
- onCookieChange?.(newCookie);
48
- console.log('[vite-dev-proxy-cookie] Cookie changed, please restart server for full effect');
49
- },
50
- (error) => {
51
- console.error('[vite-dev-proxy-cookie] Watch error:', error.message);
60
+ // 判断是否应该启用监听
61
+ // isDev 参数优先级最高
62
+ let shouldWatch: boolean;
63
+
64
+ if (isDev !== undefined) {
65
+ shouldWatch = isDev;
66
+ if (debug) {
67
+ console.log(`[vite-dev-proxy-cookie] isDev=${isDev}, ${shouldWatch ? 'enabling' : 'disabling'} watch`);
52
68
  }
53
- );
69
+ } else {
70
+ shouldWatch = shouldEnableWatch(watch, [], debug, '[vite-dev-proxy-cookie]');
71
+ }
72
+
73
+ if (shouldWatch) {
74
+ watcher = watchCookieFile(
75
+ cookieFile,
76
+ (newCookie) => {
77
+ currentCookie = newCookie;
78
+ onCookieChange?.(newCookie);
79
+ console.log('[vite-dev-proxy-cookie] Cookie changed, please restart server for full effect');
80
+ },
81
+ (error) => {
82
+ console.error('[vite-dev-proxy-cookie] Watch error:', error.message);
83
+ }
84
+ );
85
+ } else if (debug) {
86
+ console.log('[vite-dev-proxy-cookie] File watch disabled');
87
+ }
54
88
  },
55
89
 
56
90
  closeBundle() {
57
91
  watcher?.stop();
58
92
  },
59
93
  };
60
- }
94
+ }
@@ -3,6 +3,15 @@ import { AutoProxyCookie, createAutoProxyCookie, type AutoProxyCookieOptions } f
3
3
 
4
4
  export interface ViteAutoProxyCookiePluginOptions extends AutoProxyCookieOptions {
5
5
  name?: string;
6
+ /**
7
+ * 是否为开发环境(优先级最高)
8
+ * 设置此参数后,将直接决定是否启用文件监听:
9
+ * - true: 启用监听(开发模式)
10
+ * - false: 禁用监听(生产模式)
11
+ *
12
+ * 使用示例: isDev: process.env.NODE_ENV === 'development'
13
+ */
14
+ isDev?: boolean;
6
15
  }
7
16
 
8
17
  function getHttpServer(server: ViteDevServer): any {
@@ -18,6 +27,7 @@ function getHttpServer(server: ViteDevServer): any {
18
27
  export function viteAutoProxyCookie(options: ViteAutoProxyCookiePluginOptions): Plugin {
19
28
  const {
20
29
  name = 'vite-auto-proxy-cookie',
30
+ isDev,
21
31
  ...autoProxyOptions
22
32
  } = options;
23
33
 
@@ -32,6 +42,7 @@ export function viteAutoProxyCookie(options: ViteAutoProxyCookiePluginOptions):
32
42
  ...autoProxyOptions,
33
43
  debug: options.debug ?? false,
34
44
  autoRestart: options.autoRestart ?? true,
45
+ isDev,
35
46
  });
36
47
 
37
48
  try {
@@ -1,18 +1,37 @@
1
1
  import type { IncomingMessage } from 'http';
2
2
  import * as path from 'path';
3
- import { CookieReader, watchCookieFile } from '../utils';
3
+ import { CookieReader, watchCookieFile, shouldEnableWatch } from '../utils';
4
4
  import { applyDevCookieHeader } from './apply-dev-cookie-header';
5
5
 
6
6
  /** Options for {@link createFileCookieGetter}. */
7
7
  export interface CreateFileCookieGetterOptions {
8
8
  /**
9
- * Watch the file for logging when it changes. Cookie value is always read from disk on each proxy request,
10
- * so you do not need a dev-server restart when this is false.
11
- * @default true
9
+ * 是否监听文件变化
10
+ * - true: 始终监听
11
+ * - false: 从不监听
12
+ * - 'auto': 根据环境自动判断(默认)
13
+ *
14
+ * Cookie 值每次代理请求时都会从磁盘读取,因此即使 watch 为 false,
15
+ * 修改 cookie 文件后也不需要重启开发服务器。
16
+ * @default 'auto'
12
17
  */
13
- watch?: boolean;
18
+ watch?: boolean | 'auto';
14
19
  /** Log when the cookie file changes (only if `watch` is true). @default false */
15
20
  debug?: boolean;
21
+ /**
22
+ * 自定义生产环境变量名称列表,用于判断是否禁用监听
23
+ * 例如: ['MY_APP_ENV', 'BUILD_TYPE']
24
+ */
25
+ productionEnvs?: string[];
26
+ /**
27
+ * 是否为开发环境(优先级最高)
28
+ * 设置此参数后,将直接决定是否启用文件监听:
29
+ * - true: 启用监听(开发模式)
30
+ * - false: 禁用监听(生产模式)
31
+ *
32
+ * 使用示例: isDev: process.env.NODE_ENV === 'development'
33
+ */
34
+ isDev?: boolean;
16
35
  }
17
36
 
18
37
  export interface VueProxyConfigOptions {
@@ -78,10 +97,30 @@ export function createFileCookieGetter(
78
97
  cookieFile: string,
79
98
  options: CreateFileCookieGetterOptions = {}
80
99
  ): () => string {
81
- const { watch = true, debug = false } = options;
100
+ const {
101
+ watch = 'auto',
102
+ debug = false,
103
+ productionEnvs = [],
104
+ isDev
105
+ } = options;
82
106
  const reader = new CookieReader({ cookieFile: path.resolve(cookieFile) });
83
107
 
84
- if (watch) {
108
+ // 判断是否应该启用监听
109
+ // isDev 参数优先级最高,直接决定是否启用监听
110
+ let shouldWatch: boolean;
111
+
112
+ if (isDev !== undefined) {
113
+ // 用户显式设置了 isDev 参数
114
+ shouldWatch = isDev;
115
+ if (debug) {
116
+ console.log(`[CookieFile] isDev=${isDev}, ${shouldWatch ? 'enabling' : 'disabling'} watch`);
117
+ }
118
+ } else {
119
+ // 使用智能环境检测
120
+ shouldWatch = shouldEnableWatch(watch, productionEnvs, debug, '[CookieFile]');
121
+ }
122
+
123
+ if (shouldWatch) {
85
124
  watchCookieFile(
86
125
  path.resolve(cookieFile),
87
126
  (newCookie) => {
@@ -93,6 +132,8 @@ export function createFileCookieGetter(
93
132
  console.error('[CookieFile] Watch error:', error.message);
94
133
  }
95
134
  );
135
+ } else if (debug) {
136
+ console.log('[CookieFile] File watch disabled');
96
137
  }
97
138
 
98
139
  return () => reader.readCookie();