An open source design system that's fully customizable and agent ready
TypeScript
[CLI] fix: rewrite every .stylex import to a deep path in swizzle (#7144)
Joey397a12573221
diff --git a/.changeset/fix-swizzle-stylex-deep-path.md b/.changeset/fix-swizzle-stylex-deep-path.mdnew file mode 100644index 0000000000..3920d6a586--- /dev/null+++ b/.changeset/fix-swizzle-stylex-deep-path.md@@ -0,0 +1,15 @@+---+'@astryxdesign/cli': patch+'@astryxdesign/core': patch+---++[fix] swizzle: rewrite every .stylex import to a deep path+@josephfarina++`astryx swizzle Button` (and any component that imports a `.stylex` module from outside its own directory) emitted an import that collapsed to the directory barrel — e.g. `@astryxdesign/core/utils` instead of `@astryxdesign/core/utils/interactionOverlay.stylex`. The barrel does not re-export those StyleX symbols, so the swizzled file could not compile.++`rewriteImports` now gives every `*.stylex` module the deep subpath. The exports generator (`scripts/sync-exports.js`) adds 16 specific subpath exports for the `.stylex` modules swizzled components actually reference across directories:++`DateInput/tokens.stylex`, `Icon/IconSize.stylex`, `Indicator/indicator.markers.stylex`, `Layer/layerAnimations.stylex`, `Layer/layerTextReset.stylex`, `Layer/layerViewportInset.stylex`, `Layout/container.stylex`, `Layout/edgeCompensation.stylex`, `Layout/padding.stylex`, `NavItem/navItemStyles.stylex`, `Selector/selectorPresentation.stylex`, `Stack/stack.stylex`, `Stack/stackItem.stylex`, `Text/text.stylex`, `utils/focusOutline.stylex`, `utils/interactionOverlay.stylex`++These are public API additions. Each module already ships in the npm tarball (in `dist/`); only the exports map entry is new. No wildcard — a future cross-directory `.stylex` import needs a deliberate entry in `STATIC_EXPORTS`. Precedent: `./theme/dataTokens.stylex` (AST-066 FR2).diff --git a/packages/cli/api/swizzle/copy/copy.mjs b/packages/cli/api/swizzle/copy/copy.mjsindex fd958f89c5..785c90fac1 100644--- a/packages/cli/api/swizzle/copy/copy.mjs+++ b/packages/cli/api/swizzle/copy/copy.mjs@@ -46,12 +46,11 @@ const DEFAULT_ISSUES_URL = 'https://github.com/facebook/astryx/issues/new';* - Asset files (`.json`, `.css`): exported by full subpath (e.g.* `./locales/*.json`, `./reset.css`), and collapsing a two-levels-up* `../../locales/x.json` would emit the invalid `<pkg>/..`.- * - The theme StyleX token module (`../theme/tokens.stylex`): the StyleX- * compiler needs the real module, which core ships as the dedicated- * `./theme/tokens.stylex` export (the in-repo charts consumer imports it- * by that exact subpath). Other `.stylex` files (e.g. a component-local- * `../Layer/layerAnimations.stylex`) are NOT exported by subpath, so they- * keep the barrel collapse.+ * - StyleX modules (`*.stylex`): the StyleX compiler needs the real module+ * path so it can resolve the styles at compile time. The barrel re-export+ * loses the module identity. Core exports every `.stylex` file by its+ * deep subpath (e.g. `./utils/focusOutline.stylex`,+ * `./theme/tokens.stylex`).** @param {string} content* @param {string} [ownerPackage]@@ -67,8 +66,8 @@ export function rewriteImports(content, ownerPackage = CORE_PACKAGE) {if (/\.(?:json|css)$/.test(last)) {return `${ownerPackage}/${rest}`;}- // The theme token module is a dedicated deep StyleX export.- if (parts[0] === 'theme' && /\.stylex(?:\.[cm]?[jt]sx?)?$/.test(last)) {+ // StyleX modules need the deep path so the compiler can resolve them.+ if (/\.stylex(?:\.[cm]?[jt]sx?)?$/.test(last)) {return `${ownerPackage}/${rest}`;}return `${ownerPackage}/${parts[0]}`;diff --git a/packages/cli/api/swizzle/swizzle.test.mjs b/packages/cli/api/swizzle/swizzle.test.mjsindex 9a741885df..045e46b2e1 100644--- a/packages/cli/api/swizzle/swizzle.test.mjs+++ b/packages/cli/api/swizzle/swizzle.test.mjs@@ -1,6 +1,7 @@// Copyright (c) Meta Platforms, Inc. and affiliates.import {describe, it, expect} from 'vitest';+import * as fs from 'node:fs';import * as path from 'node:path';import {fileURLToPath} from 'node:url';import {rewriteImports, swizzle} from './swizzle.mjs';@@ -76,12 +77,26 @@ describe('rewriteImports', () => {);});- it('keeps the barrel collapse for a component-local .stylex (not a subpath export)', () => {- // Only theme/tokens.stylex is a dedicated deep export; component-local- // .stylex files are re-exported through the directory barrel.- const input = `import { s } from '../Layer/layerAnimations.stylex';`;+ it('rewrites ANY non-theme .stylex import to a deep path (not the barrel)', () => {+ // Every .stylex module needs the deep path so the StyleX compiler can+ // resolve styles at compile time. The barrel re-export loses identity.+ const input = `import { interactionOverlayStyles } from '../utils/interactionOverlay.stylex';`;expect(rewriteImports(input)).toBe(- `import { s } from '@astryxdesign/core/Layer';`,+ `import { interactionOverlayStyles } from '@astryxdesign/core/utils/interactionOverlay.stylex';`,+ );+ });++ it('rewrites a Layout .stylex import to a deep path', () => {+ const input = `import { container } from '../Layout/container.stylex';`;+ expect(rewriteImports(input)).toBe(+ `import { container } from '@astryxdesign/core/Layout/container.stylex';`,+ );+ });++ it('rewrites a two-levels-up .stylex import to a deep path', () => {+ const input = `import { focusOutlineProps } from '../../utils/focusOutline.stylex';`;+ expect(rewriteImports(input)).toBe(+ `import { focusOutlineProps } from '@astryxdesign/core/utils/focusOutline.stylex';`,);});});@@ -105,3 +120,38 @@ describe('swizzle() API', () => {});});});++describe('swizzle rewriteImports compiles for components with .stylex imports', () => {+ it('Button: every rewritten import resolves in the core exports map', async () => {+ const corePkg = JSON.parse(+ fs.readFileSync(+ path.join(REPO, 'packages/core/package.json'),+ 'utf-8',+ ),+ );+ const exportKeys = new Set(Object.keys(corePkg.exports));++ const buttonDir = path.join(REPO, 'packages/core/src/Button');+ const files = fs.readdirSync(buttonDir).filter(+ f => (f.endsWith('.ts') || f.endsWith('.tsx')) && !f.includes('.test.') && !f.includes('.doc.'),+ );++ for (const file of files) {+ const content = fs.readFileSync(path.join(buttonDir, file), 'utf-8');+ const rewritten = rewriteImports(content);++ // Extract all rewritten @astryxdesign/core/* imports+ const importRe = /from ['"](@astryxdesign\/core\/[^'"]+)['"]/g;+ let m;+ while ((m = importRe.exec(rewritten)) !== null) {+ const specifier = m[1];+ const subpath = './' + specifier.replace('@astryxdesign/core/', '');++ expect(+ exportKeys.has(subpath),+ `${specifier} (subpath ${subpath}) must resolve in core exports`,+ ).toBe(true);+ }+ }+ });+});diff --git a/packages/core/package.json b/packages/core/package.jsonindex b8b0bbe83b..8aa775e4f3 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -81,6 +81,86 @@"types": "./dist/theme/syntax/index.d.ts","default": "./dist/theme/syntax/index.js"},+ "./DateInput/tokens.stylex": {+ "source": "./src/DateInput/tokens.stylex.ts",+ "types": "./dist/DateInput/tokens.stylex.d.ts",+ "default": "./dist/DateInput/tokens.stylex.js"+ },+ "./Icon/IconSize.stylex": {+ "source": "./src/Icon/IconSize.stylex.ts",+ "types": "./dist/Icon/IconSize.stylex.d.ts",+ "default": "./dist/Icon/IconSize.stylex.js"+ },+ "./Indicator/indicator.markers.stylex": {+ "source": "./src/Indicator/indicator.markers.stylex.ts",+ "types": "./dist/Indicator/indicator.markers.stylex.d.ts",+ "default": "./dist/Indicator/indicator.markers.stylex.js"+ },+ "./Layer/layerAnimations.stylex": {+ "source": "./src/Layer/layerAnimations.stylex.ts",+ "types": "./dist/Layer/layerAnimations.stylex.d.ts",+ "default": "./dist/Layer/layerAnimations.stylex.js"+ },+ "./Layer/layerTextReset.stylex": {+ "source": "./src/Layer/layerTextReset.stylex.ts",+ "types": "./dist/Layer/layerTextReset.stylex.d.ts",+ "default": "./dist/Layer/layerTextReset.stylex.js"+ },+ "./Layer/layerViewportInset.stylex": {+ "source": "./src/Layer/layerViewportInset.stylex.ts",+ "types": "./dist/Layer/layerViewportInset.stylex.d.ts",+ "default": "./dist/Layer/layerViewportInset.stylex.js"+ },+ "./Layout/container.stylex": {+ "source": "./src/Layout/container.stylex.ts",+ "types": "./dist/Layout/container.stylex.d.ts",+ "default": "./dist/Layout/container.stylex.js"+ },+ "./Layout/edgeCompensation.stylex": {+ "source": "./src/Layout/edgeCompensation.stylex.ts",+ "types": "./dist/Layout/edgeCompensation.stylex.d.ts",+ "default": "./dist/Layout/edgeCompensation.stylex.js"+ },+ "./Layout/padding.stylex": {+ "source": "./src/Layout/padding.stylex.ts",+ "types": "./dist/Layout/padding.stylex.d.ts",+ "default": "./dist/Layout/padding.stylex.js"+ },+ "./NavItem/navItemStyles.stylex": {+ "source": "./src/NavItem/navItemStyles.stylex.ts",+ "types": "./dist/NavItem/navItemStyles.stylex.d.ts",+ "default": "./dist/NavItem/navItemStyles.stylex.js"+ },+ "./Selector/selectorPresentation.stylex": {+ "source": "./src/Selector/selectorPresentation.stylex.ts",+ "types": "./dist/Selector/selectorPresentation.stylex.d.ts",+ "default": "./dist/Selector/selectorPresentation.stylex.js"+ },+ "./Stack/stack.stylex": {+ "source": "./src/Stack/stack.stylex.ts",+ "types": "./dist/Stack/stack.stylex.d.ts",+ "default": "./dist/Stack/stack.stylex.js"+ },+ "./Stack/stackItem.stylex": {+ "source": "./src/Stack/stackItem.stylex.ts",+ "types": "./dist/Stack/stackItem.stylex.d.ts",+ "default": "./dist/Stack/stackItem.stylex.js"+ },+ "./Text/text.stylex": {+ "source": "./src/Text/text.stylex.ts",+ "types": "./dist/Text/text.stylex.d.ts",+ "default": "./dist/Text/text.stylex.js"+ },+ "./utils/focusOutline.stylex": {+ "source": "./src/utils/focusOutline.stylex.ts",+ "types": "./dist/utils/focusOutline.stylex.d.ts",+ "default": "./dist/utils/focusOutline.stylex.js"+ },+ "./utils/interactionOverlay.stylex": {+ "source": "./src/utils/interactionOverlay.stylex.ts",+ "types": "./dist/utils/interactionOverlay.stylex.d.ts",+ "default": "./dist/utils/interactionOverlay.stylex.js"+ },"./docs.mjs": "./docs.mjs","./groups.doc.mjs": "./groups.doc.mjs","./locales/*.json": "./locales/*.json",diff --git a/scripts/sync-exports.js b/scripts/sync-exports.jsindex d6dc099b72..ddc98d5f03 100644--- a/scripts/sync-exports.js+++ b/scripts/sync-exports.js@@ -92,6 +92,91 @@ const STATIC_EXPORTS = {types: './dist/theme/syntax/index.d.ts',default: './dist/theme/syntax/index.js',},+ // Cross-directory .stylex modules that swizzled components import. Each is+ // an explicit public subpath so the StyleX compiler can resolve the deep+ // import rewriteImports emits (see api/swizzle/copy/copy.mjs). No wildcard —+ // a future cross-directory .stylex import needs a deliberate entry here.+ // Precedent: ./theme/tokens.stylex and ./theme/dataTokens.stylex above.+ './DateInput/tokens.stylex': {+ source: './src/DateInput/tokens.stylex.ts',+ types: './dist/DateInput/tokens.stylex.d.ts',+ default: './dist/DateInput/tokens.stylex.js',+ },+ './Icon/IconSize.stylex': {+ source: './src/Icon/IconSize.stylex.ts',+ types: './dist/Icon/IconSize.stylex.d.ts',+ default: './dist/Icon/IconSize.stylex.js',+ },+ './Indicator/indicator.markers.stylex': {+ source: './src/Indicator/indicator.markers.stylex.ts',+ types: './dist/Indicator/indicator.markers.stylex.d.ts',+ default: './dist/Indicator/indicator.markers.stylex.js',+ },+ './Layer/layerAnimations.stylex': {+ source: './src/Layer/layerAnimations.stylex.ts',+ types: './dist/Layer/layerAnimations.stylex.d.ts',+ default: './dist/Layer/layerAnimations.stylex.js',+ },+ './Layer/layerTextReset.stylex': {+ source: './src/Layer/layerTextReset.stylex.ts',+ types: './dist/Layer/layerTextReset.stylex.d.ts',+ default: './dist/Layer/layerTextReset.stylex.js',+ },+ './Layer/layerViewportInset.stylex': {+ source: './src/Layer/layerViewportInset.stylex.ts',+ types: './dist/Layer/layerViewportInset.stylex.d.ts',+ default: './dist/Layer/layerViewportInset.stylex.js',+ },+ './Layout/container.stylex': {+ source: './src/Layout/container.stylex.ts',+ types: './dist/Layout/container.stylex.d.ts',+ default: './dist/Layout/container.stylex.js',+ },+ './Layout/edgeCompensation.stylex': {+ source: './src/Layout/edgeCompensation.stylex.ts',+ types: './dist/Layout/edgeCompensation.stylex.d.ts',+ default: './dist/Layout/edgeCompensation.stylex.js',+ },+ './Layout/padding.stylex': {+ source: './src/Layout/padding.stylex.ts',+ types: './dist/Layout/padding.stylex.d.ts',+ default: './dist/Layout/padding.stylex.js',+ },+ './NavItem/navItemStyles.stylex': {+ source: './src/NavItem/navItemStyles.stylex.ts',+ types: './dist/NavItem/navItemStyles.stylex.d.ts',+ default: './dist/NavItem/navItemStyles.stylex.js',+ },+ './Selector/selectorPresentation.stylex': {+ source: './src/Selector/selectorPresentation.stylex.ts',+ types: './dist/Selector/selectorPresentation.stylex.d.ts',+ default: './dist/Selector/selectorPresentation.stylex.js',+ },+ './Stack/stack.stylex': {+ source: './src/Stack/stack.stylex.ts',+ types: './dist/Stack/stack.stylex.d.ts',+ default: './dist/Stack/stack.stylex.js',+ },+ './Stack/stackItem.stylex': {+ source: './src/Stack/stackItem.stylex.ts',+ types: './dist/Stack/stackItem.stylex.d.ts',+ default: './dist/Stack/stackItem.stylex.js',+ },+ './Text/text.stylex': {+ source: './src/Text/text.stylex.ts',+ types: './dist/Text/text.stylex.d.ts',+ default: './dist/Text/text.stylex.js',+ },+ './utils/focusOutline.stylex': {+ source: './src/utils/focusOutline.stylex.ts',+ types: './dist/utils/focusOutline.stylex.d.ts',+ default: './dist/utils/focusOutline.stylex.js',+ },+ './utils/interactionOverlay.stylex': {+ source: './src/utils/interactionOverlay.stylex.ts',+ types: './dist/utils/interactionOverlay.stylex.d.ts',+ default: './dist/utils/interactionOverlay.stylex.js',+ },'./docs.mjs': './docs.mjs','./groups.doc.mjs': './groups.doc.mjs',// Rich authoring catalogs keep their existing JSON paths. Generated string