Skip to content

Commit d067938

Browse files
perf: load esbuild lazily in CLI
## Problem Every tsx CLI invocation loads esbuild's JavaScript wrapper in the parent process, although only `--eval` and `--print` use it there. Importing the wrapper does not start esbuild's native binary. This is therefore a small fixed-cost cleanup, not a major startup optimization. Local paired runs showed roughly a 1-2ms median improvement, small enough to be lost in normal startup variance. ## Changes - Load esbuild dynamically when handling `--eval` or `--print`. - Preserve existing eval and print behavior. - Guard normal CLI startup against resolving esbuild in the parent process.
1 parent 95d0672 commit d067938

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎src/cli.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { constants as osConstants } from 'node:os';
22
import type { ChildProcess, Serializable } from 'node:child_process';
33
import type { Server } from 'node:net';
44
import { cli } from 'cleye';
5-
import {
6-
transformSync as esbuildTransformSync,
7-
} from 'esbuild';
85
import { version } from '../package.json';
96
import { run } from './run.js';
107
import { watchCommand } from './watch/index.js';
@@ -206,6 +203,9 @@ cli({
206203
if (evalType) {
207204
const { inputType } = interceptedFlags;
208205
const evalCode = interceptedFlags[evalType]!;
206+
207+
// Lazy-loaded; most runs don't use --eval/--print
208+
const { transformSync: esbuildTransformSync } = await import('esbuild');
209209
const transformed = esbuildTransformSync(
210210
evalCode,
211211
{

‎tests/specs/cli.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { setTimeout } from 'node:timers/promises';
22
import { on, once } from 'node:events';
3+
import { pathToFileURL } from 'node:url';
34
import {
45
describe, test, onFinish, onTestFail, expect,
56
} from 'manten';
@@ -206,6 +207,34 @@ export const cli = (node: NodeApis) => describe('CLI', () => {
206207
});
207208
});
208209

210+
test('doesn\'t load esbuild without --eval or --print', async () => {
211+
await using fixture = await createFixture({
212+
'block-esbuild.mjs': `
213+
export const resolve = (specifier, context, nextResolve) => {
214+
if (specifier === 'esbuild') {
215+
throw new Error('Unexpected esbuild load');
216+
}
217+
218+
return nextResolve(specifier, context);
219+
};
220+
`,
221+
'index.js': 'console.log("loaded")',
222+
});
223+
224+
const tsxProcess = await tsx(['index.js'], {
225+
cwd: fixture.path,
226+
nodeOptions: [
227+
'--no-warnings',
228+
'--experimental-loader',
229+
pathToFileURL(fixture.getPath('block-esbuild.mjs')).toString(),
230+
],
231+
});
232+
233+
expect(tsxProcess.exitCode).toBe(0);
234+
expect(tsxProcess.stdout).toBe('loaded');
235+
expect(tsxProcess.stderr).toBe('');
236+
});
237+
209238
if (
210239
node.supports.cliTestFlag
211240

0 commit comments

Comments
 (0)