Skip to content

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

TypeScript

fix(build): preserve native light-dark tokens (#6844) (#7330)

Cindy Zhang492779355e8e
diff --git a/.changeset/vite-light-dark-targets.md b/.changeset/vite-light-dark-targets.md
new file mode 100644
index 0000000000..5bb71c70a2
--- /dev/null
+++ b/.changeset/vite-light-dark-targets.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/build': patch
+---
+
+[fix] Preserve native `light-dark()` token values in Vite source builds by defaulting Lightning CSS to Astryx's supported browser floor.
+
+@cixzhang
diff --git a/packages/build/src/vite.build.test.ts b/packages/build/src/vite.build.test.ts
index 8c82ab4394..ee860275a2 100644
--- a/packages/build/src/vite.build.test.ts
+++ b/packages/build/src/vite.build.test.ts
@@ -210,6 +210,17 @@ function itSplitsCorrectly(get: () => Built) {
expect(get().css).toContain('-webkit-user-select');
});
+ // Source-built tokens must stay native at the same browser floor Astryx
+ // documents. Lowering `light-dark()` here rewrites root variables through
+ // mode helpers that exist only on Theme wrappers, so inherited data colors
+ // become invalid before a theme can use them.
+ it('preserves native light-dark() token values', () => {
+ const {css} = get();
+ expect(css).toContain('light-dark(');
+ expect(css).not.toContain('--lightningcss-light');
+ expect(css).not.toContain('--lightningcss-dark');
+ });
+
it('links the stylesheet from the page', () => {
const {html, cssName} = get();
expect(html).toContain(cssName);
diff --git a/packages/build/src/vite.test.ts b/packages/build/src/vite.test.ts
index 5d888896f4..8f618832c8 100644
--- a/packages/build/src/vite.test.ts
+++ b/packages/build/src/vite.test.ts
@@ -12,7 +12,8 @@ import {mkdtempSync, mkdirSync, rmSync} from 'node:fs';
import {tmpdir} from 'node:os';
import path from 'node:path';
import {fileURLToPath, pathToFileURL} from 'node:url';
-import {astryxStylex} from './vite';
+import stylex from '@stylexjs/unplugin';
+import {astryxStylex, LIGHTNINGCSS_TARGETS} from './vite';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -73,6 +74,44 @@ describe('astryxStylex layer order (legacy API)', () => {
});
});
+describe('astryxStylex Lightning CSS defaults (modern API)', () => {
+ type StylexOptions = {
+ lightningcssOptions: {
+ targets: Record<string, number>;
+ drafts?: {nesting: boolean};
+ };
+ };
+
+ function getStylexOptions(
+ options: Parameters<typeof astryxStylex>[0] = {},
+ ): StylexOptions {
+ const vite = vi.spyOn(stylex, 'vite');
+ try {
+ astryxStylex(options);
+ return vite.mock.calls.at(-1)?.[0] as StylexOptions;
+ } finally {
+ vite.mockRestore();
+ }
+ }
+
+ it('preserves native light-dark() at the supported browser floor', () => {
+ expect(getStylexOptions().lightningcssOptions.targets).toEqual(
+ LIGHTNINGCSS_TARGETS,
+ );
+ });
+
+ it('keeps the floor when a consumer supplies another Lightning CSS option', () => {
+ const options = getStylexOptions({
+ stylexOverrides: {lightningcssOptions: {drafts: {nesting: true}}},
+ }).lightningcssOptions;
+
+ expect(options).toEqual({
+ targets: LIGHTNINGCSS_TARGETS,
+ drafts: {nesting: true},
+ });
+ });
+});
+
/**
* A production build had no equivalent of the dev server's split-layer plugin,
* so StyleX's `@layer priorityN` blocks sat outside the declared order and
diff --git a/packages/build/src/vite.ts b/packages/build/src/vite.ts
index 54a2359618..0581b58675 100644
--- a/packages/build/src/vite.ts
+++ b/packages/build/src/vite.ts
@@ -15,9 +15,11 @@ const LIBRARY_PATTERN = 'node_modules/@astryxdesign/';
const STYLEX_CSS_PATH = '/virtual:stylex.css';
/**
- * Browser targets for lightningcss (opt-in).
- * Only needed if your StyleX version lowers light-dark() without them.
- * Exported for consumers who want to opt in explicitly.
+ * Browser targets matching Astryx's native `light-dark()` support floor.
+ *
+ * These are the source-build default so Lightning CSS preserves
+ * `light-dark()` instead of lowering it into helper variables. Consumers may
+ * supply a stricter supported floor when their product requires one.
*/
export const LIGHTNINGCSS_TARGETS = {
chrome: 123 << 16,
@@ -70,10 +72,12 @@ export interface AstryxVitePluginOptions {
};
/**
- * LightningCSS browser targets. Only needed if your StyleX version
- * lowers light-dark() without them. Most recent versions preserve
- * light-dark() by default.
- * @default undefined (no targets set)
+ * Lightning CSS browser targets.
+ *
+ * Defaults to Astryx's native `light-dark()` support floor. Older targets
+ * may lower `light-dark()` and are outside the supported theme contract.
+ *
+ * @default LIGHTNINGCSS_TARGETS
*/
lightningcssTargets?: Record<string, number>;
@@ -125,7 +129,7 @@ export function astryxStylex(
rootDir = process.cwd(),
libraryPattern = LIBRARY_PATTERN,
layers = {},
- lightningcssTargets,
+ lightningcssTargets = LIGHTNINGCSS_TARGETS,
stylexPrefix = 'astryx',
stylexOverrides = {},
} = opts;
@@ -133,7 +137,13 @@ export function astryxStylex(
const libraryLayer = layers.library ?? 'astryx-base';
const productLayer = layers.product ?? 'product';
- // Build StyleX options with sensible defaults
+ const overrideLightningcssOptions = (
+ stylexOverrides as {lightningcssOptions?: Record<string, unknown>}
+ ).lightningcssOptions;
+
+ // Build StyleX options with sensible defaults. Merge Lightning CSS options
+ // separately so an unrelated override cannot silently discard the browser
+ // floor that keeps theme values native.
const stylexOptions: Record<string, unknown> = {
dev,
runtimeInjection: false,
@@ -143,10 +153,11 @@ export function astryxStylex(
type: 'commonJS',
rootDir,
},
- ...(lightningcssTargets && {
- lightningcssOptions: {targets: lightningcssTargets},
- }),
...stylexOverrides,
+ lightningcssOptions: {
+ targets: lightningcssTargets,
+ ...overrideLightningcssOptions,
+ },
};
// Inject our babel wrapper as a user plugin — it runs before the
@@ -570,9 +581,16 @@ function astryxStylexLegacy(options: AstryxVitePluginLegacyOptions): Plugin[] {
const libraryLayer = layers.library ?? 'astryx-base';
const productLayer = layers.product ?? 'product';
+ const legacyLightningcssOptions = (
+ stylexOptions as {lightningcssOptions?: Record<string, unknown>}
+ ).lightningcssOptions;
const validatedStylexOptions = {
propertyValidationMode: 'throw',
...(stylexOptions as any),
+ lightningcssOptions: {
+ targets: LIGHTNINGCSS_TARGETS,
+ ...legacyLightningcssOptions,
+ },
};
const astryxBabelPlugin = path.resolve(__dirname, 'babel.js');
const existingPlugins = validatedStylexOptions.babelConfig?.plugins ?? [];
@@ -656,8 +674,9 @@ function astryxStylexLegacy(options: AstryxVitePluginLegacyOptions): Plugin[] {
libraryPattern,
libraryLayer,
productLayer,
- lightningcssOptions: (stylexOptions as {lightningcssOptions?: unknown})
- ?.lightningcssOptions,
+ lightningcssOptions: (
+ validatedStylexOptions as {lightningcssOptions?: unknown}
+ ).lightningcssOptions,
}),
];
}