unplugin-https-reverse-proxy 1.0.0-beta.1 → 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,43 +11,30 @@ let config: ResolvedConfig
12
11
 
13
12
  let caddy: CaddyInstant
14
13
 
15
- export function vitePrintUrls(
16
- server: ViteDevServer,
14
+ const cwd = process.cwd()
15
+
16
+ export const vitePrintUrls = once((
17
17
  options: Options,
18
18
  source: string,
19
19
  target: string,
20
20
  base: string = '/',
21
21
  _printUrls?: () => void,
22
- ) {
22
+ ) => {
23
23
  _printUrls?.()
24
24
 
25
- // fix `Error: EPERM: operation not permitted`
26
- const pwd = process.cwd()
27
- const viteCacheDir = `${pwd}/node_modules/.vite`
28
- if (existsSync(viteCacheDir))
29
- chmodSync(viteCacheDir, 0o777)
30
- const nuxtCacheDir = `${pwd}/.nuxt`
31
- if (existsSync(nuxtCacheDir))
32
- chmodSync(nuxtCacheDir, 0o777)
33
-
34
- try {
35
- if (caddy)
36
- return
37
- caddy = new CaddyInstant()
38
- caddy.run(source, target, {
39
- base,
40
- ...options,
41
- }).then(() => {
42
- const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
43
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
44
- }).catch((e) => {
45
- throw e
46
- })
47
- }
48
- catch (e) {
49
- consola.error(e)
50
- }
51
- }
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
+ })
52
38
 
53
39
  export const unpluginFactory: UnpluginFactory<Options> = options => ({
54
40
  name: 'unplugin-https-reverse-proxy',
@@ -58,20 +44,21 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
58
44
  config = _config
59
45
  },
60
46
  configureServer(server) {
61
- if (!isAdmin()) {
62
- consola.warn('please run as administrator')
63
- return
64
- }
65
-
66
- if (config.command === 'build')
47
+ if (config.command !== 'serve')
67
48
  return
68
49
 
69
50
  const {
70
51
  enable = true,
71
52
  target = '',
72
53
  } = options
54
+
73
55
  if (!enable)
74
56
  return
57
+
58
+ if (!isAdmin()) {
59
+ consola.warn('please run as administrator')
60
+ return
61
+ }
75
62
  if (!target) {
76
63
  consola.fail('please provide target')
77
64
  return
@@ -88,15 +75,72 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
88
75
 
89
76
  const base = server.config.base || '/'
90
77
 
91
- server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls)
78
+ server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls)
92
79
  },
93
80
  },
94
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
+
95
93
  if (!isAdmin()) {
96
94
  consola.warn('please run as administrator')
97
95
  return
98
96
  }
99
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) {
100
144
  if (process.env.NODE_ENV !== 'development')
101
145
  return
102
146
 
@@ -104,57 +148,45 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
104
148
  enable = true,
105
149
  target = '',
106
150
  } = options
151
+
107
152
  if (!enable)
108
153
  return
154
+
155
+ if (!isAdmin()) {
156
+ consola.warn('please run as administrator')
157
+ return
158
+ }
159
+
109
160
  if (!target) {
110
161
  consola.fail('please provide target')
111
162
  return
112
163
  }
113
164
 
114
- try {
115
- const devServer = {
116
- host: 'localhost',
117
- port: 8080,
118
- ...compiler.options.devServer,
119
- // @ts-expect-error vuecli
120
- ...process.VUE_CLI_SERVICE.projectOptions.devServer,
121
- }
122
- 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
+ }
123
173
 
124
- if (caddy)
125
- return
174
+ const source = `${devServer.host}:${devServer.port}`
126
175
 
127
- // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
128
- const _close = compiler.close?.bind(compiler)
129
- if (_close) {
130
- compiler.close = async (callback: Parameters<typeof _close>[0]) => {
131
- process.on('exit', () => {
132
- _close && _close(callback)
133
- })
134
- }
135
- }
136
- else {
137
- // webpack 4 not support yet
138
- options.https = false
139
- }
176
+ if (caddy)
177
+ return
140
178
 
141
- caddy = new CaddyInstant()
179
+ caddy = new CaddyInstant()
142
180
 
143
- caddy.run(source, target, {
144
- ...options,
145
- }).then(() => {
146
- 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)}/`))
147
185
 
148
- compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
149
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
150
- })
151
- }).catch((e) => {
152
- throw e
153
- })
154
- }
155
- catch (e) {
156
- consola.error(e)
157
- }
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
+ })
158
190
  },
159
191
  })
160
192
 
package/src/nuxt.ts CHANGED
@@ -1,10 +1,8 @@
1
- import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'
2
- import type { ViteDevServer } from 'vite'
3
- import vite from './vite'
4
- import webpack from './webpack'
1
+ import process from 'node:process'
2
+ import { defineNuxtModule } from '@nuxt/kit'
5
3
  import type { Options } from './types'
6
4
  import '@nuxt/schema'
7
- import { consola, once } from './utils'
5
+ import { consola, isAdmin } from './utils'
8
6
  import { vitePrintUrls } from '.'
9
7
 
10
8
  export interface ModuleOptions extends Options {
@@ -17,26 +15,35 @@ export default defineNuxtModule<ModuleOptions>({
17
15
  configKey: 'unpluginHttpsReverseProxy',
18
16
  },
19
17
  setup(options, _nuxt) {
20
- addVitePlugin(() => vite(options))
21
- addWebpackPlugin(() => webpack(options))
18
+ if (process.env.NODE_ENV !== 'development')
19
+ return
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
+ }
22
38
 
23
39
  // https://github.com/nuxt/cli/issues/220#issuecomment-1735394141
24
- let url = ''
25
40
  _nuxt.hook('listen', (_, listener) => {
26
- url = listener.url
27
- })
28
-
29
- _nuxt.hook('vite:serverCreated', once((server: ViteDevServer) => {
41
+ const url = listener.url
30
42
  const source = new URL(url).host
31
43
 
32
- if (!options.target) {
33
- consola.fail('please provide target')
34
- return
35
- }
36
-
37
44
  const base = _nuxt.options.app.baseURL || '/'
38
45
 
39
- vitePrintUrls(server, options, source, options.target, base)
40
- }))
46
+ vitePrintUrls(options, source, options.target, base)
47
+ })
41
48
  },
42
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,15 @@ 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
+
9
20
  export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> {
10
21
  let called = false
11
22
  return (...args: any[]) => {
@@ -1,11 +0,0 @@
1
- import {
2
- unpluginFactory
3
- } from "./chunk-DUCXH7CX.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-DUCXH7CX.js";
4
-
5
- // src/vite.ts
6
- import { createVitePlugin } from "unplugin";
7
- var vite_default = createVitePlugin(unpluginFactory);
8
-
9
- export {
10
- vite_default
11
- };