Skip to content

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

TypeScript

fix(cli): a .doc.ts topic loads through jiti, installed or linked, on every supported Node (#10763)

Cindy Zhang3f1678204f20
diff --git a/.changeset/cli-ts-topic-docs-jiti.md b/.changeset/cli-ts-topic-docs-jiti.md
new file mode 100644
index 0000000000..280b817ee0
--- /dev/null
+++ b/.changeset/cli-ts-topic-docs-jiti.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx docs <topic>` reads a topic authored as `.doc.ts` from an installed integration on every supported Node, and from a linked integration below Node 22.18. Topic and docs-tree files now load `.ts` through the same TypeScript loader as component docs, where a plain import failed under `node_modules` ("Stripping types is currently unsupported") and below Node 22.18 ("Unknown file extension .ts"). `astryx search` also reads an installed `.doc.ts` component doc, so it finds the component by its keywords, shows its description, and lists a family's members. `.doc.mjs` topics load as before.
+
+@cixzhang
diff --git a/packages/cli/api/component/integration-export-name.test.mjs b/packages/cli/api/component/integration-export-name.test.mjs
index a0b7de2ac1..bee0d225c1 100644
--- a/packages/cli/api/component/integration-export-name.test.mjs
+++ b/packages/cli/api/component/integration-export-name.test.mjs
@@ -59,9 +59,7 @@ function writeComponent(file, name, source, description, extra = {}) {
}
beforeEach(() => {
- tmpDir = fs.mkdtempSync(
- path.join(REPO_ROOT, '.astryx-export-name-test-'),
- );
+ tmpDir = fs.mkdtempSync(path.join(REPO_ROOT, '.astryx-export-name-test-'));
fs.writeFileSync(
path.join(tmpDir, 'package.json'),
JSON.stringify({name: 'consumer', version: '1.0.0'}),
@@ -591,7 +589,7 @@ describe('a component doc that names another export of its file', () => {
});
it(
- 'indexes a .doc.ts family member as a member when Node cannot load the doc',
+ 'indexes an installed .doc.ts family and its member, as a .doc.mjs family is',
() => {
const fam = path.join(pkgDir, 'src', 'fam');
fs.mkdirSync(fam, {recursive: true});
@@ -607,7 +605,8 @@ describe('a component doc that names another export of its file', () => {
path.join(fam, 'AcmeFamTs.tsx'),
'export function AcmeFamTs() { return null; }\nexport function AcmeFamItem() { return null; }\n',
);
- // The real CLI under Node, which does not strip types in node_modules.
+ // The real CLI under Node, which does not strip types in node_modules:
+ // the doc loads through jiti, so the member is found as in a .doc.mjs.
const run = spawnSync(
process.execPath,
[
@@ -623,6 +622,7 @@ describe('a component doc that names another export of its file', () => {
);
expect(hits.map((/** @type {any} */ hit) => hit.name)).toEqual([
'AcmeFamTs',
+ 'AcmeFamItem',
]);
},
SLOW,
diff --git a/packages/cli/foundation/doc-compiler/import.mjs b/packages/cli/foundation/doc-compiler/import.mjs
index 8232e1448a..39a64032ce 100644
--- a/packages/cli/foundation/doc-compiler/import.mjs
+++ b/packages/cli/foundation/doc-compiler/import.mjs
@@ -145,11 +145,18 @@ export function importTemplateModule(file) {
/**
* Import a module natively, by file URL: how the docs API, search, and the
- * unchecked component and hook readers have always imported docs.
+ * unchecked component and hook readers have always imported docs. A `.ts`
+ * file goes through jiti, as {@link importDocModule} imports it: Node cannot
+ * strip types from a file under node_modules, and below 22.18 it does not
+ * strip types at all, so a plain import of an installed or linked
+ * integration's `.doc.ts` fails.
* @param {string} file
* @returns {Promise<any>}
*/
export function importNativeModule(file) {
+ if (file.endsWith('.ts')) {
+ return importDocModule(file);
+ }
return importCurrent(
file,
'native',
diff --git a/packages/cli/foundation/doc-compiler/read.test.mjs b/packages/cli/foundation/doc-compiler/read.test.mjs
index 14a0266412..419f10531d 100644
--- a/packages/cli/foundation/doc-compiler/read.test.mjs
+++ b/packages/cli/foundation/doc-compiler/read.test.mjs
@@ -303,9 +303,9 @@ export const docsZh = {get props() { throw new TypeError('bad translation'); }};
).rejects.toThrow(/received null/);
});
- it('keeps the unchecked readers on a plain import', () => {
+ it('reads an installed TypeScript doc through the unchecked reader too', () => {
// Outside vitest's own loader: a plain import cannot strip types under
- // node_modules, and the checked loader (jiti) can, as on main.
+ // node_modules, so every reader takes a `.ts` doc through jiti.
const dir = path.join(tmpDir, 'node_modules', 'kit');
fs.mkdirSync(dir, {recursive: true});
const file = path.join(dir, 'Typed.doc.ts');
@@ -329,7 +329,7 @@ catch (e) { console.log('threw', e.message.split('\\n')[0]); }`,
],
{encoding: 'utf8'},
).stdout.trim();
- expect(run('loadDocs')).toMatch(/^threw .*node_modules/);
+ expect(run('loadDocs')).toBe('ok Typed');
expect(run('loadComponentDoc')).toMatch(/^(ok Typed|threw .*is invalid)/);
});
});
@@ -358,19 +358,18 @@ describe('an edited doc file, read again in the same process', () => {
expect((await loadComponentDoc(file)).displayName).toBe('Second, longer');
});
- it('gives each importer its own read of an edited TypeScript doc, as a new process would', async () => {
+ it('reads an edited TypeScript doc again, one jiti read shared by every importer', async () => {
const file = write('Pair.doc.ts', doc('First'));
- const nativeBefore = await importNativeModule(file);
- expect(await importDocModule(file)).not.toBe(nativeBefore);
+ const before = await importNativeModule(file);
+ expect(before.default.displayName).toBe('First');
fs.writeFileSync(file, doc('Second, longer'));
- // The unchecked reader (a plain import) first, then the checked one (jiti).
+ // The unchecked reader first, then the checked one: both read the edit.
expect((await loadDocs(file)).displayName).toBe('Second, longer');
expect((await loadComponentDoc(file)).displayName).toBe('Second, longer');
- const nativeAfter = await importNativeModule(file);
- const docAfter = await importDocModule(file);
- expect(docAfter).not.toBe(nativeAfter);
- expect(await importNativeModule(file)).toBe(nativeAfter);
- expect(await importDocModule(file)).toBe(docAfter);
+ const after = await importNativeModule(file);
+ expect(after).not.toBe(before);
+ expect(await importDocModule(file)).toBe(after);
+ expect(await importNativeModule(file)).toBe(after);
});
it('lowers an edited file again for a whole-project compile', async () => {
diff --git a/packages/cli/foundation/doc-compiler/topic-ts.test.mjs b/packages/cli/foundation/doc-compiler/topic-ts.test.mjs
new file mode 100644
index 0000000000..c14e0a8330
--- /dev/null
+++ b/packages/cli/foundation/doc-compiler/topic-ts.test.mjs
@@ -0,0 +1,112 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Topic and tree docs authored as `.doc.ts`: they load through the same
+ * jiti TypeScript path as component docs, wherever the integration lives.
+ *
+ * Node cannot strip types from a file under node_modules, on any version, and
+ * below 22.18 it does not strip types at all. An installed integration's
+ * `.doc.ts` topic therefore fails a plain `import()` on every Node, and a
+ * linked one fails it below 22.18. These cases run on whatever Node runs the
+ * suite; the linked case is the one that fails on Node 22.13 to 22.17.
+ */
+
+import {afterEach, beforeEach, describe, expect, it} from 'vitest';
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./read.mjs')} */
+const {loadTopicFile, readDocView} = require('./read.mjs');
+/** @type {typeof import('./import.mjs')} */
+const {importNativeModule} = require('./import.mjs');
+
+const TOPIC = (/** @type {string} */ name) => `
+type Doc = {name: string; title: string; category: string; description: string; sections: unknown[]};
+export const docs: Doc = {
+ name: '${name}',
+ title: 'Deploying',
+ category: 'guide',
+ description: 'How to deploy the integration.',
+ sections: [{title: 'Deploy', content: [{type: 'prose', text: 'Run the deploy command.'}]}],
+};
+`;
+
+let tmpDir;
+
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-topic-ts-'));
+});
+
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+});
+
+/**
+ * @param {string} relative
+ * @param {string} text
+ */
+function write(relative, text) {
+ const file = path.join(tmpDir, relative);
+ fs.mkdirSync(path.dirname(file), {recursive: true});
+ fs.writeFileSync(file, text);
+ return file;
+}
+
+describe('a .doc.ts topic', () => {
+ it('loads from an installed integration under node_modules', async () => {
+ const file = write(
+ 'node_modules/@acme/integration/docs/deploying.doc.ts',
+ TOPIC('deploying'),
+ );
+ const result = await loadTopicFile(file, null);
+ expect(result.error).toBeUndefined();
+ expect(result.doc?.name).toBe('deploying');
+ });
+
+ it('loads from a linked integration outside node_modules', async () => {
+ const file = write('linked/docs/deploying.doc.ts', TOPIC('deploying'));
+ const result = await loadTopicFile(file, null);
+ expect(result.error).toBeUndefined();
+ expect(result.doc?.name).toBe('deploying');
+ });
+
+ it('loads a tree doc read through the native loader', async () => {
+ const file = write(
+ 'node_modules/@acme/integration/docs/guides.doc.ts',
+ TOPIC('guides'),
+ );
+ const view = await readDocView(file, {root: 'tree', loader: 'native'});
+ expect(view?.name).toBe('guides');
+ });
+});
+
+describe('a .doc.mjs topic', () => {
+ it('still loads natively, installed or linked', async () => {
+ const text = `export const docs = ${JSON.stringify({
+ name: 'deploying',
+ title: 'Deploying',
+ category: 'guide',
+ description: 'How to deploy the integration.',
+ sections: [
+ {
+ title: 'Deploy',
+ content: [{type: 'prose', text: 'Run the deploy command.'}],
+ },
+ ],
+ })};\n`;
+ for (const relative of [
+ 'node_modules/@acme/integration/docs/deploying.doc.mjs',
+ 'linked/docs/deploying.doc.mjs',
+ ]) {
+ const file = write(relative, text);
+ const result = await loadTopicFile(file, null);
+ expect(result.error).toBeUndefined();
+ expect(result.doc?.name).toBe('deploying');
+ const mod = await importNativeModule(file);
+ expect(Object.keys(mod)).toEqual(['docs']);
+ }
+ });
+});