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/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, ViteDevServer } from 'vite'
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
- let _servce: ViteDevServer
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 (!isAdmin()) {
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
- _servce = server
45
-
46
- const _printUrls = server.printUrls
47
- server.printUrls = () => {
48
- _printUrls()
49
-
50
- // fix `Error: EPERM: operation not permitted`
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
- try {
104
- const devServer = {
105
- host: 'localhost',
106
- port: 8080,
107
- ...compiler.options.devServer,
108
- // @ts-expect-error vuecli
109
- ...process.VUE_CLI_SERVICE.projectOptions.devServer,
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
- if (caddy)
114
- return
174
+ const source = `${devServer.host}:${devServer.port}`
115
175
 
116
- // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
117
- const _close = compiler.close?.bind(compiler)
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
- caddy = new CaddyInstant()
179
+ caddy = new CaddyInstant()
131
180
 
132
- caddy.run(source, target, {
133
- ...options,
134
- }).then(() => {
135
- const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
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
- compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
138
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
139
- })
140
- }).catch((e) => {
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 { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'
2
- import vite from './vite'
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
- addVitePlugin(() => vite(options))
18
- addWebpackPlugin(() => webpack(options))
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
@@ -0,0 +1,5 @@
1
+ import type { Options } from './types'
2
+ import unplugin from '.'
3
+
4
+ // TODO: some upstream lib failed generate invalid dts, remove the any in the future
5
+ export default unplugin.rspack as (options?: Options) => any
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 {
@@ -1,11 +0,0 @@
1
- import {
2
- unpluginFactory
3
- } from "./chunk-KYMRDRAJ.js";
4
-
5
- // src/webpack.ts
6
- import { createWebpackPlugin } from "unplugin";
7
- var webpack_default = createWebpackPlugin(unpluginFactory);
8
-
9
- export {
10
- webpack_default
11
- };
@@ -1,11 +0,0 @@
1
- import {
2
- unpluginFactory
3
- } from "./chunk-KYMRDRAJ.js";
4
-
5
- // src/vite.ts
6
- import { createVitePlugin } from "unplugin";
7
- var vite_default = createVitePlugin(unpluginFactory);
8
-
9
- export {
10
- vite_default
11
- };