@@ -3,11 +3,69 @@ import { resolvePath } from 'mlly'
3
3
import type { Nuxt } from '@nuxt/schema'
4
4
import { join , dirname } from 'pathe'
5
5
import { getPort } from 'get-port-please'
6
+ import type { ModuleOptions } from '../../types'
7
+
8
+ export async function setupDevToolsIntegration (
9
+ options : ModuleOptions ,
10
+ nuxt : Nuxt ,
11
+ ) {
12
+ const {
13
+ enabled = 'lazy' ,
14
+ port,
15
+ } = ( typeof options . config !== 'boolean' ? options . config || { } : { } ) . devtools || { }
16
+
17
+ if ( enabled === false )
18
+ return
6
19
7
- export async function setupDevToolsIntegration ( nuxt : Nuxt ) {
8
20
let viewerProcess : ReturnType < typeof import ( '@nuxt/devtools-kit' ) [ 'startSubprocess' ] > | undefined
9
21
let viewerPort : number | undefined
10
22
let viewerUrl : string | undefined
23
+ let started = false
24
+
25
+ async function start ( ) {
26
+ if ( started )
27
+ return
28
+ started = true
29
+ const { startSubprocess } = await import ( '@nuxt/devtools-kit' )
30
+ const inspectorBinPath = join (
31
+ dirname ( await resolvePath (
32
+ '@eslint/config-inspector/package.json' ,
33
+ { url : dirname ( fileURLToPath ( import . meta. url ) ) } ,
34
+ ) ) ,
35
+ 'bin.mjs' ,
36
+ )
37
+
38
+ viewerPort = port || await getPort ( {
39
+ portRange : [ 8123 , 10000 ] ,
40
+ random : true ,
41
+ } )
42
+ viewerProcess = startSubprocess (
43
+ {
44
+ command : 'node' ,
45
+ args : [ inspectorBinPath , '--no-open' ] ,
46
+ cwd : nuxt . options . rootDir ,
47
+ env : {
48
+ PORT : viewerPort . toString ( ) ,
49
+ } ,
50
+ } ,
51
+ {
52
+ id : 'eslint-config-inspector' ,
53
+ name : 'ESLint Config Viewer' ,
54
+ } ,
55
+ nuxt ,
56
+ )
57
+ nuxt . callHook ( 'devtools:customTabs:refresh' )
58
+
59
+ // Wait for viewer to be ready
60
+ const url = `http://localhost:${ viewerPort } `
61
+ for ( let i = 0 ; i < 100 ; i ++ ) {
62
+ if ( await fetch ( url ) . then ( r => r . ok ) . catch ( ( ) => false ) )
63
+ break
64
+ await new Promise ( resolve => setTimeout ( resolve , 500 ) )
65
+ }
66
+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) )
67
+ viewerUrl = url
68
+ }
11
69
12
70
nuxt . hook ( 'devtools:customTabs' , ( tabs ) => {
13
71
tabs . push ( {
@@ -26,50 +84,13 @@ export async function setupDevToolsIntegration(nuxt: Nuxt) {
26
84
{
27
85
label : 'Launch' ,
28
86
pending : ! ! viewerProcess ,
29
- handle : async ( ) => {
30
- const { startSubprocess } = await import ( '@nuxt/devtools-kit' )
31
- const inspectorBinPath = join (
32
- dirname ( await resolvePath (
33
- '@eslint/config-inspector/package.json' ,
34
- { url : dirname ( fileURLToPath ( import . meta. url ) ) } ,
35
- ) ) ,
36
- 'bin.mjs' ,
37
- )
38
-
39
- viewerPort = await getPort ( {
40
- portRange : [ 8123 , 10000 ] ,
41
- random : true ,
42
- } )
43
- viewerProcess = startSubprocess (
44
- {
45
- command : 'node' ,
46
- args : [ inspectorBinPath , '--no-open' ] ,
47
- cwd : nuxt . options . rootDir ,
48
- env : {
49
- PORT : viewerPort . toString ( ) ,
50
- } ,
51
- } ,
52
- {
53
- id : 'eslint-config-inspector' ,
54
- name : 'ESLint Config Viewer' ,
55
- } ,
56
- nuxt ,
57
- )
58
- nuxt . callHook ( 'devtools:customTabs:refresh' )
59
-
60
- // Wait for viewer to be ready
61
- const url = `http://localhost:${ viewerPort } `
62
- for ( let i = 0 ; i < 100 ; i ++ ) {
63
- if ( await fetch ( url ) . then ( r => r . ok ) . catch ( ( ) => false ) )
64
- break
65
- await new Promise ( resolve => setTimeout ( resolve , 500 ) )
66
- }
67
- await new Promise ( resolve => setTimeout ( resolve , 2000 ) )
68
- viewerUrl = url
69
- } ,
87
+ handle : start ,
70
88
} ,
71
89
] ,
72
90
} ,
73
91
} )
74
92
} )
93
+
94
+ if ( enabled === true )
95
+ start ( )
75
96
}
0 commit comments