Skip to content

Commit a0b35fc

Browse files
authored
Merge pull request #22 from rhyek/refactor/extensionless-imports-tsdown
Build the package with tsdown, and drop .js from relative imports
2 parents 53b9a76 + 363c0a9 commit a0b35fc

233 files changed

Lines changed: 1450 additions & 1059 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CLAUDE.md‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,21 @@ Build with:
141141
pnpm build
142142
```
143143

144-
The root `build` script runs `pnpm --filter stackctl build`, which cleans
145-
`dist/`, compiles with `tsc -p tsconfig.build.json`, and marks the compiled CLI
146-
(`dist/cli.js`) executable, then `pnpm docs:package`, which builds the docs site and copies
144+
The root `build` script runs `pnpm --filter stackctl build`, which is tsdown
145+
(`packages/stackctl/tsdown.config.ts`), then `pnpm docs:package`, which builds the docs site and copies
147146
its `llms.txt` and pages into `packages/stackctl/docs/`. CI (`.github/workflows/ci.yaml`) and `release.yaml` use this same
148147
command.
149148

149+
- **tsdown in unbundle mode**: one `dist/*.js` (with its map and `.d.ts`) per source file, as
150+
`tsc` would lay it out. It cleans `dist/`, marks `cli.js` executable, and type-checks while it
151+
writes the declarations (`dts.typecheck`), so a type error fails the build.
152+
- **Relative imports carry no extension** (`from './config'`); the build writes them out as
153+
`./config.js`, and the tsconfig resolves them as a bundler does. Node can't run the sources
154+
as they are: a script that imports `src/` under Node's TypeScript support needs a resolve hook,
155+
as `scripts/focus-preview.ts` has.
156+
- **Tests that run the compiled CLI** build it with `buildForTests` (`src/cli-spec/build.ts`),
157+
the same config with no maps or declarations, into a folder of their own.
158+
150159
## Changelog and releases
151160

152161
A merged pull request whose `CHANGELOG.md` has a new top `## X.Y.Z` heading **is** a release:

‎maintenance/scripts/check-source-maps.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// node maintenance/scripts/check-source-maps.ts
55
//
6-
// TypeScript writes each map's sources relative to the map, so `dist/tui/layout.js.map` names
6+
// The build writes each map's sources relative to the map, so `dist/tui/layout.js.map` names
77
// `../../src/tui/layout.ts`. That holds only while the build writes into `dist/`, beside `src/`:
88
// compiled anywhere else, the relative path climbs out of the package and through the build
99
// machine's own directories, and the published maps carry them. The release runs this before it

‎packages/stackctl/e2e/cli.ts‎

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,20 @@
66
* and may be running at the same time. It is laid out as the published package is, `dist/` beside `assets/` and
77
* `skills/`, which the compiled code reads from its own directory's parent.
88
*/
9-
import { execFile } from 'node:child_process';
109
import fs from 'node:fs';
1110
import path from 'node:path';
1211
import { fileURLToPath } from 'node:url';
13-
import { promisify } from 'node:util';
12+
import { buildForTests } from '../src/cli-spec/build';
1413

1514
export const packageDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
1615
const copyDir = path.join(packageDir, 'node_modules', '.cache', 'cli-e2e');
1716
export const buildDir = path.join(copyDir, 'dist');
1817
export const cliPath = path.join(buildDir, 'cli.js');
1918

20-
/**
21-
* TypeScript's `bin/tsc`, from the nearest `node_modules/typescript` at or above `dir`. (The global
22-
* setup runs where `import.meta.resolve` isn't available, and TypeScript 7 exports no `bin/tsc`.)
23-
*/
24-
function findTsc(dir: string): string {
25-
const tsc = path.join(dir, 'node_modules', 'typescript', 'bin', 'tsc');
26-
if (fs.existsSync(tsc)) {
27-
return tsc;
28-
}
29-
const parent = path.dirname(dir);
30-
if (parent === dir) {
31-
throw new Error('typescript is not installed');
32-
}
33-
return findTsc(parent);
34-
}
35-
3619
export async function buildCli(): Promise<void> {
37-
const tsc = findTsc(packageDir);
3820
fs.rmSync(copyDir, { recursive: true, force: true });
3921
for (const dir of ['assets', 'skills']) {
4022
fs.cpSync(path.join(packageDir, dir), path.join(copyDir, dir), { recursive: true });
4123
}
42-
await promisify(execFile)(
43-
process.execPath,
44-
[
45-
tsc,
46-
'-p',
47-
path.join(packageDir, 'tsconfig.build.json'),
48-
'--outDir',
49-
buildDir,
50-
// A build the tests only run: no source maps, which would name sources outside the
51-
// build's folder, and no declarations.
52-
'--sourceMap',
53-
'false',
54-
'--declaration',
55-
'false',
56-
],
57-
{ cwd: packageDir },
58-
);
24+
await buildForTests(buildDir);
5925
}

‎packages/stackctl/e2e/exit.e2e.spec.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import fs from 'node:fs';
66
import net from 'node:net';
77
import path from 'node:path';
88
import { expect, test } from 'vitest';
9-
import { PORTS, sessionRecord } from './fixture.js';
10-
import { track } from './processes.js';
11-
import { Pty } from './pty.js';
12-
import { expectTerminalRestored, fixtureWorkspace } from './suite.js';
9+
import { PORTS, sessionRecord } from './fixture';
10+
import { track } from './processes';
11+
import { Pty } from './pty';
12+
import { expectTerminalRestored, fixtureWorkspace } from './suite';
1313

1414
const ws = fixtureWorkspace();
1515

‎packages/stackctl/e2e/fixture.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import fs from 'node:fs';
77
import os from 'node:os';
88
import path from 'node:path';
9-
import { buildDir, packageDir } from './cli.js';
9+
import { buildDir, packageDir } from './cli';
1010

1111
/**
1212
* The ports the fixture uses, from stackctl's block in the port registry (21050–21099): 21080–21089,

‎packages/stackctl/e2e/global-setup.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { buildCli } from './cli.js';
1+
import { buildCli } from './cli';
22

33
/** Compiles the CLI once for every e2e file (see `cli.ts`). */
44
export default async function setup(): Promise<void> {

‎packages/stackctl/e2e/guidance.e2e.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import fs from 'node:fs';
66
import path from 'node:path';
77
import { expect, test } from 'vitest';
8-
import { Pty } from './pty.js';
9-
import { fixtureWorkspace } from './suite.js';
8+
import { Pty } from './pty';
9+
import { fixtureWorkspace } from './suite';
1010

1111
const ws = fixtureWorkspace();
1212

‎packages/stackctl/e2e/headless.e2e.spec.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import fs from 'node:fs';
66
import path from 'node:path';
77
import { expect, test } from 'vitest';
8-
import { isProcessAlive } from '../src/process-info.js';
9-
import { PORTS, sessionRecord } from './fixture.js';
10-
import { CliProcess, runCli, track, until } from './processes.js';
11-
import { fixtureWorkspace } from './suite.js';
8+
import { isProcessAlive } from '../src/process-info';
9+
import { PORTS, sessionRecord } from './fixture';
10+
import { CliProcess, runCli, track, until } from './processes';
11+
import { fixtureWorkspace } from './suite';
1212

1313
const ws = fixtureWorkspace();
1414

‎packages/stackctl/e2e/one-off.e2e.spec.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { execFileSync, spawnSync } from 'node:child_process';
66
import fs from 'node:fs';
77
import path from 'node:path';
88
import { beforeAll, expect, test } from 'vitest';
9-
import { cliPath } from './cli.js';
10-
import { PORTS } from './fixture.js';
11-
import { CliProcess, childEnv, runCli } from './processes.js';
12-
import { Pty } from './pty.js';
13-
import { fixtureWorkspace } from './suite.js';
9+
import { cliPath } from './cli';
10+
import { PORTS } from './fixture';
11+
import { CliProcess, childEnv, runCli } from './processes';
12+
import { Pty } from './pty';
13+
import { fixtureWorkspace } from './suite';
1414

1515
const ws = fixtureWorkspace();
1616

‎packages/stackctl/e2e/picker.e2e.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import fs from 'node:fs';
55
import path from 'node:path';
66
import { expect, test } from 'vitest';
7-
import { Pty } from './pty.js';
8-
import { allRunning, expectTerminalRestored, fixtureWorkspace } from './suite.js';
7+
import { Pty } from './pty';
8+
import { allRunning, expectTerminalRestored, fixtureWorkspace } from './suite';
99

1010
const ws = fixtureWorkspace();
1111

0 commit comments

Comments
 (0)