@@ -3,11 +3,69 @@ import { resolvePath } from 'mlly'
33import type { Nuxt } from '@nuxt/schema'
44import { join , dirname } from 'pathe'
55import { 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
619
7- export async function setupDevToolsIntegration ( nuxt : Nuxt ) {
820 let viewerProcess : ReturnType < typeof import ( '@nuxt/devtools-kit' ) [ 'startSubprocess' ] > | undefined
921 let viewerPort : number | undefined
1022 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+ }
1169
1270 nuxt . hook ( 'devtools:customTabs' , ( tabs ) => {
1371 tabs . push ( {
@@ -26,50 +84,13 @@ export async function setupDevToolsIntegration(nuxt: Nuxt) {
2684 {
2785 label : 'Launch' ,
2886 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 ,
7088 } ,
7189 ] ,
7290 } ,
7391 } )
7492 } )
93+
94+ if ( enabled === true )
95+ start ( )
7596}
0 commit comments