@gylautorun/dev-proxy-cookie 1.0.0-beta.1 → 1.0.0
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/README.md +2 -214
- package/dist/index.d.mts +5 -108
- package/dist/index.d.ts +5 -108
- package/dist/index.js +21 -174
- package/dist/index.min.js +4 -4
- package/dist/index.min.mjs +4 -4
- package/dist/index.mjs +21 -171
- package/package.json +1 -1
- package/src/proxy/core.ts +6 -36
- package/src/proxy/vite-adapter.ts +0 -21
- package/src/proxy/vite-cookie-plugin.ts +14 -48
- package/src/proxy/vite-plugin.ts +0 -11
- package/src/proxy/vue-proxy-config.ts +7 -48
- package/src/utils/index.ts +1 -2
- package/src/utils/env-detector.ts +0 -155
package/dist/index.mjs
CHANGED
|
@@ -118,102 +118,6 @@ 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
|
-
|
|
217
121
|
// src/proxy/apply-dev-cookie-header.ts
|
|
218
122
|
function applyDevCookieHeader(proxyReq, cookie) {
|
|
219
123
|
if (!cookie) return;
|
|
@@ -486,29 +390,13 @@ var AutoProxyCookie = class {
|
|
|
486
390
|
}
|
|
487
391
|
}
|
|
488
392
|
startFileWatch() {
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
}
|
|
495
|
-
} else {
|
|
496
|
-
shouldWatch = true;
|
|
497
|
-
if (this.options.debug) {
|
|
498
|
-
console.log("[AutoProxyCookie] Default behavior: enabling watch (dev mode)");
|
|
393
|
+
this.watcher = watchCookieFile(
|
|
394
|
+
this.options.cookieFile,
|
|
395
|
+
this.handleCookieChange,
|
|
396
|
+
(error) => {
|
|
397
|
+
this.log("error", "[AutoProxyCookie] File watch error:", error.message);
|
|
499
398
|
}
|
|
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
|
-
}
|
|
399
|
+
);
|
|
512
400
|
}
|
|
513
401
|
stop() {
|
|
514
402
|
if (this.watcher) {
|
|
@@ -533,7 +421,6 @@ function createAutoProxyCookie(options) {
|
|
|
533
421
|
function viteAutoProxyCookie(options) {
|
|
534
422
|
const {
|
|
535
423
|
name = "vite-auto-proxy-cookie",
|
|
536
|
-
isDev,
|
|
537
424
|
...autoProxyOptions
|
|
538
425
|
} = options;
|
|
539
426
|
let autoProxy = null;
|
|
@@ -544,8 +431,7 @@ function viteAutoProxyCookie(options) {
|
|
|
544
431
|
autoProxy = createAutoProxyCookie({
|
|
545
432
|
...autoProxyOptions,
|
|
546
433
|
debug: options.debug ?? false,
|
|
547
|
-
autoRestart: options.autoRestart ?? true
|
|
548
|
-
isDev
|
|
434
|
+
autoRestart: options.autoRestart ?? true
|
|
549
435
|
});
|
|
550
436
|
try {
|
|
551
437
|
await autoProxy.setup(server);
|
|
@@ -564,7 +450,7 @@ function viteAutoProxyCookie(options) {
|
|
|
564
450
|
|
|
565
451
|
// src/proxy/vite-cookie-plugin.ts
|
|
566
452
|
function viteDevProxyCookie(options) {
|
|
567
|
-
const { cookieFile, debug = false, onCookieChange
|
|
453
|
+
const { cookieFile, debug = false, onCookieChange } = options;
|
|
568
454
|
let watcher = null;
|
|
569
455
|
let currentCookie = "";
|
|
570
456
|
const cookieReader = new CookieReader({ cookieFile });
|
|
@@ -588,30 +474,17 @@ function viteDevProxyCookie(options) {
|
|
|
588
474
|
} else {
|
|
589
475
|
console.warn("[vite-dev-proxy-cookie] Could not access middleware stack, cookie injection disabled");
|
|
590
476
|
}
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
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);
|
|
596
486
|
}
|
|
597
|
-
|
|
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
|
-
}
|
|
487
|
+
);
|
|
615
488
|
},
|
|
616
489
|
closeBundle() {
|
|
617
490
|
watcher?.stop();
|
|
@@ -654,23 +527,9 @@ function createVueProxyConfig(target, options = {}) {
|
|
|
654
527
|
return config;
|
|
655
528
|
}
|
|
656
529
|
function createFileCookieGetter(cookieFile, options = {}) {
|
|
657
|
-
const {
|
|
658
|
-
watch = "auto",
|
|
659
|
-
debug = false,
|
|
660
|
-
productionEnvs = [],
|
|
661
|
-
isDev
|
|
662
|
-
} = options;
|
|
530
|
+
const { watch = true, debug = false } = options;
|
|
663
531
|
const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) });
|
|
664
|
-
|
|
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) {
|
|
532
|
+
if (watch) {
|
|
674
533
|
watchCookieFile(
|
|
675
534
|
path4.resolve(cookieFile),
|
|
676
535
|
(newCookie) => {
|
|
@@ -682,8 +541,6 @@ function createFileCookieGetter(cookieFile, options = {}) {
|
|
|
682
541
|
console.error("[CookieFile] Watch error:", error.message);
|
|
683
542
|
}
|
|
684
543
|
);
|
|
685
|
-
} else if (debug) {
|
|
686
|
-
console.log("[CookieFile] File watch disabled");
|
|
687
544
|
}
|
|
688
545
|
return () => reader.readCookie();
|
|
689
546
|
}
|
|
@@ -745,8 +602,6 @@ function detectViteVersion() {
|
|
|
745
602
|
function createDevProxyCookie(options) {
|
|
746
603
|
const {
|
|
747
604
|
mode = "auto",
|
|
748
|
-
watch = "auto",
|
|
749
|
-
isDev,
|
|
750
605
|
...restOptions
|
|
751
606
|
} = options;
|
|
752
607
|
const version = detectViteVersion();
|
|
@@ -757,9 +612,7 @@ function createDevProxyCookie(options) {
|
|
|
757
612
|
return viteDevProxyCookie({
|
|
758
613
|
cookieFile: options.cookieFile,
|
|
759
614
|
debug: options.debug,
|
|
760
|
-
onCookieChange: options.onCookieChange
|
|
761
|
-
watch,
|
|
762
|
-
isDev
|
|
615
|
+
onCookieChange: options.onCookieChange
|
|
763
616
|
});
|
|
764
617
|
}
|
|
765
618
|
const pluginOptions = {
|
|
@@ -790,11 +643,8 @@ export {
|
|
|
790
643
|
createDevProxyCookie,
|
|
791
644
|
createFileCookieGetter,
|
|
792
645
|
createVueProxyConfig,
|
|
793
|
-
detectProductionEnvironment,
|
|
794
646
|
getViteMajorVersion,
|
|
795
647
|
getViteVersion,
|
|
796
|
-
isProductionValue,
|
|
797
|
-
shouldEnableWatch,
|
|
798
648
|
viteAutoProxyCookie,
|
|
799
649
|
viteDevProxyCookie,
|
|
800
650
|
watchCookieFile
|
package/package.json
CHANGED
package/src/proxy/core.ts
CHANGED
|
@@ -60,15 +60,6 @@ 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;
|
|
72
63
|
}
|
|
73
64
|
|
|
74
65
|
export class AutoProxyCookie {
|
|
@@ -377,34 +368,13 @@ export class AutoProxyCookie {
|
|
|
377
368
|
}
|
|
378
369
|
|
|
379
370
|
private startFileWatch(): void {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
shouldWatch = this.options.isDev;
|
|
386
|
-
if (this.options.debug) {
|
|
387
|
-
console.log(`[AutoProxyCookie] isDev=${this.options.isDev}, ${shouldWatch ? 'enabling' : 'disabling'} watch`);
|
|
371
|
+
this.watcher = watchCookieFile(
|
|
372
|
+
this.options.cookieFile,
|
|
373
|
+
this.handleCookieChange,
|
|
374
|
+
(error) => {
|
|
375
|
+
this.log('error', '[AutoProxyCookie] File watch error:', error.message);
|
|
388
376
|
}
|
|
389
|
-
|
|
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
|
-
}
|
|
377
|
+
);
|
|
408
378
|
}
|
|
409
379
|
|
|
410
380
|
stop(): void {
|
|
@@ -32,30 +32,11 @@ 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;
|
|
52
35
|
}
|
|
53
36
|
|
|
54
37
|
export function createDevProxyCookie(options: UnifiedProxyCookieOptions): Plugin {
|
|
55
38
|
const {
|
|
56
39
|
mode = 'auto',
|
|
57
|
-
watch = 'auto',
|
|
58
|
-
isDev,
|
|
59
40
|
...restOptions
|
|
60
41
|
} = options;
|
|
61
42
|
|
|
@@ -70,8 +51,6 @@ export function createDevProxyCookie(options: UnifiedProxyCookieOptions): Plugin
|
|
|
70
51
|
cookieFile: options.cookieFile,
|
|
71
52
|
debug: options.debug,
|
|
72
53
|
onCookieChange: options.onCookieChange,
|
|
73
|
-
watch,
|
|
74
|
-
isDev,
|
|
75
54
|
});
|
|
76
55
|
}
|
|
77
56
|
|
|
@@ -1,32 +1,15 @@
|
|
|
1
1
|
import type { Plugin, ViteDevServer } from 'vite';
|
|
2
|
-
import { CookieReader,
|
|
2
|
+
import { CookieReader, CookieWatcher, watchCookieFile } 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;
|
|
25
8
|
}
|
|
26
9
|
|
|
27
10
|
export function viteDevProxyCookie(options: ViteDevProxyCookieOptions): Plugin {
|
|
28
|
-
const { cookieFile, debug = false, onCookieChange
|
|
29
|
-
let watcher:
|
|
11
|
+
const { cookieFile, debug = false, onCookieChange } = options;
|
|
12
|
+
let watcher: CookieWatcher | null = null;
|
|
30
13
|
let currentCookie: string = '';
|
|
31
14
|
|
|
32
15
|
const cookieReader = new CookieReader({ cookieFile });
|
|
@@ -57,38 +40,21 @@ export function viteDevProxyCookie(options: ViteDevProxyCookieOptions): Plugin {
|
|
|
57
40
|
console.warn('[vite-dev-proxy-cookie] Could not access middleware stack, cookie injection disabled');
|
|
58
41
|
}
|
|
59
42
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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);
|
|
68
52
|
}
|
|
69
|
-
|
|
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
|
-
}
|
|
53
|
+
);
|
|
88
54
|
},
|
|
89
55
|
|
|
90
56
|
closeBundle() {
|
|
91
57
|
watcher?.stop();
|
|
92
58
|
},
|
|
93
59
|
};
|
|
94
|
-
}
|
|
60
|
+
}
|
package/src/proxy/vite-plugin.ts
CHANGED
|
@@ -3,15 +3,6 @@ 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;
|
|
15
6
|
}
|
|
16
7
|
|
|
17
8
|
function getHttpServer(server: ViteDevServer): any {
|
|
@@ -27,7 +18,6 @@ function getHttpServer(server: ViteDevServer): any {
|
|
|
27
18
|
export function viteAutoProxyCookie(options: ViteAutoProxyCookiePluginOptions): Plugin {
|
|
28
19
|
const {
|
|
29
20
|
name = 'vite-auto-proxy-cookie',
|
|
30
|
-
isDev,
|
|
31
21
|
...autoProxyOptions
|
|
32
22
|
} = options;
|
|
33
23
|
|
|
@@ -42,7 +32,6 @@ export function viteAutoProxyCookie(options: ViteAutoProxyCookiePluginOptions):
|
|
|
42
32
|
...autoProxyOptions,
|
|
43
33
|
debug: options.debug ?? false,
|
|
44
34
|
autoRestart: options.autoRestart ?? true,
|
|
45
|
-
isDev,
|
|
46
35
|
});
|
|
47
36
|
|
|
48
37
|
try {
|
|
@@ -1,37 +1,18 @@
|
|
|
1
1
|
import type { IncomingMessage } from 'http';
|
|
2
2
|
import * as path from 'path';
|
|
3
|
-
import { CookieReader, watchCookieFile
|
|
3
|
+
import { CookieReader, watchCookieFile } 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
|
-
*
|
|
10
|
-
* -
|
|
11
|
-
*
|
|
12
|
-
* - 'auto': 根据环境自动判断(默认)
|
|
13
|
-
*
|
|
14
|
-
* Cookie 值每次代理请求时都会从磁盘读取,因此即使 watch 为 false,
|
|
15
|
-
* 修改 cookie 文件后也不需要重启开发服务器。
|
|
16
|
-
* @default 'auto'
|
|
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
|
|
17
12
|
*/
|
|
18
|
-
watch?: boolean
|
|
13
|
+
watch?: boolean;
|
|
19
14
|
/** Log when the cookie file changes (only if `watch` is true). @default false */
|
|
20
15
|
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;
|
|
35
16
|
}
|
|
36
17
|
|
|
37
18
|
export interface VueProxyConfigOptions {
|
|
@@ -97,30 +78,10 @@ export function createFileCookieGetter(
|
|
|
97
78
|
cookieFile: string,
|
|
98
79
|
options: CreateFileCookieGetterOptions = {}
|
|
99
80
|
): () => string {
|
|
100
|
-
const {
|
|
101
|
-
watch = 'auto',
|
|
102
|
-
debug = false,
|
|
103
|
-
productionEnvs = [],
|
|
104
|
-
isDev
|
|
105
|
-
} = options;
|
|
81
|
+
const { watch = true, debug = false } = options;
|
|
106
82
|
const reader = new CookieReader({ cookieFile: path.resolve(cookieFile) });
|
|
107
83
|
|
|
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) {
|
|
84
|
+
if (watch) {
|
|
124
85
|
watchCookieFile(
|
|
125
86
|
path.resolve(cookieFile),
|
|
126
87
|
(newCookie) => {
|
|
@@ -132,8 +93,6 @@ export function createFileCookieGetter(
|
|
|
132
93
|
console.error('[CookieFile] Watch error:', error.message);
|
|
133
94
|
}
|
|
134
95
|
);
|
|
135
|
-
} else if (debug) {
|
|
136
|
-
console.log('[CookieFile] File watch disabled');
|
|
137
96
|
}
|
|
138
97
|
|
|
139
98
|
return () => reader.readCookie();
|
package/src/utils/index.ts
CHANGED