@astryxdesign/cli 0.1.6-canary.eba4f34 → 0.1.6-canary.f87204e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/docs/integration-authoring.md +105 -0
  2. package/docs/internationalization.doc.mjs +243 -0
  3. package/package.json +13 -9
  4. package/src/api/integration-block-exports.test.mjs +240 -0
  5. package/src/api/template-suffix.test.mjs +246 -0
  6. package/src/api/template.mjs +104 -28
  7. package/src/api/validate-integration.mjs +0 -8
  8. package/src/config.mjs +5 -14
  9. package/src/doc.mjs +27 -0
  10. package/src/doc.test.mjs +383 -0
  11. package/src/integration.mjs +4 -15
  12. package/src/lib/component-discovery.importpath.test.mjs +59 -0
  13. package/src/lib/component-discovery.mjs +15 -5
  14. package/src/lib/component-format.mjs +45 -13
  15. package/src/lib/component-format.test.mjs +95 -1
  16. package/src/lib/component-loader.mjs +104 -2
  17. package/src/lib/componentDocOverlay.test.mjs +111 -0
  18. package/src/lib/config-schema.mjs +0 -30
  19. package/src/lib/hook-format.mjs +8 -3
  20. package/src/lib/xle/registry.mjs +0 -5
  21. package/src/schemas/doc-schema.mjs +226 -0
  22. package/src/schemas/template-schema.mjs +47 -0
  23. package/src/template.mjs +9 -67
  24. package/src/types/config.d.ts +11 -66
  25. package/src/types/doc.d.ts +23 -0
  26. package/src/types/integration.d.ts +7 -18
  27. package/src/types/template-api.d.ts +14 -50
  28. package/templates/blocks/components/Avatar/AvatarGroup.tsx +5 -7
  29. package/templates/blocks/components/Avatar/AvatarShowcase.tsx +4 -6
  30. package/templates/blocks/components/Avatar/AvatarUserCard.tsx +3 -5
  31. package/templates/blocks/components/Avatar/AvatarWithImage.tsx +8 -6
  32. package/templates/blocks/components/Avatar/AvatarWithStatus.tsx +3 -5
  33. package/templates/blocks/components/ChatComposerInput/ChatComposerInputControlledInput.tsx +1 -1
  34. package/templates/blocks/components/ChatComposerInput/ChatComposerInputDisabled.tsx +1 -1
  35. package/templates/blocks/components/ChatComposerInput/ChatComposerInputMentionTrigger.tsx +1 -1
  36. package/templates/blocks/components/ChatComposerInput/ChatComposerInputMultipleTriggers.tsx +1 -1
  37. package/templates/blocks/components/ChatComposerInput/ChatComposerInputShowcase.tsx +1 -1
  38. package/templates/blocks/components/ChatComposerInput/ChatComposerInputSlashCommands.tsx +1 -1
  39. package/templates/blocks/components/VisuallyHidden/VisuallyHiddenLiveRegion.doc.mjs +14 -0
  40. package/templates/blocks/components/VisuallyHidden/VisuallyHiddenLiveRegion.tsx +41 -0
  41. package/templates/blocks/components/VisuallyHidden/VisuallyHiddenShowcase.doc.mjs +13 -0
  42. package/templates/blocks/components/VisuallyHidden/VisuallyHiddenShowcase.tsx +78 -0
  43. package/templates/blocks/components/VisuallyHidden/VisuallyHiddenStructuralHeading.doc.mjs +14 -0
  44. package/templates/blocks/components/VisuallyHidden/VisuallyHiddenStructuralHeading.tsx +38 -0
  45. package/templates/blocks/components/VisuallyHidden/VisuallyHiddenSupplementaryContext.doc.mjs +14 -0
  46. package/templates/blocks/components/VisuallyHidden/VisuallyHiddenSupplementaryContext.tsx +47 -0
  47. package/templates/pages/theme-showcase/page.tsx +7 -7
  48. package/templates/themes/neutral/neutralTheme.ts +63 -32
@@ -0,0 +1,105 @@
1
+ # Authoring an Astryx Integration
2
+
3
+ > **Status:** working notes. This should eventually move to the public wiki
4
+ > alongside the rest of the integration-authoring guidance; it lives here for
5
+ > now so it ships and is versioned with the CLI.
6
+
7
+ An **Integration** is an npm package that contributes components, templates, and/or
8
+ codemods to a consumer's design-system workflow. Consumers install the 3rd party
9
+ package, add a line to their astryx.config file:
10
+
11
+ ```js
12
+ import {createConfig} from '@astryxdesign/cli/config';
13
+
14
+ export default createConfig({
15
+ integrations: ['@acme/astryx-widgets'],
16
+ ...
17
+ });
18
+ ```
19
+
20
+ Then the integration's components and templates will be surfaced alongside Astryx
21
+ components in the Astryx CLI.
22
+
23
+ ```sh
24
+ astryx component AcmeCarousel --props
25
+ astryx component --list --package @acme/astryx-widgets
26
+ ```
27
+
28
+ ## The Integration File
29
+
30
+ In order to register your package as an Astryx Integration, create an
31
+ `astryx.integration.{ts,mjs,js}` file as a sibling to your `package.json`. This file
32
+ tells Astryx where to find your components, templates, codemods, etc.
33
+
34
+ ```js
35
+ // astryx.integration.{ts,mjs,js}
36
+ import {createIntegration} from '@astryxdesign/cli/integration';
37
+
38
+ export default createIntegration({
39
+ components: './components',
40
+ templates: './templates',
41
+ codemods: './codemods',
42
+ issuesUrl: 'https://github.com/acme/widgets/issues',
43
+ });
44
+ ```
45
+
46
+ ## Components
47
+
48
+ Your components themselves may be exported from your library as you see fit (consumers
49
+ will still import them from your package) but Astryx CLI will look for a .doc.{ts,mjs,js}
50
+ file with the same stem e.g. `AcmeCarousel.tsx` and `AcmeCarousel.doc.ts`.
51
+
52
+ ```js
53
+ // AcmeCarousel.doc.ts
54
+ import {createComponentDoc} from '@astryxdesign/cli/doc';
55
+
56
+ export default createComponentDoc({
57
+ name: 'AcmeCarousel',
58
+ description: '...',
59
+ ...
60
+ });
61
+ ```
62
+
63
+ ## Templates
64
+
65
+ Templates are typically not exported from the package directly, but instead accessed
66
+ via the Astryx CLI. Consumers can look through your templates and materialize them
67
+ into their apps.
68
+
69
+ You define a template with the `createPageTemplate` (for full pages) or `createBlockTemplate`
70
+ (for smaller chunks). e.g. `AcmeLandingPage.tsx`, `AcmeLandingPage.template.ts`
71
+
72
+ ```js
73
+ // AcmeLandingPage.template.ts
74
+ import {createPageTemplate} from '@astryxdesign/cli/template';
75
+
76
+ export default createPageTemplate({
77
+ ...
78
+ });
79
+ ```
80
+
81
+ Note that, since the CLI needs access to the template source code, you need to make sure
82
+ that it is included in your published package. This will also allow us to render previews
83
+ of templates in the future by bundling your template into a doc site build.
84
+
85
+ Typically, this is done via the package.json `exports` key.
86
+
87
+ ```jsonc
88
+ {
89
+ "exports": {
90
+ // ...
91
+ "./templates/*.tsx": "./templates/*.tsx",
92
+ },
93
+ }
94
+ ```
95
+
96
+ In order to verify that it's working, you can test importing the template component like this:
97
+
98
+ ```ts
99
+ import('@acme/astryx-widgets/templates/AcmeLandingPage.tsx');
100
+ ```
101
+
102
+ Import **with the `.tsx` extension** — an extensionless specifier won't resolve
103
+ under `moduleResolution: bundler`. The extensionful `"./templates/*.tsx"` export
104
+ above is what lets that import type-check without consumers enabling
105
+ `allowImportingTsExtensions`.
@@ -0,0 +1,243 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /** @type {import('../../core/src/docs-types').ReferenceDoc} */
4
+
5
+ export const docs = {
6
+ name: 'internationalization',
7
+ title: 'Internationalization',
8
+ category: 'guide',
9
+ description:
10
+ 'Localize astryx component strings, provide translation catalogs, override default text, coexist with your own i18n library, swap languages at runtime, and test translations with the pseudo locale.',
11
+
12
+ sections: [
13
+ {
14
+ title: 'Quick Start',
15
+ category: 'guide',
16
+ content: [
17
+ {
18
+ type: 'prose',
19
+ text: 'Internationalization ships with `@astryxdesign/core`. There is nothing to install. Wrap your app in `<InternationalizationProvider>` and set a `locale` — astryx components pick up localized strings automatically.',
20
+ },
21
+ {
22
+ type: 'code',
23
+ lang: 'tsx',
24
+ label: 'Wrap your app',
25
+ code: `import {InternationalizationProvider} from '@astryxdesign/core';
26
+
27
+ function App() {
28
+ return (
29
+ <InternationalizationProvider locale="en">
30
+ <YourApp />
31
+ </InternationalizationProvider>
32
+ );
33
+ }`,
34
+ },
35
+ {
36
+ type: 'code',
37
+ lang: 'tsx',
38
+ label: 'Read strings inside a component',
39
+ code: `import {useTranslator} from '@astryxdesign/core';
40
+
41
+ function SaveButton() {
42
+ const t = useTranslator();
43
+ return <button>{t('@myapp.actions.save')}</button>;
44
+ }`,
45
+ },
46
+ {
47
+ type: 'prose',
48
+ text: 'The hook is available to consumer components too, but using it is entirely optional — many teams keep their app strings on their existing i18n library (react-intl, i18next, next-intl, LinguiJS) and only use `useTranslator` when reading astryx keys. If you do route your own strings through it, we recommend namespacing them (`@myapp.*` or your npm scope) to keep them separated from `@astryx.*`, but this is a convention, not a requirement — the resolver treats every key as an opaque string.',
49
+ },
50
+ {
51
+ type: 'prose',
52
+ text: "Astryx ships translations only for English today. First-party translations for other locales are on the roadmap and coming soon — track https://github.com/facebook/astryx/issues/3641. In the meantime, if you want astryx UI translated into another locale, you can ship your own catalog through the `messages` prop (covered in the next section). If you're using `useTranslator` for your own strings, you'll want to ship your own catalog either way — astryx only carries the fallback for `@astryx.*` keys, not the ones you author.",
53
+ },
54
+ ],
55
+ },
56
+ {
57
+ title: 'Providing locale catalogs',
58
+ category: 'guide',
59
+ content: [
60
+ {
61
+ type: 'prose',
62
+ text: 'Astryx bundles only the English catalog today. To render in any other locale, provide a translation catalog through the `messages` prop and set `locale` accordingly. This matches how MUI, Ant Design, and AG Grid work — the consumer app supplies the catalogs it actually needs so unused translations stay out of the bundle.',
63
+ },
64
+ {
65
+ type: 'code',
66
+ lang: 'tsx',
67
+ label: 'Add French',
68
+ code: `import {InternationalizationProvider} from '@astryxdesign/core';
69
+ import fr from './locales/astryx/fr.json';
70
+
71
+ <InternationalizationProvider locale="fr" messages={{fr}}>
72
+ <App />
73
+ </InternationalizationProvider>;`,
74
+ },
75
+ {
76
+ type: 'prose',
77
+ text: "See `@astryxdesign/core/locales/en.json` for the full inventory of keys to translate. Copy it as the starting point — every key you translate replaces the English default; anything you omit falls back through the locale chain to English (e.g. `pt-BR` walks to `pt` then to shipped `en`), so a partial translation renders as a mix rather than empty text or raw key names.",
78
+ },
79
+ {
80
+ type: 'prose',
81
+ text: 'A community-maintained set of astryx translations is on the roadmap but not shipped yet. For now, consumer apps that ship in multiple languages own their astryx catalogs alongside their app catalogs. Contributions to a first-party set are welcome — track discussion at https://github.com/facebook/astryx/issues/3641.',
82
+ },
83
+ ],
84
+ },
85
+ {
86
+ title: "Overriding astryx's default text",
87
+ category: 'guide',
88
+ content: [
89
+ {
90
+ type: 'prose',
91
+ text: 'Use `overrides` to change individual strings without shipping a full catalog. Overrides are keyed by locale and merged on top of the built-in and user-supplied catalogs.',
92
+ },
93
+ {
94
+ type: 'code',
95
+ lang: 'tsx',
96
+ label: 'Change one string in English',
97
+ code: `<InternationalizationProvider
98
+ locale="en"
99
+ overrides={{en: {'@astryx.pagination.next': 'Next →'}}}
100
+ >
101
+ <App />
102
+ </InternationalizationProvider>`,
103
+ },
104
+ {
105
+ type: 'prose',
106
+ text: 'Overrides win over both bundled English and any `messages` catalog for the same key. Use them for brand voice tweaks or one-off wording changes.',
107
+ },
108
+ ],
109
+ },
110
+ {
111
+ title: 'Using astryx with your own i18n library',
112
+ category: 'guide',
113
+ content: [
114
+ {
115
+ type: 'prose',
116
+ text: "Astryx components render astryx strings through astryx's provider. Consumer components render consumer strings through whatever i18n library you already use — react-intl, i18next, next-intl, LinguiJS, and so on. The two systems coexist and read from the same source of truth for the active locale.",
117
+ },
118
+ {
119
+ type: 'code',
120
+ lang: 'tsx',
121
+ label: 'Astryx + react-intl side by side',
122
+ code: `import {InternationalizationProvider} from '@astryxdesign/core';
123
+ import {Selector} from '@astryxdesign/core/Selector';
124
+ import {Button} from '@astryxdesign/core/Button';
125
+ import {FormattedMessage, IntlProvider, useIntl} from 'react-intl';
126
+ import astryxFr from './locales/astryx/fr.json'; // astryx's UI, in French
127
+ import appFr from './locales/app/fr.json'; // your app strings, in French
128
+
129
+ function Pricing() {
130
+ // Consumer strings — resolved by react-intl.
131
+ const intl = useIntl();
132
+
133
+ return (
134
+ <section>
135
+ <h1><FormattedMessage id="pricing.heading" /></h1>
136
+
137
+ {/* Astryx Selector — trigger placeholder, search-box placeholder,
138
+ clear-button aria-label all resolved by
139
+ <InternationalizationProvider>. Options come from react-intl. */}
140
+ <Selector
141
+ label={intl.formatMessage({id: 'pricing.region.label'})}
142
+ options={[
143
+ {value: 'na', label: intl.formatMessage({id: 'pricing.region.na'})},
144
+ {value: 'eu', label: intl.formatMessage({id: 'pricing.region.eu'})},
145
+ ]}
146
+ hasSearch
147
+ hasClear
148
+ />
149
+
150
+ <Button label={intl.formatMessage({id: 'pricing.cta.subscribe'})} />
151
+ </section>
152
+ );
153
+ }
154
+
155
+ export default function App() {
156
+ return (
157
+ // Same locale, two providers reading their own catalogs.
158
+ <IntlProvider locale="fr" messages={appFr}>
159
+ <InternationalizationProvider locale="fr" messages={{fr: astryxFr}}>
160
+ <Pricing />
161
+ </InternationalizationProvider>
162
+ </IntlProvider>
163
+ );
164
+ }`,
165
+ },
166
+ {
167
+ type: 'prose',
168
+ text: 'Keep the two providers in sync on locale, and each library owns its own catalog. Astryx never sees your app strings, and your i18n library never sees astryx internals. Runtime locale swap works the same way — re-render both providers with a new `locale` prop and the whole tree updates live.',
169
+ },
170
+ {
171
+ type: 'prose',
172
+ text: "Single-catalog usage — where an external i18n runtime like react-intl or i18next resolves both your app strings AND astryx's strings through one provider — is on the roadmap via a `Translator` adapter. Track https://github.com/facebook/astryx/issues/4029. For now, run the two providers side by side as shown above.",
173
+ },
174
+ ],
175
+ },
176
+ {
177
+ title: 'Runtime language swap',
178
+ category: 'guide',
179
+ content: [
180
+ {
181
+ type: 'prose',
182
+ text: 'Re-render `<InternationalizationProvider>` with a new `locale` prop and every astryx string updates live. No reload, no separate API call.',
183
+ },
184
+ {
185
+ type: 'code',
186
+ lang: 'tsx',
187
+ label: 'Toggle between locales',
188
+ code: `const [locale, setLocale] = useState<'en' | 'fr'>('en');
189
+
190
+ <InternationalizationProvider locale={locale} messages={{fr}}>
191
+ <Button
192
+ label={locale === 'en' ? 'Français' : 'English'}
193
+ onClick={() => setLocale(l => (l === 'en' ? 'fr' : 'en'))}
194
+ />
195
+ <App />
196
+ </InternationalizationProvider>;`,
197
+ },
198
+ {
199
+ type: 'prose',
200
+ text: "Persisting the user's choice (localStorage, cookie, URL segment, account setting) is up to the consumer. Astryx reads whatever `locale` you pass in.",
201
+ },
202
+ ],
203
+ },
204
+ {
205
+ title: 'Testing your translations',
206
+ category: 'guide',
207
+ content: [
208
+ {
209
+ type: 'prose',
210
+ text: 'Astryx generates a `pseudo` locale that wraps every string in `⟦…⟧` and replaces letters with accented look-alikes. Switching to it in development instantly reveals any astryx string that isn\'t going through the translator, plus any layout that breaks under longer text.',
211
+ },
212
+ {
213
+ type: 'code',
214
+ lang: 'tsx',
215
+ label: 'Turn on pseudo-localization',
216
+ code: `import pseudo from '@astryxdesign/core/locales/pseudo.json';
217
+
218
+ <InternationalizationProvider locale="pseudo" messages={{pseudo}}>
219
+ <App />
220
+ </InternationalizationProvider>;`,
221
+ },
222
+ {
223
+ type: 'prose',
224
+ text: 'Any bare English text you still see on screen is a hardcoded string that needs to be routed through `useTranslator`.',
225
+ },
226
+ {
227
+ type: 'prose',
228
+ text: "Pseudoloc also has a subtle caveat worth knowing: the pseudo catalog is complete (astryx generates it from every shipped key), so a component using an astryx-shipped key will always render its pseudo version. Your handwritten translation catalogs, on the other hand, only cover the keys you translated — anything missing falls back to English. That means \"looks perfect in pseudo\" is not the same guarantee as \"looks perfect in French.\" Check each real locale by hand for coverage gaps.",
229
+ },
230
+ ],
231
+ },
232
+ {
233
+ title: 'For contributors',
234
+ category: 'guide',
235
+ content: [
236
+ {
237
+ type: 'prose',
238
+ text: "Astryx's own strings live in `packages/core/locales/en.json`. New user-facing strings must go through `useTranslator` — this is enforced by the `@astryx/no-hardcoded-i18n-string` ESLint rule. See the AI contribution guide for the alias-and-resolve pattern used when adding new keys.",
239
+ },
240
+ ],
241
+ },
242
+ ],
243
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astryxdesign/cli",
3
- "version": "0.1.6-canary.eba4f34",
3
+ "version": "0.1.6-canary.f87204e",
4
4
  "displayName": "CLI",
5
5
  "description": "Scaffold projects, browse templates, generate themes, and get agent-ready docs from the command line.",
6
6
  "author": "Meta Open Source",
@@ -47,6 +47,10 @@
47
47
  "types": "./src/types/integration.d.ts",
48
48
  "import": "./src/integration.mjs"
49
49
  },
50
+ "./doc": {
51
+ "types": "./src/types/doc.d.ts",
52
+ "import": "./src/doc.mjs"
53
+ },
50
54
  "./template": {
51
55
  "types": "./src/types/template-api.d.ts",
52
56
  "import": "./src/template.mjs"
@@ -75,10 +79,10 @@
75
79
  "zod": "^4.4.3"
76
80
  },
77
81
  "peerDependencies": {
78
- "@astryxdesign/charts": "0.1.6-canary.eba4f34",
79
- "@astryxdesign/core": "0.1.6-canary.eba4f34",
80
- "@astryxdesign/lab": "0.1.6-canary.eba4f34",
81
- "@astryxdesign/theme-neutral": "0.1.6-canary.eba4f34",
82
+ "@astryxdesign/charts": "0.1.6-canary.f87204e",
83
+ "@astryxdesign/core": "0.1.6-canary.f87204e",
84
+ "@astryxdesign/lab": "0.1.6-canary.f87204e",
85
+ "@astryxdesign/theme-neutral": "0.1.6-canary.f87204e",
82
86
  "gpt-tokenizer": "^3.4.0"
83
87
  },
84
88
  "peerDependenciesMeta": {
@@ -96,10 +100,10 @@
96
100
  }
97
101
  },
98
102
  "devDependencies": {
99
- "@astryxdesign/charts": "0.1.6-canary.eba4f34",
100
- "@astryxdesign/core": "0.1.6-canary.eba4f34",
101
- "@astryxdesign/lab": "0.1.6-canary.eba4f34",
102
- "@astryxdesign/theme-neutral": "0.1.6-canary.eba4f34",
103
+ "@astryxdesign/charts": "0.1.6-canary.f87204e",
104
+ "@astryxdesign/core": "0.1.6-canary.f87204e",
105
+ "@astryxdesign/lab": "0.1.6-canary.f87204e",
106
+ "@astryxdesign/theme-neutral": "0.1.6-canary.f87204e",
103
107
  "gpt-tokenizer": "^3.4.0"
104
108
  },
105
109
  "scripts": {
@@ -0,0 +1,240 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Proves the minimal `exports` recipe an integration package needs so a
5
+ * bundler-resolution consumer can `import()` its block templates AND type-check
6
+ * them under `moduleResolution: bundler`.
7
+ *
8
+ * Integration packages in this ecosystem ship TypeScript SOURCE — their block
9
+ * templates are `.tsx` files with no compiled `.d.ts`. A later feature renders
10
+ * showcase previews by a REAL dynamic `import()` of a block's `.tsx` source
11
+ * (e.g. `import('@acme/widgets/templates/Gauge/GaugeShowcase.tsx')`) instead of
12
+ * eval'ing source text. For that import to resolve, the integration's
13
+ * `package.json#exports` map must GATE the deep path.
14
+ *
15
+ * These tests stand up a throwaway `@acme/widgets` fixture and drive the two
16
+ * gates that matter with the repo's own toolchain:
17
+ * 1. `tsc --noEmit` under `moduleResolution: bundler` (the consumer profile
18
+ * used by the Next.js example apps — NO `allowImportingTsExtensions`).
19
+ * 2. `esbuild --bundle` (a real bundler resolving + loading the deep import).
20
+ *
21
+ * CANONICAL RECIPE (see integration-authoring.md):
22
+ * exports: { "./templates/*.tsx": "./templates/*.tsx" }
23
+ * import: import('@acme/widgets/templates/<Name>/<Name>Showcase.tsx') // WITH .tsx
24
+ *
25
+ * The negative controls lock in WHY the extension is required: a bare
26
+ * `./templates/*` export with an extensionless import fails to type-check under
27
+ * bundler resolution (TS cannot infer the `.tsx` extension for a deep
28
+ * specifier), and an extensionless import fails to bundle.
29
+ */
30
+
31
+ import {afterEach, beforeEach, describe, expect, it} from 'vitest';
32
+ import * as fs from 'node:fs';
33
+ import * as os from 'node:os';
34
+ import * as path from 'node:path';
35
+ import {createRequire} from 'node:module';
36
+ import {execFileSync} from 'node:child_process';
37
+
38
+ const require = createRequire(import.meta.url);
39
+
40
+ /** Resolve the workspace tsc/esbuild binaries; null if unavailable. */
41
+ function resolveBin(spec) {
42
+ try {
43
+ return require.resolve(spec);
44
+ } catch {
45
+ return null;
46
+ }
47
+ }
48
+ const TSC_BIN = resolveBin('typescript/bin/tsc');
49
+ const ESBUILD_BIN = resolveBin('esbuild/bin/esbuild');
50
+
51
+ let tmpDir;
52
+
53
+ /**
54
+ * Build an @acme/widgets fixture: a block template (`.template.ts` doc +
55
+ * same-stem `.tsx` source) under `templates/`, plus an astryx.integration
56
+ * manifest, plus a caller-supplied `exports` map.
57
+ * @param {Record<string, string> | undefined} exportsMap
58
+ */
59
+ function makeWidgets(exportsMap) {
60
+ const pkgDir = path.join(tmpDir, 'node_modules', '@acme', 'widgets');
61
+ const blockDir = path.join(pkgDir, 'templates', 'Gauge');
62
+ fs.mkdirSync(blockDir, {recursive: true});
63
+
64
+ const pkg = {name: '@acme/widgets', version: '2.0.0', type: 'module'};
65
+ if (exportsMap) pkg.exports = exportsMap;
66
+ fs.writeFileSync(
67
+ path.join(pkgDir, 'package.json'),
68
+ JSON.stringify(pkg, null, 2),
69
+ );
70
+ fs.writeFileSync(
71
+ path.join(pkgDir, 'astryx.integration.mjs'),
72
+ `export default { templates: './templates' };\n`,
73
+ );
74
+ // The template-spec doc (canonical `.template.*` family).
75
+ fs.writeFileSync(
76
+ path.join(blockDir, 'GaugeShowcase.template.ts'),
77
+ `export default { name: 'Gauge showcase', description: 'A gauge.' };\n`,
78
+ );
79
+ // The same-stem `.tsx` SOURCE that a preview will dynamically import().
80
+ // Returns a plain value so the fixture type-checks without React types.
81
+ fs.writeFileSync(
82
+ path.join(blockDir, 'GaugeShowcase.tsx'),
83
+ `const GaugeShowcase = (): string => 'gauge-showcase';\n` +
84
+ `export default GaugeShowcase;\n`,
85
+ );
86
+ return pkgDir;
87
+ }
88
+
89
+ /** Write a consumer that dynamically imports the given specifier. */
90
+ function writeConsumer(specifier) {
91
+ fs.writeFileSync(
92
+ path.join(tmpDir, 'consumer.ts'),
93
+ `const load = () => import('${specifier}');\nexport default load;\n`,
94
+ );
95
+ }
96
+
97
+ /**
98
+ * The realistic consumer tsconfig: matches the repo's Next.js example apps
99
+ * (`moduleResolution: bundler`, `noEmit`, and deliberately NO
100
+ * `allowImportingTsExtensions`).
101
+ */
102
+ function writeTsconfig() {
103
+ fs.writeFileSync(
104
+ path.join(tmpDir, 'tsconfig.json'),
105
+ JSON.stringify(
106
+ {
107
+ compilerOptions: {
108
+ target: 'ES2022',
109
+ lib: ['ES2022', 'DOM', 'DOM.Iterable'],
110
+ module: 'ESNext',
111
+ moduleResolution: 'bundler',
112
+ jsx: 'react-jsx',
113
+ strict: true,
114
+ skipLibCheck: true,
115
+ esModuleInterop: true,
116
+ noEmit: true,
117
+ isolatedModules: true,
118
+ },
119
+ include: ['consumer.ts'],
120
+ },
121
+ null,
122
+ 2,
123
+ ),
124
+ );
125
+ }
126
+
127
+ /** @returns {{ok: boolean, out: string}} */
128
+ function runTsc() {
129
+ try {
130
+ execFileSync(process.execPath, [TSC_BIN, '--noEmit', '-p', 'tsconfig.json'], {
131
+ cwd: tmpDir,
132
+ stdio: 'pipe',
133
+ });
134
+ return {ok: true, out: ''};
135
+ } catch (e) {
136
+ return {ok: false, out: `${e.stdout ?? ''}${e.stderr ?? ''}`};
137
+ }
138
+ }
139
+
140
+ /** @returns {{ok: boolean, out: string}} */
141
+ function runEsbuild() {
142
+ try {
143
+ // esbuild's bin is a native executable (not a node script), so invoke it
144
+ // directly rather than through process.execPath.
145
+ execFileSync(
146
+ ESBUILD_BIN,
147
+ [
148
+ 'consumer.ts',
149
+ '--bundle',
150
+ '--format=esm',
151
+ '--loader:.tsx=tsx',
152
+ '--outfile=/dev/null',
153
+ '--log-level=error',
154
+ ],
155
+ {cwd: tmpDir, stdio: 'pipe'},
156
+ );
157
+ return {ok: true, out: ''};
158
+ } catch (e) {
159
+ return {ok: false, out: `${e.stdout ?? ''}${e.stderr ?? ''}`};
160
+ }
161
+ }
162
+
163
+ beforeEach(() => {
164
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-block-exports-'));
165
+ fs.writeFileSync(
166
+ path.join(tmpDir, 'package.json'),
167
+ JSON.stringify({name: 'consumer', private: true, type: 'module'}),
168
+ );
169
+ writeTsconfig();
170
+ });
171
+
172
+ afterEach(() => {
173
+ fs.rmSync(tmpDir, {recursive: true, force: true});
174
+ });
175
+
176
+ const CANONICAL = 'import(@acme/widgets/templates/Gauge/GaugeShowcase.tsx)';
177
+ const SPEC = '@acme/widgets/templates/Gauge/GaugeShowcase.tsx';
178
+
179
+ describe('integration block-template exports recipe', () => {
180
+ it.runIf(TSC_BIN != null)(
181
+ `CANONICAL: exports {"./templates/*.tsx"} + ${CANONICAL} type-checks under bundler resolution`,
182
+ () => {
183
+ makeWidgets({'./templates/*.tsx': './templates/*.tsx'});
184
+ writeConsumer(SPEC);
185
+ const {ok, out} = runTsc();
186
+ expect(out).toBe('');
187
+ expect(ok).toBe(true);
188
+ },
189
+ 30_000,
190
+ );
191
+
192
+ it.runIf(ESBUILD_BIN != null)(
193
+ `CANONICAL: the same import resolves + bundles with a real bundler`,
194
+ () => {
195
+ makeWidgets({'./templates/*.tsx': './templates/*.tsx'});
196
+ writeConsumer(SPEC);
197
+ const {ok, out} = runEsbuild();
198
+ expect(out).toBe('');
199
+ expect(ok).toBe(true);
200
+ },
201
+ 30_000,
202
+ );
203
+
204
+ it.runIf(TSC_BIN != null)(
205
+ 'NEGATIVE: an extensionless import does NOT type-check (TS cannot infer .tsx)',
206
+ () => {
207
+ makeWidgets({'./templates/*.tsx': './templates/*.tsx'});
208
+ writeConsumer('@acme/widgets/templates/Gauge/GaugeShowcase');
209
+ const {ok, out} = runTsc();
210
+ expect(ok).toBe(false);
211
+ expect(out).toContain('TS2307');
212
+ },
213
+ 30_000,
214
+ );
215
+
216
+ it.runIf(TSC_BIN != null)(
217
+ 'NEGATIVE: a bare "./templates/*" export (no .tsx entry) does NOT type-check the .tsx import',
218
+ () => {
219
+ // Only a bare mapping — the extensionful subpath is not exported.
220
+ makeWidgets({'./templates/*': './templates/*'});
221
+ writeConsumer(SPEC);
222
+ const {ok, out} = runTsc();
223
+ expect(ok).toBe(false);
224
+ // Bare export forces the TS5097 opt-in requirement (allowImportingTsExtensions).
225
+ expect(out).toContain('TS5097');
226
+ },
227
+ 30_000,
228
+ );
229
+
230
+ it.runIf(ESBUILD_BIN != null)(
231
+ 'NEGATIVE: an extensionless import does NOT bundle against the .tsx-only export',
232
+ () => {
233
+ makeWidgets({'./templates/*.tsx': './templates/*.tsx'});
234
+ writeConsumer('@acme/widgets/templates/Gauge/GaugeShowcase');
235
+ const {ok} = runEsbuild();
236
+ expect(ok).toBe(false);
237
+ },
238
+ 30_000,
239
+ );
240
+ });