forked from nuxt/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.ts
87 lines (81 loc) · 2.51 KB
/
local.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Local development module entry
*
* Change `@nuxt/devtools` to the absolute path of this module in any of your Nuxt projects,
* allows you to try Nuxt DevTools locally directly from the source code. HMR is supported
* for the front-end client.
*
* For example, if you clone this repo to `/users/me/nuxt-devtools`, update your nuxt config:
*
* ```diff
* // nuxt.config.ts
* export default defineNuxtConfig({
* modules: [
* - '@nuxt/devtools',
* + '/users/me/nuxt-devtools/local',
* ]
* })
* ```
*/
import { defineNuxtModule, logger } from '@nuxt/kit'
import { resolve } from 'pathe'
import { getPort } from 'get-port-please'
import { searchForWorkspaceRoot } from 'vite'
import { defaultOptions } from './packages/devtools/src/constant'
import type { ModuleOptions } from './packages/devtools/src/types'
import { packageDir } from './packages/devtools/src/dirs'
import { enableModule } from './packages/devtools/src/module-main'
import { startSubprocess } from './packages/devtools-kit/src/index'
export type { ModuleOptions }
export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@nuxt/devtools',
configKey: 'devtools',
},
defaults: defaultOptions,
async setup(options, nuxt) {
const clientDir = resolve(packageDir, 'client')
const workspaceRoot = resolve(packageDir, '../..')
const PORT = await getPort({ port: 12442 })
nuxt.hook('vite:extendConfig', (config) => {
config.server ||= {}
// add proxy to client
config.server.proxy ||= {}
// TODO: ws proxy is not working
config.server.proxy['/__nuxt_devtools__/client'] = {
target: `http://localhost:${PORT}`,
changeOrigin: true,
followRedirects: true,
ws: true,
}
// add fs allow for local modules
config.server.fs ||= {}
config.server.fs.allow ||= [
searchForWorkspaceRoot(process.cwd()),
]
config.server.fs.allow.push(workspaceRoot)
})
nuxt.hook('app:resolve', () => {
startSubprocess(
{
command: 'npx',
args: ['nuxi', 'dev'],
cwd: clientDir,
stdio: 'pipe',
env: {
NUXT_DEVTOOLS_LOCAL: 'true',
PORT: PORT.toString(),
},
},
{
id: 'devtools:local',
name: 'Nuxt DevTools Local',
icon: 'logos-nuxt-icon',
},
nuxt,
)
})
logger.info(`Nuxt DevTools is using local client from \`${clientDir}\` at port \`${PORT}\``)
return enableModule(options, nuxt)
},
})