Skip to content

Commit 706683b

Browse files
committed
fix(build): resolve defineConsts read across files in astryxStylex
A file that reads only defineConsts values from a .stylex.ts module lost that import under the legacy {stylexOptions} form Storybook uses: the babel wrapper's StyleX instance ran without treeshakeCompensation (the unplugin applies its Vite default only to its own instance, which runs after the wrapper), so once stylex.create was compiled the TypeScript transform dropped the import and the defining module never reached the build. A constant used as a media-query key failed the build with "Invalid empty selector"; one used as a value shipped as a custom property nothing declares. The wrapper now gets treeshakeCompensation by default. The layer split also substituted constants only within each layer, so a constant defined on the library side and read on the product side stayed a placeholder in either form; every layer now sees every constant. A production build now warns, naming the variable and the rule that reads it, when the StyleX stylesheet reads a generated custom property nothing declares; undefinedVariables: 'error' fails the build instead, and this repo's Storybook and Sandbox configs set it so CI catches the next case.
1 parent e769940 commit 706683b

17 files changed

Lines changed: 423 additions & 18 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/build': patch
3+
---
4+
5+
[fix] `astryxStylex` resolves `defineConsts` values read from another file. With the `{stylexOptions}` form (the one Storybook uses), a file that read only constants from a `.stylex.ts` module lost that import once StyleX compiled it, so the constants never reached the CSS: a constant used as a media-query key failed the build ("Invalid empty selector") and one used as a value shipped as a custom property nothing declares. A constant defined on the library side of the layer split and read on the product side stayed unresolved in either form. A production build now also warns, naming the variable and the rule that reads it, when its stylesheet reads a StyleX-generated custom property that nothing declares; set `undefinedVariables: 'error'` to fail the build instead.
6+
7+
@vjeux

‎apps/sandbox/vite.config.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export default defineConfig({
191191
unstable_moduleResolution: {type: 'commonJS', rootDir: appDir},
192192
},
193193
stylexPrefix: 'x',
194+
undefinedVariables: 'error',
194195
}),
195196
react({
196197
babel: {

‎apps/storybook/.storybook/main.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const runtimeImports = [
5757
describe('storybook main.ts workspace source aliases', () => {
5858
let viteAliases: Record<string, string>;
5959
let stylexAliases: Record<string, string[]>;
60+
let undefinedVariables: unknown;
6061
const stylexSpy = vi.spyOn(build, 'astryxStylex');
6162

6263
beforeAll(async () => {
@@ -69,6 +70,11 @@ describe('storybook main.ts workspace source aliases', () => {
6970
throw new Error('Expected the Storybook StyleX options');
7071
}
7172
stylexAliases = options.stylexOptions!.aliases as Record<string, string[]>;
73+
undefinedVariables = options.undefinedVariables;
74+
});
75+
76+
it('fails the build on a generated variable nothing declares', () => {
77+
expect(undefinedVariables).toBe('error');
7278
});
7379

7480
afterAll(() => stylexSpy.mockRestore());

‎apps/storybook/.storybook/main.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ const config: StorybookConfig = {
200200
},
201201
},
202202
libraryPattern: 'packages/',
203+
// CI catches a style that reads a generated variable nothing
204+
// declares (a dropped declaration) instead of shipping it.
205+
undefinedVariables: 'error',
203206
}),
204207
],
205208
resolve: {

‎packages/build/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ Wraps `@stylexjs/unplugin` and intercepts the dev CSS endpoint (`/virtual:stylex
239239

240240
Unsupported declarations fail the build by default with StyleX's replacement guidance. Set `propertyValidationMode` explicitly in `stylexOverrides` (modern API) or `stylexOptions` (legacy API) only when a migration needs the upstream `warn` or `silent` behavior.
241241

242+
A production build also checks the StyleX stylesheet for generated custom properties (`--<prefix><hash>`) that it reads but nothing declares, which every browser would drop: a `.stylex.ts` export read in a style that is not a `defineVars`/`defineConsts` value, or one whose module never reached the build. By default it warns, naming the variable and the rule that reads it; `undefinedVariables: 'error'` (either API) fails the build instead.
243+
242244
---
243245

244246
## Advanced Options
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* Reads `defineConsts` values from other files, as a media-query key and as a
5+
* plain value, from both sides of the layer split. Only the constants are
6+
* imported: once StyleX compiles `stylex.create`, nothing in this file uses
7+
* the imports at runtime any more.
8+
*/
9+
10+
import * as stylex from '@stylexjs/stylex';
11+
import {appConsts} from './appConsts.stylex';
12+
import {libraryConsts} from './library/libraryConsts.stylex';
13+
14+
const styles = stylex.create({
15+
row: {
16+
minHeight: appConsts.rowHeight,
17+
transitionProperty: {default: 'color', [appConsts.touch]: 'none'},
18+
},
19+
panel: {
20+
paddingTop: libraryConsts.inset,
21+
maxWidth: {default: null, [libraryConsts.wide]: '640px'},
22+
},
23+
});
24+
25+
export function App() {
26+
return (
27+
<div {...stylex.props(styles.panel)}>
28+
<div {...stylex.props(styles.row)}>row</div>
29+
</div>
30+
);
31+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
import * as stylex from '@stylexjs/stylex';
4+
5+
/** Constants the app defines itself, beside the file that reads them. */
6+
export const appConsts = stylex.defineConsts({
7+
touch: '@media (pointer: coarse)',
8+
rowHeight: '52px',
9+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<!-- Copyright (c) Meta Platforms, Inc. and affiliates. -->
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8" />
6+
<title>stylex-consts fixture — defineConsts imported across files</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script type="module" src="./main.tsx"></script>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
import * as stylex from '@stylexjs/stylex';
4+
5+
/**
6+
* Constants on the library side of the layer split (the build test points
7+
* `libraryPattern` at this folder), read by an app file on the product side.
8+
*/
9+
export const libraryConsts = stylex.defineConsts({
10+
wide: '@media (min-width: 900px)',
11+
inset: '13px',
12+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
import {createRoot} from 'react-dom/client';
4+
import {App} from './App';
5+
6+
createRoot(document.getElementById('root')!).render(<App />);

0 commit comments

Comments
 (0)