Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

perf(cli): resolveImportPath reads Core's exports once per operation (#9886)

Christopher Chedeau5876379c7111
diff --git a/.changeset/cli-core-exports-once.md b/.changeset/cli-core-exports-once.md
new file mode 100644
index 0000000000..7fad6877fb
--- /dev/null
+++ b/.changeset/cli-core-exports-once.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[perf] `astryx search` and `astryx build` read Core's exports map once per run, not once per component. With the other readers, a search now reads Core's package.json 10 times instead of 183. Results are unchanged.
+
+@vjeux
diff --git a/packages/cli/foundation/discovery/component-discovery.mjs b/packages/cli/foundation/discovery/component-discovery.mjs
index 007431c3ac..17fdedda6e 100644
--- a/packages/cli/foundation/discovery/component-discovery.mjs
+++ b/packages/cli/foundation/discovery/component-discovery.mjs
@@ -766,12 +766,7 @@ function caseSpelledComponentSource(srcDir, name) {
*/
export function resolveImportPath(coreDir, componentName) {
const srcDir = path.join(coreDir, 'src');
- const pkgPath = path.join(coreDir, 'package.json');
- const pkg = pathExists(pkgPath)
- ? JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
- : null;
-
- const exportKeys = Object.keys(pkg?.exports || {});
+ const exportKeys = exportKeysOf(coreDir);
// Priority 1: exact subpath export matching the component name (e.g. ./Heading)
// This allows convenience re-export directories to win over the source directory.
@@ -799,6 +794,22 @@ export function resolveImportPath(coreDir, componentName) {
return '@astryxdesign/core';
}
+/**
+ * The subpaths the package's `exports` map names, read once per operation:
+ * a search resolves the import of every component.
+ * @param {string} coreDir
+ * @returns {string[]}
+ */
+function exportKeysOf(coreDir) {
+ const pkgPath = path.join(coreDir, 'package.json');
+ return oncePerListing(`export-keys\0${pkgPath}`, () => {
+ const pkg = pathExists(pkgPath)
+ ? JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
+ : null;
+ return Object.keys(pkg?.exports || {});
+ });
+}
+
/**
* Does a package's `exports` map publish this subpath?
*
diff --git a/packages/cli/foundation/discovery/component-discovery.test.mjs b/packages/cli/foundation/discovery/component-discovery.test.mjs
index 4a60a4ae95..2731310e19 100644
--- a/packages/cli/foundation/discovery/component-discovery.test.mjs
+++ b/packages/cli/foundation/discovery/component-discovery.test.mjs
@@ -13,6 +13,7 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
+import {withListingCache} from '../fs/listing-cache.mjs';
import {
CORE_PACKAGE,
discoverComponents,
@@ -330,6 +331,22 @@ describe('findComponentReadme / findComponentSource / resolveImportPath', () =>
expect(resolveImportPath(core, 'Nope')).toBe('@astryxdesign/core');
});
+ it('reads the exports map once per operation and fresh outside one', () => {
+ const core = mkTmp('as-rip-once-');
+ fs.mkdirSync(path.join(core, 'src'));
+ const pkgPath = path.join(core, 'package.json');
+ const writeExports = exports =>
+ fs.writeFileSync(pkgPath, JSON.stringify({exports}));
+ writeExports({'./Card': './dist/Card.js'});
+ withListingCache(() => {
+ expect(resolveImportPath(core, 'Card')).toBe('@astryxdesign/core/Card');
+ writeExports({});
+ // The operation read the map once, so the edit does not reach it.
+ expect(resolveImportPath(core, 'Card')).toBe('@astryxdesign/core/Card');
+ });
+ expect(resolveImportPath(core, 'Card')).toBe('@astryxdesign/core');
+ });
+
it('throws when src/ is missing', () => {
const core = mkTmp('as-fcr-nosrc-');
expect(() => findComponentReadme(core, 'Button')).toThrow(/ENOENT/);