Skip to content

Commit 4d18c91

Browse files
committed
perf: make cli more efficient by lazy loading deps
1 parent 4c46a5e commit 4d18c91

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

packages/core/src/node/cli-commands.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { existsSync } from 'node:fs'
44
import fs from 'node:fs/promises'
5-
import { createServer } from 'node:http'
65
import {
76
DEVTOOLS_CONNECTION_META_FILENAME,
87
DEVTOOLS_DIRNAME,
@@ -12,17 +11,9 @@ import {
1211
DEVTOOLS_RPC_DUMP_MANIFEST_FILENAME,
1312
} from '@vitejs/devtools-kit/constants'
1413
import c from 'ansis'
15-
import { getPort } from 'get-port-please'
16-
import { createApp, eventHandler, fromNodeMiddleware, sendRedirect, toNodeListener } from 'h3'
17-
import open from 'open'
1814
import { dirname, join, relative, resolve } from 'pathe'
19-
import sirv from 'sirv'
2015
import { dirClientStandalone } from '../dirs'
2116
import { MARK_NODE } from './constants'
22-
import { renderDockImportsMap } from './plugins/server'
23-
import { createDevToolsMiddleware } from './server'
24-
import { startStandaloneDevTools } from './standalone'
25-
import { collectStaticRpcDump } from './static-dump'
2617
import { normalizeHttpServerUrl } from './utils'
2718

2819
export interface StartOptions {
@@ -35,22 +26,29 @@ export interface StartOptions {
3526

3627
export async function start(options: StartOptions) {
3728
const { host } = options
29+
const { getPort } = await import('get-port-please')
3830
const port = await getPort({
3931
host,
4032
port: options.port == null ? undefined : +options.port,
4133
portRange: [9999, 15000],
4234
})
4335

36+
const { startStandaloneDevTools } = await import('./standalone')
37+
const { createDevToolsMiddleware } = await import('./server')
38+
4439
const devtools = await startStandaloneDevTools({
4540
cwd: options.root,
4641
})
47-
4842
const { h3 } = await createDevToolsMiddleware({
4943
cwd: devtools.config.root,
5044
hostWebSocket: host,
5145
context: devtools.context,
5246
})
5347

48+
const { createServer } = await import('node:http')
49+
const { createApp, eventHandler, fromNodeMiddleware, sendRedirect, toNodeListener } = await import('h3')
50+
const { default: sirv } = await import('sirv')
51+
5452
const app = createApp()
5553

5654
for (const { baseUrl, distDir } of devtools.context.views.buildStaticDirs) {
@@ -71,6 +69,7 @@ export async function start(options: StartOptions) {
7169
server.listen(port, host, async () => {
7270
const url = normalizeHttpServerUrl(host, port)
7371
console.log(c.green`${MARK_NODE} Vite DevTools started at`, c.green(url), '\n')
72+
const { default: open } = await import('open')
7473
if (options.open)
7574
await open(url)
7675
})
@@ -86,6 +85,7 @@ export interface BuildOptions {
8685
export async function build(options: BuildOptions) {
8786
console.log(c.cyan`${MARK_NODE} Building static Vite DevTools...`)
8887

88+
const { startStandaloneDevTools } = await import('./standalone')
8989
const devtools = await startStandaloneDevTools({
9090
cwd: options.root,
9191
config: options.config,
@@ -106,11 +106,14 @@ export async function build(options: BuildOptions) {
106106
await fs.cp(distDir, join(outDir, baseUrl), { recursive: true })
107107
}
108108

109+
const { renderDockImportsMap } = await import('./plugins/server')
110+
109111
await fs.mkdir(resolve(devToolsRoot, DEVTOOLS_RPC_DUMP_DIRNAME), { recursive: true })
110112
await fs.writeFile(resolve(devToolsRoot, DEVTOOLS_CONNECTION_META_FILENAME), JSON.stringify({ backend: 'static' }, null, 2), 'utf-8')
111113
await fs.writeFile(resolve(devToolsRoot, DEVTOOLS_DOCK_IMPORTS_FILENAME), renderDockImportsMap(devtools.context.docks.values()), 'utf-8')
112114

113115
console.log(c.cyan`${MARK_NODE} Writing RPC dump to ${resolve(devToolsRoot, DEVTOOLS_RPC_DUMP_MANIFEST_FILENAME)}`)
116+
const { collectStaticRpcDump } = await import('./static-dump')
114117
const dump = await collectStaticRpcDump(
115118
devtools.context.rpc.definitions.values(),
116119
devtools.context,

packages/core/src/node/cli.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import process from 'node:process'
22
import cac from 'cac'
3-
import { build, start } from './cli-commands'
43

54
const cli = cac('vite-devtools')
65

@@ -16,7 +15,10 @@ cli
1615
.option('--base <baseURL>', 'Base URL for deployment', { default: '/' })
1716
.option('--outDir <dir>', 'Output directory', { default: '.vite-devtools' })
1817
// Action
19-
.action(build)
18+
.action(async (options) => {
19+
const { build } = await import('./cli-commands')
20+
return await build(options)
21+
})
2022

2123
cli
2224
.command('', 'Start devtools')
@@ -27,7 +29,10 @@ cli
2729
.option('--port <port>', 'Port', { default: process.env.PORT || 9999 })
2830
.option('--open', 'Open browser', { default: true })
2931
// Action
30-
.action(start)
32+
.action(async (options) => {
33+
const { start } = await import('./cli-commands')
34+
return await start(options)
35+
})
3136

3237
cli.help()
3338
cli.parse()

0 commit comments

Comments
 (0)