An open source design system that's fully customizable and agent ready
TypeScript
fix(Selector): preserve StyleX evaluation in source builds (#5508)
Jiun Shin47b288edb2ce
diff --git a/.changeset/selector-source-build-compat.md b/.changeset/selector-source-build-compat.mdnew file mode 100644index 0000000000..acbeaa3bc8--- /dev/null+++ b/.changeset/selector-source-build-compat.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Selector raw source now compiles when consumer Babel presets lower arrow functions before StyleX extraction++@jiunshinndiff --git a/packages/core/src/Selector/Selector.source-build.test.mjs b/packages/core/src/Selector/Selector.source-build.test.mjsnew file mode 100644index 0000000000..9ebc01c61a--- /dev/null+++ b/packages/core/src/Selector/Selector.source-build.test.mjs@@ -0,0 +1,65 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Selector.source-build.test.mjs+ * @input Uses Babel, next/babel, the StyleX compiler, and Selector source+ * @output Verifies raw Selector source survives a consumer's Babel pipeline+ * @position Regression test for Selector source-build compatibility+ */++import fs from 'node:fs/promises';+import path from 'node:path';+import {fileURLToPath} from 'node:url';+import {transformAsync} from '@babel/core';+import stylexBabelPlugin from '@stylexjs/babel-plugin';+import {describe, expect, it} from 'vitest';++const __dirname = path.dirname(fileURLToPath(import.meta.url));+const ROOT = path.resolve(__dirname, '../../../..');+const SELECTOR_SOURCE = path.join(__dirname, 'Selector.tsx');++const EXPECTED_PADDING_DECLARATIONS = [+ 'padding-block:calc((var(--size-element-sm) - var(--spacing-5) - 2 * var(--border-width)) / 2)',+ 'padding-block:calc((var(--size-element-md) - var(--spacing-5) - 2 * var(--border-width)) / 2)',+ 'padding-block:calc((var(--size-element-lg) - var(--spacing-5) - 2 * var(--border-width)) / 2)',+];++describe('Selector source-build compatibility (#5464)', () => {+ it('compiles after next/babel lowers module-scope arrows', async () => {+ const source = await fs.readFile(SELECTOR_SOURCE, 'utf8');+ const result = await transformAsync(source, {+ babelrc: false,+ caller: {+ name: 'selector-source-build-test',+ isDev: false,+ isServer: false,+ supportsStaticESM: false,+ },+ configFile: false,+ envName: 'production',+ filename: SELECTOR_SOURCE,+ presets: ['next/babel'],+ plugins: [+ [+ stylexBabelPlugin,+ {+ dev: false,+ runtimeInjection: false,+ genConditionalClasses: true,+ treeshakeCompensation: true,+ unstable_moduleResolution: {+ type: 'commonJS',+ rootDir: ROOT,+ },+ },+ ],+ ],+ });++ const declarations = (result?.metadata?.stylex ?? [])+ .map(([, rule]) => rule.ltr.match(/^\.[^{]+\{(.+)\}$/)?.[1])+ .filter(declaration => declaration?.startsWith('padding-block:calc'));++ expect(declarations).toEqual(EXPECTED_PADDING_DECLARATIONS);+ });+});diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex 3dbb9d4c6b..e1f6a789b2 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -395,21 +395,22 @@ const styles = stylex.create({// `--spacing-5` is one line here because `triggerContainer` pins its// line-height to exactly that; the two must stay in step, which is why both// read the same token rather than one hardcoding 20px.-const linePad = (token: string) =>- `calc((${token} - ${spacingVars['--spacing-5']} - 2 * ${borderVars['--border-width']}) / 2)`;+// Keep these calculations inline: a consumer's Babel preset can lower a+// module-scope helper to a function expression before StyleX evaluates this+// object, and StyleX cannot constant-evaluate that transformed helper.const sizeStyles = stylex.create({sm: {minHeight: sizeVars['--size-element-sm'],- paddingBlock: linePad(sizeVars['--size-element-sm']),+ paddingBlock: `calc((${sizeVars['--size-element-sm']} - ${spacingVars['--spacing-5']} - 2 * ${borderVars['--border-width']}) / 2)`,},md: {minHeight: sizeVars['--size-element-md'],- paddingBlock: linePad(sizeVars['--size-element-md']),+ paddingBlock: `calc((${sizeVars['--size-element-md']} - ${spacingVars['--spacing-5']} - 2 * ${borderVars['--border-width']}) / 2)`,},lg: {minHeight: sizeVars['--size-element-lg'],- paddingBlock: linePad(sizeVars['--size-element-lg']),+ paddingBlock: `calc((${sizeVars['--size-element-lg']} - ${spacingVars['--spacing-5']} - 2 * ${borderVars['--border-width']}) / 2)`,},});