An open source design system that's fully customizable and agent ready
TypeScript
perf(cli): read each included file as TypeScript once while it is unchanged (#9422)
Christopher Chedeau83f5da1f04b0
diff --git a/.changeset/cli-ambient-files-read-once.md b/.changeset/cli-ambient-files-read-once.mdnew file mode 100644index 0000000000..678feab05f--- /dev/null+++ b/.changeset/cli-ambient-files-read-once.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[perf] Calling `integrationComponentConflicts()` or `integrationCheck()` from `@astryxdesign/cli/api` more than once in one process now reads each TypeScript file a package's `tsconfig.json` includes once while the file is unchanged, instead of on every call. A single `astryx doctor integration components` or `astryx integration check` run is unchanged.++@vjeuxdiff --git a/packages/cli/foundation/discovery/package-tsconfig.mjs b/packages/cli/foundation/discovery/package-tsconfig.mjsindex c272f1c2f4..9043e03849 100644--- a/packages/cli/foundation/discovery/package-tsconfig.mjs+++ b/packages/cli/foundation/discovery/package-tsconfig.mjs@@ -26,6 +26,16 @@import * as fs from 'node:fs';import * as path from 'node:path';+import {fileStamp, recordContentRead} from '../fs/read-record.mjs';++/**+ * What `isAmbient` found for each file, per TypeScript, kept while the+ * file's stamp (`fileStamp`) is the same. A process that compares a package+ * more than once reads the same config each time, and reading a file the way+ * TypeScript does costs far more than a stat.+ * @type {WeakMap<object, Map<string, {stamp: string, ambient: boolean}>>}+ */+const ambientByFile = new WeakMap();/*** Whether a file the config includes declares types every file sees: a@@ -36,6 +46,30 @@ import * as path from 'node:path';*/function isAmbient(ts, file) {if (/\.d\.[cm]?ts$/.test(file)) return true;+ // A kept answer still counts as reading the file, so an operation that+ // keeps its own result while what it read is unchanged sees a change to it.+ recordContentRead(file);+ const stamp = fileStamp(file);+ if (stamp === '-') return false;+ let known = ambientByFile.get(ts);+ if (known == null) {+ known = new Map();+ ambientByFile.set(ts, known);+ }+ const key = path.resolve(file);+ const seen = known.get(key);+ if (seen && seen.stamp === stamp) return seen.ambient;+ const ambient = readAmbient(ts, file);+ known.set(key, {stamp, ambient});+ return ambient;+}++/**+ * `isAmbient` for a file that is not a declaration file, read afresh.+ * @param {any} ts+ * @param {string} file+ */+function readAmbient(ts, file) {let text;try {text = fs.readFileSync(file, 'utf8');diff --git a/packages/cli/foundation/discovery/package-tsconfig.test.mjs b/packages/cli/foundation/discovery/package-tsconfig.test.mjsindex a7f70cc30b..3735eabcb1 100644--- a/packages/cli/foundation/discovery/package-tsconfig.test.mjs+++ b/packages/cli/foundation/discovery/package-tsconfig.test.mjs@@ -12,6 +12,7 @@ import * as os from 'node:os';import * as path from 'node:path';import {createRequire} from 'node:module';import {afterEach, beforeEach, describe, expect, it} from 'vitest';+import {recordReads} from '../fs/read-record.mjs';import {readPackageTsconfig} from './package-tsconfig.mjs';const require = createRequire(import.meta.url);@@ -81,6 +82,51 @@ describe('readPackageTsconfig', () => {);});+ it('reads an included file as TypeScript once while it is unchanged, and again once it changes', () => {+ const source = write('src/Button.tsx', 'export {};\n');+ write('tsconfig.json', {+ compilerOptions: OPTIONS,+ include: ['src', 'types'],+ });+ // A module: only reading it as TypeScript tells that it is not ambient.+ const file = write('types/maybe.ts', 'export interface Maybe {}\n');+ let reads = 0;+ const counted = {+ ...ts,+ /** @param {any[]} args */+ createSourceFile: (...args) => {+ if (args[0] === file) reads += 1;+ return ts.createSourceFile(...args);+ },+ };+ expect(readPackageTsconfig(counted, root, [source]).declarations).toEqual(+ [],+ );+ // The kept answer still counts as reading the file, for an operation+ // that keeps its own result while what it read is unchanged.+ const seen = new Map();+ expect(+ recordReads(seen, () => readPackageTsconfig(counted, root, [source]))+ .declarations,+ ).toEqual([]);+ expect(reads).toBe(1);+ expect(seen.has(file)).toBe(true);++ // Now a script, a different size: ambient.+ fs.writeFileSync(file, 'interface Maybe {}\n');+ expect(readPackageTsconfig(counted, root, [source]).declarations).toEqual([+ file,+ ]);+ // A module again at the same size, seen by its modification time.+ fs.writeFileSync(file, 'export type X = 1;\n');+ const later = new Date(Date.now() + 2000);+ fs.utimesSync(file, later, later);+ expect(readPackageTsconfig(counted, root, [source]).declarations).toEqual(+ [],+ );+ expect(reads).toBe(2);+ });+it("follows a solution-style config's references, at any depth and through a cycle, to the config that includes the sources", () => {const source = write('src/Button.tsx', 'export {};\n');const chain = [