unplugin-https-reverse-proxy 1.0.0-beta.4 → 1.0.0-beta.7

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 { existsSync } from 'node:fs'
3
2
  import type { UnpluginFactory } from 'unplugin'
4
3
  import { createUnplugin } from 'unplugin'
5
4
  import type { ResolvedConfig } from 'vite'
6
5
  import c from 'picocolors'
7
- import { chmodRecursive, consola, isAdmin, once } from './utils'
6
+ import { consola, isAdmin, once } from './utils'
8
7
  import type { Options } from './types'
9
8
  import { CaddyInstant } from './caddy'
10
9
 
@@ -23,31 +22,18 @@ export const vitePrintUrls = once((
23
22
  ) => {
24
23
  _printUrls?.()
25
24
 
26
- // fix `Error: EPERM: operation not permitted`
27
- const viteCacheDir = `${pwd}/node_modules/.vite`
28
- if (existsSync(viteCacheDir))
29
- chmodRecursive(viteCacheDir, 0o777)
30
- const nuxtCacheDir = `${pwd}/.nuxt`
31
- if (existsSync(nuxtCacheDir))
32
- chmodRecursive(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
- }
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
+ })
51
37
  })
52
38
 
53
39
  export const unpluginFactory: UnpluginFactory<Options> = options => ({
@@ -58,14 +44,14 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
58
44
  config = _config
59
45
  },
60
46
  configureServer(server) {
47
+ if (config.command !== 'serve')
48
+ return
49
+
61
50
  if (!isAdmin()) {
62
51
  consola.warn('please run as administrator')
63
52
  return
64
53
  }
65
54
 
66
- if (config.command === 'build')
67
- return
68
-
69
55
  const {
70
56
  enable = true,
71
57
  target = '',
@@ -92,14 +78,14 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
92
78
  },
93
79
  },
94
80
  webpack(compiler) {
81
+ if (process.env.NODE_ENV !== 'development')
82
+ return
83
+
95
84
  if (!isAdmin()) {
96
85
  consola.warn('please run as administrator')
97
86
  return
98
87
  }
99
88
 
100
- if (process.env.NODE_ENV !== 'development')
101
- return
102
-
103
89
  const {
104
90
  enable = true,
105
91
  target = '',
@@ -111,54 +97,45 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
111
97
  return
112
98
  }
113
99
 
114
- const nuxtCacheDir = `${pwd}/node_modules/.cache`
115
- if (existsSync(nuxtCacheDir))
116
- chmodRecursive(nuxtCacheDir, 0o777)
117
-
118
- try {
119
- const devServer = {
120
- host: 'localhost',
121
- port: 8080,
122
- ...compiler.options.devServer,
123
- // @ts-expect-error vuecli
124
- ...process.VUE_CLI_SERVICE.projectOptions.devServer,
125
- }
126
- const source = `${devServer.host}:${devServer.port}`
100
+ const devServer = {
101
+ host: 'localhost',
102
+ port: 8080,
103
+ ...compiler.options.devServer,
104
+ // @ts-expect-error vuecli
105
+ ...process.VUE_CLI_SERVICE.projectOptions.devServer,
106
+ }
107
+ const source = `${devServer.host}:${devServer.port}`
127
108
 
128
- if (caddy)
129
- return
109
+ if (caddy)
110
+ return
130
111
 
131
- // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
132
- const _close = compiler.close?.bind(compiler)
133
- if (_close) {
134
- compiler.close = async (callback: Parameters<typeof _close>[0]) => {
135
- process.on('exit', () => {
136
- _close && _close(callback)
137
- })
138
- }
139
- }
140
- else {
141
- // webpack 4 not support yet
142
- options.https = false
112
+ // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
113
+ const _close = compiler.close?.bind(compiler)
114
+ if (_close) {
115
+ compiler.close = async (callback: Parameters<typeof _close>[0]) => {
116
+ process.on('exit', () => {
117
+ _close && _close(callback)
118
+ })
143
119
  }
120
+ }
121
+ else {
122
+ // webpack 4 not support yet
123
+ options.https = false
124
+ }
144
125
 
145
- caddy = new CaddyInstant()
126
+ caddy = new CaddyInstant()
146
127
 
147
- caddy.run(source, target, {
148
- ...options,
149
- }).then(() => {
150
- const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
128
+ caddy.run(source, target, {
129
+ ...options,
130
+ }).then(() => {
131
+ const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
151
132
 
152
- compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
153
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
154
- })
155
- }).catch((e) => {
156
- throw e
133
+ compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
134
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
157
135
  })
158
- }
159
- catch (e) {
160
- consola.error(e)
161
- }
136
+ }).catch((e) => {
137
+ throw e
138
+ })
162
139
  },
163
140
  })
164
141
 
package/src/nuxt.ts CHANGED
@@ -15,14 +15,14 @@ export default defineNuxtModule<ModuleOptions>({
15
15
  configKey: 'unpluginHttpsReverseProxy',
16
16
  },
17
17
  setup(options, _nuxt) {
18
+ if (process.env.NODE_ENV !== 'development')
19
+ return
20
+
18
21
  if (!isAdmin()) {
19
22
  consola.warn('please run as administrator')
20
23
  return
21
24
  }
22
25
 
23
- if (process.env.NODE_ENV !== 'development')
24
- return
25
-
26
26
  const {
27
27
  enable = true,
28
28
  target = '',