unplugin-https-reverse-proxy 1.0.0-beta.0 → 1.0.0-beta.10
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 +94 -1
- package/dist/{chunk-3RTK3FSZ.js → chunk-RUJE3YFB.js} +21 -0
- package/dist/{chunk-KYMRDRAJ.js → chunk-XLSIGDFU.js} +135 -77
- package/dist/esbuild.cjs +153 -79
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +157 -81
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +6 -4
- package/dist/nuxt.cjs +101 -134
- package/dist/nuxt.js +29 -9
- package/dist/rollup.cjs +153 -79
- package/dist/rollup.js +2 -2
- package/dist/rspack.cjs +643 -0
- package/dist/rspack.d.cts +5 -0
- package/dist/rspack.d.ts +5 -0
- package/dist/rspack.js +10 -0
- package/dist/utils.cjs +25 -2
- package/dist/utils.d.cts +3 -1
- package/dist/utils.d.ts +3 -1
- package/dist/utils.js +7 -3
- package/dist/vite.cjs +153 -79
- package/dist/vite.d.cts +1 -2
- package/dist/vite.d.ts +1 -2
- package/dist/vite.js +7 -4
- package/dist/webpack.cjs +153 -79
- package/dist/webpack.js +7 -4
- package/package.json +42 -24
- package/src/caddy/constants.ts +13 -1
- package/src/caddy/index.ts +32 -5
- package/src/host/index.ts +4 -1
- package/src/index.ts +128 -85
- package/src/nuxt.ts +33 -6
- package/src/rspack.ts +5 -0
- package/src/utils.ts +21 -0
- package/dist/chunk-AXBUFN6T.js +0 -11
- package/dist/chunk-MDJOTQRD.js +0 -11
package/src/index.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import process from 'node:process'
|
|
2
|
-
import { chmodSync, existsSync } from 'node:fs'
|
|
3
2
|
import type { UnpluginFactory } from 'unplugin'
|
|
4
3
|
import { createUnplugin } from 'unplugin'
|
|
5
|
-
import type { ResolvedConfig
|
|
4
|
+
import type { ResolvedConfig } from 'vite'
|
|
6
5
|
import c from 'picocolors'
|
|
7
|
-
import { consola, isAdmin } from './utils'
|
|
6
|
+
import { consola, isAdmin, once } from './utils'
|
|
8
7
|
import type { Options } from './types'
|
|
9
8
|
import { CaddyInstant } from './caddy'
|
|
10
9
|
|
|
@@ -12,7 +11,30 @@ let config: ResolvedConfig
|
|
|
12
11
|
|
|
13
12
|
let caddy: CaddyInstant
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
const cwd = process.cwd()
|
|
15
|
+
|
|
16
|
+
export const vitePrintUrls = once((
|
|
17
|
+
options: Options,
|
|
18
|
+
source: string,
|
|
19
|
+
target: string,
|
|
20
|
+
base: string = '/',
|
|
21
|
+
_printUrls?: () => void,
|
|
22
|
+
) => {
|
|
23
|
+
_printUrls?.()
|
|
24
|
+
|
|
25
|
+
if (caddy)
|
|
26
|
+
return
|
|
27
|
+
caddy = new CaddyInstant()
|
|
28
|
+
caddy.run(source, target, {
|
|
29
|
+
base,
|
|
30
|
+
...options,
|
|
31
|
+
}).then(() => {
|
|
32
|
+
const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
|
|
33
|
+
consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
|
|
34
|
+
}).catch((e) => {
|
|
35
|
+
throw e
|
|
36
|
+
})
|
|
37
|
+
})
|
|
16
38
|
|
|
17
39
|
export const unpluginFactory: UnpluginFactory<Options> = options => ({
|
|
18
40
|
name: 'unplugin-https-reverse-proxy',
|
|
@@ -22,70 +44,103 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
|
|
|
22
44
|
config = _config
|
|
23
45
|
},
|
|
24
46
|
configureServer(server) {
|
|
25
|
-
if (
|
|
26
|
-
consola.warn('please run as administrator')
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (config.command === 'build')
|
|
47
|
+
if (config.command !== 'serve')
|
|
31
48
|
return
|
|
32
49
|
|
|
33
50
|
const {
|
|
34
51
|
enable = true,
|
|
35
52
|
target = '',
|
|
36
53
|
} = options
|
|
54
|
+
|
|
37
55
|
if (!enable)
|
|
38
56
|
return
|
|
57
|
+
|
|
58
|
+
if (!isAdmin()) {
|
|
59
|
+
consola.warn('please run as administrator')
|
|
60
|
+
return
|
|
61
|
+
}
|
|
39
62
|
if (!target) {
|
|
40
63
|
consola.fail('please provide target')
|
|
41
64
|
return
|
|
42
65
|
}
|
|
43
66
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const pwd = process.cwd()
|
|
52
|
-
const vireCacheDir = `${pwd}/node_modules/.vite`
|
|
53
|
-
if (existsSync(vireCacheDir))
|
|
54
|
-
chmodSync(vireCacheDir, 0o777)
|
|
55
|
-
|
|
56
|
-
let source = `localhost:${config.server.port}`
|
|
57
|
-
const url = server.resolvedUrls?.local[0]
|
|
58
|
-
if (url) {
|
|
59
|
-
const u = new URL(url)
|
|
60
|
-
source = u.host
|
|
61
|
-
}
|
|
62
|
-
const base = server.config.base || '/'
|
|
63
|
-
try {
|
|
64
|
-
if (caddy)
|
|
65
|
-
return
|
|
66
|
-
caddy = new CaddyInstant()
|
|
67
|
-
caddy.run(source, target, {
|
|
68
|
-
base,
|
|
69
|
-
...options,
|
|
70
|
-
}).then(() => {
|
|
71
|
-
const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
|
|
72
|
-
consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
|
|
73
|
-
}).catch((e) => {
|
|
74
|
-
throw e
|
|
75
|
-
})
|
|
76
|
-
}
|
|
77
|
-
catch (e) {
|
|
78
|
-
consola.error(e)
|
|
79
|
-
}
|
|
67
|
+
const _printUrls = server.printUrls.bind(server)
|
|
68
|
+
|
|
69
|
+
let source = `localhost:${config.server.port || 5173}`
|
|
70
|
+
const url = server.resolvedUrls?.local[0]
|
|
71
|
+
if (url) {
|
|
72
|
+
const u = new URL(url)
|
|
73
|
+
source = u.host
|
|
80
74
|
}
|
|
75
|
+
|
|
76
|
+
const base = server.config.base || '/'
|
|
77
|
+
|
|
78
|
+
server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls)
|
|
81
79
|
},
|
|
82
80
|
},
|
|
83
81
|
webpack(compiler) {
|
|
82
|
+
if (process.env.NODE_ENV !== 'development')
|
|
83
|
+
return
|
|
84
|
+
|
|
85
|
+
const {
|
|
86
|
+
enable = true,
|
|
87
|
+
target = '',
|
|
88
|
+
} = options
|
|
89
|
+
|
|
90
|
+
if (!enable)
|
|
91
|
+
return
|
|
92
|
+
|
|
84
93
|
if (!isAdmin()) {
|
|
85
94
|
consola.warn('please run as administrator')
|
|
86
95
|
return
|
|
87
96
|
}
|
|
88
97
|
|
|
98
|
+
if (!target) {
|
|
99
|
+
consola.fail('please provide target')
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const devServer = {
|
|
104
|
+
host: 'localhost',
|
|
105
|
+
port: 8080,
|
|
106
|
+
...compiler.options.devServer,
|
|
107
|
+
// @ts-expect-error vuecli
|
|
108
|
+
...(process.VUE_CLI_SERVICE?.projectOptions.devServer),
|
|
109
|
+
}
|
|
110
|
+
const source = `${devServer.host}:${devServer.port}`
|
|
111
|
+
|
|
112
|
+
if (caddy)
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
// https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
|
|
116
|
+
const _close = compiler.close?.bind(compiler)
|
|
117
|
+
if (_close) {
|
|
118
|
+
compiler.close = async (callback: Parameters<typeof _close>[0]) => {
|
|
119
|
+
process.on('exit', () => {
|
|
120
|
+
_close && _close(callback)
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
// webpack 4 not support yet
|
|
126
|
+
options.https = false
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
caddy = new CaddyInstant()
|
|
130
|
+
|
|
131
|
+
caddy.run(source, target, {
|
|
132
|
+
...options,
|
|
133
|
+
}).then(() => {
|
|
134
|
+
const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
|
|
135
|
+
|
|
136
|
+
compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
|
|
137
|
+
consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
|
|
138
|
+
})
|
|
139
|
+
}).catch((e) => {
|
|
140
|
+
throw e
|
|
141
|
+
})
|
|
142
|
+
},
|
|
143
|
+
async rspack(compiler) {
|
|
89
144
|
if (process.env.NODE_ENV !== 'development')
|
|
90
145
|
return
|
|
91
146
|
|
|
@@ -93,57 +148,45 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
|
|
|
93
148
|
enable = true,
|
|
94
149
|
target = '',
|
|
95
150
|
} = options
|
|
151
|
+
|
|
96
152
|
if (!enable)
|
|
97
153
|
return
|
|
154
|
+
|
|
155
|
+
if (!isAdmin()) {
|
|
156
|
+
consola.warn('please run as administrator')
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
|
|
98
160
|
if (!target) {
|
|
99
161
|
consola.fail('please provide target')
|
|
100
162
|
return
|
|
101
163
|
}
|
|
102
164
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const source = `${devServer.host}:${devServer.port}`
|
|
165
|
+
const devServer = {
|
|
166
|
+
host: 'localhost',
|
|
167
|
+
port: 8080,
|
|
168
|
+
...compiler.options.devServer,
|
|
169
|
+
...(await (await import('@rsbuild/core'))?.loadConfig({
|
|
170
|
+
cwd,
|
|
171
|
+
}))?.content?.server,
|
|
172
|
+
}
|
|
112
173
|
|
|
113
|
-
|
|
114
|
-
return
|
|
174
|
+
const source = `${devServer.host}:${devServer.port}`
|
|
115
175
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (_close) {
|
|
119
|
-
compiler.close = async (callback: Parameters<typeof _close>[0]) => {
|
|
120
|
-
process.on('exit', () => {
|
|
121
|
-
_close && _close(callback)
|
|
122
|
-
})
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
// webpack 4 not support yet
|
|
127
|
-
options.https = false
|
|
128
|
-
}
|
|
176
|
+
if (caddy)
|
|
177
|
+
return
|
|
129
178
|
|
|
130
|
-
|
|
179
|
+
caddy = new CaddyInstant()
|
|
131
180
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
181
|
+
caddy.run(source, target, {
|
|
182
|
+
...options,
|
|
183
|
+
}).then(() => {
|
|
184
|
+
const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
|
|
136
185
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
throw e
|
|
142
|
-
})
|
|
143
|
-
}
|
|
144
|
-
catch (e) {
|
|
145
|
-
consola.error(e)
|
|
146
|
-
}
|
|
186
|
+
consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
|
|
187
|
+
}).catch((e) => {
|
|
188
|
+
throw e
|
|
189
|
+
})
|
|
147
190
|
},
|
|
148
191
|
})
|
|
149
192
|
|
package/src/nuxt.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import webpack from './webpack'
|
|
1
|
+
import process from 'node:process'
|
|
2
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
4
3
|
import type { Options } from './types'
|
|
5
4
|
import '@nuxt/schema'
|
|
5
|
+
import { consola, isAdmin } from './utils'
|
|
6
|
+
import { vitePrintUrls } from '.'
|
|
6
7
|
|
|
7
8
|
export interface ModuleOptions extends Options {
|
|
8
9
|
|
|
@@ -14,9 +15,35 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
14
15
|
configKey: 'unpluginHttpsReverseProxy',
|
|
15
16
|
},
|
|
16
17
|
setup(options, _nuxt) {
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
if (process.env.NODE_ENV !== 'development')
|
|
19
|
+
return
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
const {
|
|
22
|
+
enable = true,
|
|
23
|
+
target = '',
|
|
24
|
+
} = options
|
|
25
|
+
|
|
26
|
+
if (!enable)
|
|
27
|
+
return
|
|
28
|
+
|
|
29
|
+
if (!isAdmin()) {
|
|
30
|
+
consola.warn('please run as administrator')
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!target) {
|
|
35
|
+
consola.fail('please provide target')
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// https://github.com/nuxt/cli/issues/220#issuecomment-1735394141
|
|
40
|
+
_nuxt.hook('listen', (_, listener) => {
|
|
41
|
+
const url = listener.url
|
|
42
|
+
const source = new URL(url).host
|
|
43
|
+
|
|
44
|
+
const base = _nuxt.options.app.baseURL || '/'
|
|
45
|
+
|
|
46
|
+
vitePrintUrls(options, source, options.target, base)
|
|
47
|
+
})
|
|
21
48
|
},
|
|
22
49
|
})
|
package/src/rspack.ts
ADDED
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import process from 'node:process'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { chmod, readdir, stat } from 'node:fs/promises'
|
|
2
4
|
import { execSync } from 'node:child_process'
|
|
3
5
|
import { createConsola } from 'consola'
|
|
4
6
|
|
|
@@ -6,6 +8,25 @@ export const consola = createConsola({
|
|
|
6
8
|
level: 3,
|
|
7
9
|
}).withTag('reverse-proxy')
|
|
8
10
|
|
|
11
|
+
export async function chmodRecursive(_path: string, mode: number) {
|
|
12
|
+
await chmod(_path, mode)
|
|
13
|
+
if ((await stat(_path)).isDirectory()) {
|
|
14
|
+
const children = await readdir(_path)
|
|
15
|
+
for (const child of children)
|
|
16
|
+
await chmodRecursive(path.join(_path, child), mode)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> {
|
|
21
|
+
let called = false
|
|
22
|
+
return (...args: any[]) => {
|
|
23
|
+
if (called)
|
|
24
|
+
return
|
|
25
|
+
called = true
|
|
26
|
+
return fn(...args)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
9
30
|
export function isAdmin() {
|
|
10
31
|
if (process.platform === 'win32') {
|
|
11
32
|
try {
|
package/dist/chunk-AXBUFN6T.js
DELETED