An open source design system that's fully customizable and agent ready
TypeScript
fix(themes): theme packages declare type module so ./built reads as an ES module (#10876)
Cindy Zhanga815e7de8243
diff --git a/.changeset/theme-packages-type-module.md b/.changeset/theme-packages-type-module.mdnew file mode 100644index 0000000000..6953b67744--- /dev/null+++ b/.changeset/theme-packages-type-module.md@@ -0,0 +1,13 @@+---+'@astryxdesign/theme-butter': patch+'@astryxdesign/theme-chocolate': patch+'@astryxdesign/theme-gothic': patch+'@astryxdesign/theme-matcha': patch+'@astryxdesign/theme-neutral': patch+'@astryxdesign/theme-stone': patch+'@astryxdesign/theme-y2k': patch+---++[fix] Theme packages declare `"type": "module"`, so bundlers and Node read the `./built` entry as the ES module it is. Before, it was an ES module in a `.js` file in a package without a `type`, which tools that follow the package's `type` read as CommonJS. Every export subpath stays the same. The CommonJS build of the source entry is now `dist/source.cjs` behind the same `require` condition, with its own CommonJS declarations (`dist/source.d.cts`), so TypeScript types each condition in the format it loads. As before, `require('…/built')` loads an ES module, so it needs a Node version with `require(esm)` (22.12 or later). Deep imports that reach past `exports` into `dist/` change too: `dist/source.js` is now `dist/source.cjs`, and a `require` of `dist/<theme>.js` gets an ES module (`require(esm)`, Node 22.12 or later).++@cixzhangdiff --git a/packages/themes/butter/package.json b/packages/themes/butter/package.jsonindex 2be06df7ac..16d19ff2a4 100644--- a/packages/themes/butter/package.json+++ b/packages/themes/butter/package.json@@ -25,13 +25,19 @@"sideEffects": ["**/*.css"],- "main": "./dist/source.js",+ "type": "module",+ "main": "./dist/source.cjs","types": "./dist/source.d.ts","exports": {".": {- "types": "./dist/source.d.ts",- "import": "./dist/source.mjs",- "require": "./dist/source.js"+ "import": {+ "types": "./dist/source.d.ts",+ "default": "./dist/source.mjs"+ },+ "require": {+ "types": "./dist/source.d.cts",+ "default": "./dist/source.cjs"+ }},"./built": {"types": "./dist/butter.d.ts",@@ -69,7 +75,7 @@],"scripts": {"api:report": "node ../../../scripts/api-report/cli.mjs --package .",- "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-butter-palette-refs.mjs --check && astryx theme build src/butterTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/check-fully-specified.mjs"+ "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-butter-palette-refs.mjs --check && astryx theme build src/butterTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/add-declaration-extensions.mjs && node ../../../scripts/emit-commonjs-declarations.mjs dist/source.d.ts && node ../../../scripts/check-fully-specified.mjs"},"dependencies": {"lucide-react": "^1.18.0"diff --git a/packages/themes/butter/tsup.config.ts b/packages/themes/butter/tsup.config.tsindex 2766dd2ce3..eaeca04e83 100644--- a/packages/themes/butter/tsup.config.ts+++ b/packages/themes/butter/tsup.config.ts@@ -4,6 +4,12 @@ import {defineConfig} from 'tsup';const external = ['@astryxdesign/core', 'react', 'lucide-react'];+// The package is `"type": "module"`, so CommonJS needs `.cjs`; ES modules+// keep `.mjs`, the name the export map and the built theme import.+const outExtension = ({format}: {format: string}) => ({+ js: format === 'cjs' ? '.cjs' : '.mjs',+});+export default defineConfig([{entry: ['src/source.ts'],@@ -11,6 +17,7 @@ export default defineConfig([dts: false,clean: false, // Don't clean — astryx theme build already put theme files in dist/external,+ outExtension,},{entry: ['src/icons.tsx'],@@ -18,5 +25,6 @@ export default defineConfig([dts: false,clean: false,external,+ outExtension,},]);diff --git a/packages/themes/chocolate/package.json b/packages/themes/chocolate/package.jsonindex 6e1499a4cb..c8bd503e09 100644--- a/packages/themes/chocolate/package.json+++ b/packages/themes/chocolate/package.json@@ -25,13 +25,19 @@"sideEffects": ["**/*.css"],- "main": "./dist/source.js",+ "type": "module",+ "main": "./dist/source.cjs","types": "./dist/source.d.ts","exports": {".": {- "types": "./dist/source.d.ts",- "import": "./dist/source.mjs",- "require": "./dist/source.js"+ "import": {+ "types": "./dist/source.d.ts",+ "default": "./dist/source.mjs"+ },+ "require": {+ "types": "./dist/source.d.cts",+ "default": "./dist/source.cjs"+ }},"./built": {"types": "./dist/chocolate.d.ts",@@ -68,7 +74,7 @@],"scripts": {"api:report": "node ../../../scripts/api-report/cli.mjs --package .",- "build": "node ../../../scripts/clean-dist.mjs && astryx theme build src/chocolateTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/check-fully-specified.mjs"+ "build": "node ../../../scripts/clean-dist.mjs && astryx theme build src/chocolateTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/add-declaration-extensions.mjs && node ../../../scripts/emit-commonjs-declarations.mjs dist/source.d.ts && node ../../../scripts/check-fully-specified.mjs"},"dependencies": {"lucide-react": "^1.18.0"diff --git a/packages/themes/chocolate/tsup.config.ts b/packages/themes/chocolate/tsup.config.tsindex 2766dd2ce3..eaeca04e83 100644--- a/packages/themes/chocolate/tsup.config.ts+++ b/packages/themes/chocolate/tsup.config.ts@@ -4,6 +4,12 @@ import {defineConfig} from 'tsup';const external = ['@astryxdesign/core', 'react', 'lucide-react'];+// The package is `"type": "module"`, so CommonJS needs `.cjs`; ES modules+// keep `.mjs`, the name the export map and the built theme import.+const outExtension = ({format}: {format: string}) => ({+ js: format === 'cjs' ? '.cjs' : '.mjs',+});+export default defineConfig([{entry: ['src/source.ts'],@@ -11,6 +17,7 @@ export default defineConfig([dts: false,clean: false, // Don't clean — astryx theme build already put theme files in dist/external,+ outExtension,},{entry: ['src/icons.tsx'],@@ -18,5 +25,6 @@ export default defineConfig([dts: false,clean: false,external,+ outExtension,},]);diff --git a/packages/themes/gothic/package.json b/packages/themes/gothic/package.jsonindex 0217a0a715..cff9e13f10 100644--- a/packages/themes/gothic/package.json+++ b/packages/themes/gothic/package.json@@ -25,13 +25,19 @@"sideEffects": ["**/*.css"],- "main": "./dist/source.js",+ "type": "module",+ "main": "./dist/source.cjs","types": "./dist/source.d.ts","exports": {".": {- "types": "./dist/source.d.ts",- "import": "./dist/source.mjs",- "require": "./dist/source.js"+ "import": {+ "types": "./dist/source.d.ts",+ "default": "./dist/source.mjs"+ },+ "require": {+ "types": "./dist/source.d.cts",+ "default": "./dist/source.cjs"+ }},"./built": {"types": "./dist/gothic.d.ts",@@ -69,7 +75,7 @@],"scripts": {"api:report": "node ../../../scripts/api-report/cli.mjs --package .",- "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-gothic-palette-refs.mjs --check && astryx theme build src/gothicTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/check-fully-specified.mjs"+ "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-gothic-palette-refs.mjs --check && astryx theme build src/gothicTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/add-declaration-extensions.mjs && node ../../../scripts/emit-commonjs-declarations.mjs dist/source.d.ts && node ../../../scripts/check-fully-specified.mjs"},"dependencies": {"lucide-react": "^1.18.0"diff --git a/packages/themes/gothic/tsup.config.ts b/packages/themes/gothic/tsup.config.tsindex 2766dd2ce3..eaeca04e83 100644--- a/packages/themes/gothic/tsup.config.ts+++ b/packages/themes/gothic/tsup.config.ts@@ -4,6 +4,12 @@ import {defineConfig} from 'tsup';const external = ['@astryxdesign/core', 'react', 'lucide-react'];+// The package is `"type": "module"`, so CommonJS needs `.cjs`; ES modules+// keep `.mjs`, the name the export map and the built theme import.+const outExtension = ({format}: {format: string}) => ({+ js: format === 'cjs' ? '.cjs' : '.mjs',+});+export default defineConfig([{entry: ['src/source.ts'],@@ -11,6 +17,7 @@ export default defineConfig([dts: false,clean: false, // Don't clean — astryx theme build already put theme files in dist/external,+ outExtension,},{entry: ['src/icons.tsx'],@@ -18,5 +25,6 @@ export default defineConfig([dts: false,clean: false,external,+ outExtension,},]);diff --git a/packages/themes/matcha/package.json b/packages/themes/matcha/package.jsonindex 4fa87ecdc0..73802fa38d 100644--- a/packages/themes/matcha/package.json+++ b/packages/themes/matcha/package.json@@ -24,13 +24,19 @@"sideEffects": ["**/*.css"],- "main": "./dist/source.js",+ "type": "module",+ "main": "./dist/source.cjs","types": "./dist/source.d.ts","exports": {".": {- "types": "./dist/source.d.ts",- "import": "./dist/source.mjs",- "require": "./dist/source.js"+ "import": {+ "types": "./dist/source.d.ts",+ "default": "./dist/source.mjs"+ },+ "require": {+ "types": "./dist/source.d.cts",+ "default": "./dist/source.cjs"+ }},"./built": {"types": "./dist/matcha.d.ts",@@ -68,7 +74,7 @@],"scripts": {"api:report": "node ../../../scripts/api-report/cli.mjs --package .",- "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-matcha-palette-refs.mjs --check && astryx theme build src/matchaTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/check-fully-specified.mjs"+ "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-matcha-palette-refs.mjs --check && astryx theme build src/matchaTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/add-declaration-extensions.mjs && node ../../../scripts/emit-commonjs-declarations.mjs dist/source.d.ts && node ../../../scripts/check-fully-specified.mjs"},"dependencies": {"lucide-react": "^1.18.0"diff --git a/packages/themes/matcha/tsup.config.ts b/packages/themes/matcha/tsup.config.tsindex 2766dd2ce3..eaeca04e83 100644--- a/packages/themes/matcha/tsup.config.ts+++ b/packages/themes/matcha/tsup.config.ts@@ -4,6 +4,12 @@ import {defineConfig} from 'tsup';const external = ['@astryxdesign/core', 'react', 'lucide-react'];+// The package is `"type": "module"`, so CommonJS needs `.cjs`; ES modules+// keep `.mjs`, the name the export map and the built theme import.+const outExtension = ({format}: {format: string}) => ({+ js: format === 'cjs' ? '.cjs' : '.mjs',+});+export default defineConfig([{entry: ['src/source.ts'],@@ -11,6 +17,7 @@ export default defineConfig([dts: false,clean: false, // Don't clean — astryx theme build already put theme files in dist/external,+ outExtension,},{entry: ['src/icons.tsx'],@@ -18,5 +25,6 @@ export default defineConfig([dts: false,clean: false,external,+ outExtension,},]);diff --git a/packages/themes/neutral/package.json b/packages/themes/neutral/package.jsonindex 9c1be810e4..6baa04471e 100644--- a/packages/themes/neutral/package.json+++ b/packages/themes/neutral/package.json@@ -25,13 +25,19 @@"sideEffects": ["**/*.css"],- "main": "./dist/source.js",+ "type": "module",+ "main": "./dist/source.cjs","types": "./dist/source.d.ts","exports": {".": {- "types": "./dist/source.d.ts",- "import": "./dist/source.mjs",- "require": "./dist/source.js"+ "import": {+ "types": "./dist/source.d.ts",+ "default": "./dist/source.mjs"+ },+ "require": {+ "types": "./dist/source.d.cts",+ "default": "./dist/source.cjs"+ }},"./built": {"types": "./dist/neutral.d.ts",@@ -69,7 +75,7 @@],"scripts": {"api:report": "node ../../../scripts/api-report/cli.mjs --package .",- "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-neutral-palette-refs.mjs --check && astryx theme build src/neutralTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/check-fully-specified.mjs"+ "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-neutral-palette-refs.mjs --check && astryx theme build src/neutralTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/add-declaration-extensions.mjs && node ../../../scripts/emit-commonjs-declarations.mjs dist/source.d.ts && node ../../../scripts/check-fully-specified.mjs"},"dependencies": {"lucide-react": "^1.18.0"diff --git a/packages/themes/neutral/tsup.config.ts b/packages/themes/neutral/tsup.config.tsindex 2766dd2ce3..eaeca04e83 100644--- a/packages/themes/neutral/tsup.config.ts+++ b/packages/themes/neutral/tsup.config.ts@@ -4,6 +4,12 @@ import {defineConfig} from 'tsup';const external = ['@astryxdesign/core', 'react', 'lucide-react'];+// The package is `"type": "module"`, so CommonJS needs `.cjs`; ES modules+// keep `.mjs`, the name the export map and the built theme import.+const outExtension = ({format}: {format: string}) => ({+ js: format === 'cjs' ? '.cjs' : '.mjs',+});+export default defineConfig([{entry: ['src/source.ts'],@@ -11,6 +17,7 @@ export default defineConfig([dts: false,clean: false, // Don't clean — astryx theme build already put theme files in dist/external,+ outExtension,},{entry: ['src/icons.tsx'],@@ -18,5 +25,6 @@ export default defineConfig([dts: false,clean: false,external,+ outExtension,},]);diff --git a/packages/themes/stone/package.json b/packages/themes/stone/package.jsonindex dc7a3c8b54..a4601d3395 100644--- a/packages/themes/stone/package.json+++ b/packages/themes/stone/package.json@@ -25,13 +25,19 @@"sideEffects": ["**/*.css"],- "main": "./dist/source.js",+ "type": "module",+ "main": "./dist/source.cjs","types": "./dist/source.d.ts","exports": {".": {- "types": "./dist/source.d.ts",- "import": "./dist/source.mjs",- "require": "./dist/source.js"+ "import": {+ "types": "./dist/source.d.ts",+ "default": "./dist/source.mjs"+ },+ "require": {+ "types": "./dist/source.d.cts",+ "default": "./dist/source.cjs"+ }},"./built": {"types": "./dist/stone.d.ts",@@ -69,7 +75,7 @@],"scripts": {"api:report": "node ../../../scripts/api-report/cli.mjs --package .",- "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-stone-palette-refs.mjs --check && astryx theme build src/stoneTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/check-fully-specified.mjs"+ "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-stone-palette-refs.mjs --check && astryx theme build src/stoneTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/add-declaration-extensions.mjs && node ../../../scripts/emit-commonjs-declarations.mjs dist/source.d.ts && node ../../../scripts/check-fully-specified.mjs"},"dependencies": {"lucide-react": "^1.18.0"diff --git a/packages/themes/stone/tsup.config.ts b/packages/themes/stone/tsup.config.tsindex 2766dd2ce3..eaeca04e83 100644--- a/packages/themes/stone/tsup.config.ts+++ b/packages/themes/stone/tsup.config.ts@@ -4,6 +4,12 @@ import {defineConfig} from 'tsup';const external = ['@astryxdesign/core', 'react', 'lucide-react'];+// The package is `"type": "module"`, so CommonJS needs `.cjs`; ES modules+// keep `.mjs`, the name the export map and the built theme import.+const outExtension = ({format}: {format: string}) => ({+ js: format === 'cjs' ? '.cjs' : '.mjs',+});+export default defineConfig([{entry: ['src/source.ts'],@@ -11,6 +17,7 @@ export default defineConfig([dts: false,clean: false, // Don't clean — astryx theme build already put theme files in dist/external,+ outExtension,},{entry: ['src/icons.tsx'],@@ -18,5 +25,6 @@ export default defineConfig([dts: false,clean: false,external,+ outExtension,},]);diff --git a/packages/themes/y2k/package.json b/packages/themes/y2k/package.jsonindex f90f77a655..5cd0726abd 100644--- a/packages/themes/y2k/package.json+++ b/packages/themes/y2k/package.json@@ -25,13 +25,19 @@"sideEffects": ["**/*.css"],- "main": "./dist/source.js",+ "type": "module",+ "main": "./dist/source.cjs","types": "./dist/source.d.ts","exports": {".": {- "types": "./dist/source.d.ts",- "import": "./dist/source.mjs",- "require": "./dist/source.js"+ "import": {+ "types": "./dist/source.d.ts",+ "default": "./dist/source.mjs"+ },+ "require": {+ "types": "./dist/source.d.cts",+ "default": "./dist/source.cjs"+ }},"./built": {"types": "./dist/y2k.d.ts",@@ -69,7 +75,7 @@],"scripts": {"api:report": "node ../../../scripts/api-report/cli.mjs --package .",- "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-y2k-palette-refs.mjs --check && astryx theme build src/y2kTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/check-fully-specified.mjs"+ "build": "node ../../../scripts/clean-dist.mjs && node ../../../scripts/generate-y2k-palette-refs.mjs --check && astryx theme build src/y2kTheme.ts -o dist/theme.css --icons-specifier ./icons.mjs && tsup && tsc --project tsconfig.build.json && node ../../../scripts/add-declaration-extensions.mjs && node ../../../scripts/emit-commonjs-declarations.mjs dist/source.d.ts && node ../../../scripts/check-fully-specified.mjs"},"dependencies": {"lucide-react": "^1.18.0"diff --git a/packages/themes/y2k/tsup.config.ts b/packages/themes/y2k/tsup.config.tsindex 2766dd2ce3..eaeca04e83 100644--- a/packages/themes/y2k/tsup.config.ts+++ b/packages/themes/y2k/tsup.config.ts@@ -4,6 +4,12 @@ import {defineConfig} from 'tsup';const external = ['@astryxdesign/core', 'react', 'lucide-react'];+// The package is `"type": "module"`, so CommonJS needs `.cjs`; ES modules+// keep `.mjs`, the name the export map and the built theme import.+const outExtension = ({format}: {format: string}) => ({+ js: format === 'cjs' ? '.cjs' : '.mjs',+});+export default defineConfig([{entry: ['src/source.ts'],@@ -11,6 +17,7 @@ export default defineConfig([dts: false,clean: false, // Don't clean — astryx theme build already put theme files in dist/external,+ outExtension,},{entry: ['src/icons.tsx'],@@ -18,5 +25,6 @@ export default defineConfig([dts: false,clean: false,external,+ outExtension,},]);diff --git a/scripts/emit-commonjs-declarations.mjs b/scripts/emit-commonjs-declarations.mjsnew file mode 100644index 0000000000..a74eab2898--- /dev/null+++ b/scripts/emit-commonjs-declarations.mjs@@ -0,0 +1,75 @@+#!/usr/bin/env node+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * Build step: write CommonJS declarations beside the ES module declarations of+ * a `"type": "module"` package's dual entry.+ *+ * TypeScript reads a `.d.ts` file in a `"type": "module"` package as ES module+ * declarations, so a `require` condition that loads a `.cjs` file needs `.d.cts`+ * declarations. Starting at each named entry, this copies every declaration+ * file the entry reaches through relative specifiers to a `.d.cts` sibling and+ * points each relative `.js` specifier at its `.cjs` sibling, so the CommonJS+ * declarations reach only CommonJS declarations. Run it after+ * add-declaration-extensions.mjs, which makes the specifiers fully specified.+ *+ * Usage: node <repo-root>/scripts/emit-commonjs-declarations.mjs <entry.d.ts>...+ * (run from a package root; entries are paths relative to it)+ */++import fs from 'node:fs';+import path from 'node:path';+import {fileURLToPath} from 'node:url';++// from './x.js' | import './x.js' | import('./x.js')+const RELATIVE_JS =+ /(\bfrom\s*|\bimport\s+|\bimport\(\s*)(["'])(\.\.?\/[^"']*)\.js\2/g;++const SOURCE_MAP = /^\/\/# sourceMappingURL=.*$/m;++/** The CommonJS declarations for one ES module declaration file. */+export function toCommonJsDeclarations(source) {+ return source+ .replace(RELATIVE_JS, '$1$2$3.cjs$2')+ .replace(SOURCE_MAP, '')+ .trimEnd()+ .concat('\n');+}++/**+ * Writes the `.d.cts` copy of each entry and of every declaration file it+ * reaches. Returns the files written, relative to `root`.+ */+export function emitCommonJsDeclarations(root, entries) {+ const written = [];+ const pending = entries.map(entry => path.resolve(root, entry));+ const seen = new Set();+ while (pending.length > 0) {+ const file = pending.pop();+ if (seen.has(file) || !fs.existsSync(file)) continue;+ seen.add(file);+ const source = fs.readFileSync(file, 'utf8');+ for (const match of source.matchAll(RELATIVE_JS)) {+ pending.push(path.resolve(path.dirname(file), `${match[3]}.d.ts`));+ }+ const target = file.replace(/\.d\.ts$/, '.d.cts');+ fs.writeFileSync(target, toCommonJsDeclarations(source));+ written.push(path.relative(root, target));+ }+ return written.sort();+}++if (process.argv[1] === fileURLToPath(import.meta.url)) {+ const entries = process.argv.slice(2);+ if (entries.length === 0) {+ console.error('Usage: emit-commonjs-declarations.mjs <entry.d.ts>...');+ process.exit(2);+ }+ const missing = entries.filter(entry => !fs.existsSync(path.resolve(entry)));+ if (missing.length > 0) {+ console.error(`Declaration entry not found: ${missing.join(', ')}`);+ process.exit(1);+ }+ const written = emitCommonJsDeclarations(process.cwd(), entries);+ console.log(`Wrote ${written.length} CommonJS declaration file(s).`);+}diff --git a/scripts/emit-commonjs-declarations.test.mjs b/scripts/emit-commonjs-declarations.test.mjsnew file mode 100644index 0000000000..e349900ff1--- /dev/null+++ b/scripts/emit-commonjs-declarations.test.mjs@@ -0,0 +1,50 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {afterEach, describe, expect, it} from 'vitest';+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import {+ emitCommonJsDeclarations,+ toCommonJsDeclarations,+} from './emit-commonjs-declarations.mjs';++let dir;+afterEach(() => {+ if (dir) fs.rmSync(dir, {recursive: true, force: true});+});++function write(file, source) {+ fs.mkdirSync(path.dirname(path.join(dir, file)), {recursive: true});+ fs.writeFileSync(path.join(dir, file), source);+}++describe('emit-commonjs-declarations', () => {+ it('points relative .js specifiers at .cjs and drops the source map', () => {+ expect(+ toCommonJsDeclarations(+ "export {a} from './a.js';\nimport type {B} from '../b.js';\nexport type {C} from '@scope/pkg';\n//# sourceMappingURL=x.d.ts.map",+ ),+ ).toBe(+ "export {a} from './a.cjs';\nimport type {B} from '../b.cjs';\nexport type {C} from '@scope/pkg';\n",+ );+ });++ it('writes a .d.cts for the entry and every declaration it reaches', () => {+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cjs-decl-'));+ write('dist/source.d.ts', "export {a} from './a.js';\n");+ write('dist/a.d.ts', "export {b} from './nested/b.js';\n");+ write('dist/nested/b.d.ts', 'export declare const b: 1;\n');+ write('dist/unrelated.d.ts', 'export {};\n');++ expect(emitCommonJsDeclarations(dir, ['dist/source.d.ts'])).toEqual([+ 'dist/a.d.cts',+ 'dist/nested/b.d.cts',+ 'dist/source.d.cts',+ ]);+ expect(fs.readFileSync(path.join(dir, 'dist/source.d.cts'), 'utf8')).toBe(+ "export {a} from './a.cjs';\n",+ );+ expect(fs.existsSync(path.join(dir, 'dist/unrelated.d.cts'))).toBe(false);+ });+});diff --git a/scripts/theme-package-module-format.test.mjs b/scripts/theme-package-module-format.test.mjsnew file mode 100644index 0000000000..b140be4e7e--- /dev/null+++ b/scripts/theme-package-module-format.test.mjs@@ -0,0 +1,97 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Theme packages declare the module format of every entry they export.+ * @input Each published theme package's `package.json`.+ * @output A failure when an exported entry's extension and the package's+ * `type` would make a bundler or Node read it in the wrong format.+ * @position Packaging check for the theme packages. `astryx theme build`+ * writes `./built` (`dist/<theme>.js`) as an ES module, and tsup writes the+ * `source` entry in both formats. A `.js` file is an ES module only where+ * its package says `"type": "module"`; otherwise bundlers and older Node+ * read it as CommonJS.+ */++import {describe, expect, it} from 'vitest';+import * as fs from 'node:fs';+import * as path from 'node:path';++const THEMES_DIR = path.resolve(import.meta.dirname, '../packages/themes');++const THEMES = fs+ .readdirSync(THEMES_DIR)+ .map(name => ({name, dir: path.join(THEMES_DIR, name)}))+ .filter(({dir}) => fs.existsSync(path.join(dir, 'package.json')))+ .map(({name, dir}) => ({+ name,+ manifest: JSON.parse(+ fs.readFileSync(path.join(dir, 'package.json'), 'utf8'),+ ),+ }))+ .filter(({manifest}) => manifest.private !== true);++/** The format Node, bundlers and TypeScript read a file in, from its name. */+function formatOf(file, packageType) {+ if (/\.(?:mjs|d\.mts)$/.test(file)) return 'esm';+ if (/\.(?:cjs|d\.cts)$/.test(file)) return 'cjs';+ return packageType === 'module' ? 'esm' : 'cjs';+}++/**+ * The runtime file and the declaration file one condition resolves to. A+ * condition holds a path, or `{types, default}`; a `types` beside the+ * conditions applies to each of them.+ */+function resolveCondition(entry, condition) {+ const target = entry[condition];+ if (typeof target === 'string') {+ return {runtime: target, types: entry.types};+ }+ return {runtime: target.default, types: target.types ?? entry.types};+}++describe('theme package module formats', () => {+ it('finds the published theme packages', () => {+ expect(THEMES.length).toBeGreaterThan(0);+ });++ for (const {name, manifest} of THEMES) {+ describe(name, () => {+ it('reads ./built, an ES module, as an ES module for import and require', () => {+ for (const condition of ['import', 'require']) {+ const {runtime} = resolveCondition(+ manifest.exports['./built'],+ condition,+ );+ expect(formatOf(runtime, manifest.type), condition).toBe('esm');+ }+ });++ it('reads the source entry in the format tsup writes for each condition', () => {+ const source = manifest.exports['.'];+ const imported = resolveCondition(source, 'import');+ const required = resolveCondition(source, 'require');+ expect(formatOf(imported.runtime, manifest.type)).toBe('esm');+ expect(formatOf(required.runtime, manifest.type)).toBe('cjs');+ expect(formatOf(manifest.main, manifest.type)).toBe('cjs');+ expect(manifest.module).toBe(imported.runtime);+ });++ it('gives each condition declarations in the format of its runtime file', () => {+ // TypeScript reads a declaration file in the format its name and the+ // package's type give it, and checks imports against that format.+ for (const [subpath, entry] of Object.entries(manifest.exports)) {+ for (const condition of ['import', 'require']) {+ if (typeof entry !== 'object' || !(condition in entry)) continue;+ const {runtime, types} = resolveCondition(entry, condition);+ if (types === undefined || !/\.[cm]?js$/.test(runtime)) continue;+ expect(+ formatOf(types, manifest.type),+ `${subpath} ${condition}: ${types} for ${runtime}`,+ ).toBe(formatOf(runtime, manifest.type));+ }+ }+ });+ });+ }+});