Skip to content

Commit 002bf2f

Browse files
committed
fix: disable debug classnames in stylex-define-vars
1 parent ba121fc commit 002bf2f

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

packages/babel-plugin/src/utils/evaluate-path.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ function evaluateThemeRef(
172172
? utils.genFileBasedIdentifier({ fileName, exportName })
173173
: utils.genFileBasedIdentifier({ fileName, exportName, key });
174174

175-
const debug = state.traversalState.options.debug;
175+
const {debug, enableDebugClassNames} = state.traversalState.options;
176176

177177
const varName =
178-
debug === true
178+
debug && enableDebugClassNames
179179
? key +
180180
'-' +
181181
state.traversalState.options.classNamePrefix +

packages/shared/__tests__/define-vars/stylex-define-vars-test.js

+76
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,82 @@ describe('stylex-define-vars test', () => {
261261
`);
262262
});
263263

264+
test('converts set of vars with nested at rules to CSS and does not include key prefix in debug mode with debug classnames off', () => {
265+
const themeName = 'TestTheme.stylex.js//buttonTheme';
266+
const classNamePrefix = 'x';
267+
const defaultVars = {
268+
bgColor: {
269+
default: 'blue',
270+
'@media (prefers-color-scheme: dark)': {
271+
default: 'lightblue',
272+
'@supports (color: oklab(0 0 0))': 'oklab(0.7 -0.3 -0.4)',
273+
},
274+
'@media print': 'white',
275+
},
276+
bgColorDisabled: {
277+
default: {
278+
default: 'grey',
279+
'@supports (color: oklab(0 0 0))': 'oklab(0.7 -0.3 -0.4)',
280+
},
281+
'@media (prefers-color-scheme: dark)': {
282+
default: 'rgba(0, 0, 0, 0.8)',
283+
'@supports (color: oklab(0 0 0))': 'oklab(0.7 -0.3 -0.4)',
284+
},
285+
},
286+
cornerRadius: '10px',
287+
fgColor: {
288+
default: 'pink',
289+
},
290+
};
291+
const [jsOutput, cssOutput] = styleXDefineVars(defaultVars, {
292+
themeName,
293+
enableDebugClassNames: false,
294+
debug: false,
295+
});
296+
297+
expect(jsOutput).toEqual({
298+
__themeName__: classNamePrefix + createHash(themeName),
299+
bgColor: `var(--${classNamePrefix + createHash(`${themeName}.bgColor`)})`,
300+
bgColorDisabled: `var(--${
301+
classNamePrefix + createHash(`${themeName}.bgColorDisabled`)
302+
})`,
303+
cornerRadius: `var(--${
304+
classNamePrefix + createHash(`${themeName}.cornerRadius`)
305+
})`,
306+
fgColor: `var(--${classNamePrefix + createHash(`${themeName}.fgColor`)})`,
307+
});
308+
309+
expect(cssOutput).toMatchInlineSnapshot(`
310+
{
311+
"x568ih9": {
312+
"ltr": ":root, .x568ih9{--xgck17p:blue;--xpegid5:grey;--xrqfjmn:10px;--x4y59db:pink;}",
313+
"priority": 0,
314+
"rtl": null,
315+
},
316+
"x568ih9-1e6ryz3": {
317+
"ltr": "@supports (color: oklab(0 0 0)){@media (prefers-color-scheme: dark){:root, .x568ih9{--xgck17p:oklab(0.7 -0.3 -0.4);--xpegid5:oklab(0.7 -0.3 -0.4);}}}",
318+
"priority": 0.2,
319+
"rtl": null,
320+
},
321+
"x568ih9-1lveb7": {
322+
"ltr": "@media (prefers-color-scheme: dark){:root, .x568ih9{--xgck17p:lightblue;--xpegid5:rgba(0, 0, 0, 0.8);}}",
323+
"priority": 0.1,
324+
"rtl": null,
325+
},
326+
"x568ih9-bdddrq": {
327+
"ltr": "@media print{:root, .x568ih9{--xgck17p:white;}}",
328+
"priority": 0.1,
329+
"rtl": null,
330+
},
331+
"x568ih9-kpd015": {
332+
"ltr": "@supports (color: oklab(0 0 0)){:root, .x568ih9{--xpegid5:oklab(0.7 -0.3 -0.4);}}",
333+
"priority": 0.1,
334+
"rtl": null,
335+
},
336+
}
337+
`);
338+
});
339+
264340
test('converts set of typed vars with nested at rules to CSS', () => {
265341
const themeName = 'TestTheme.stylex.js//buttonTheme';
266342
const classNamePrefix = 'x';

packages/shared/src/stylex-define-vars.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function styleXDefineVars<Vars: VarsConfig>(
3636
variables: Vars,
3737
options: $ReadOnly<{ ...Partial<StyleXOptions>, themeName: string, ... }>,
3838
): [VarsObject<Vars>, { [string]: InjectableStyle }] {
39-
const { classNamePrefix, themeName, debug } = {
39+
const { classNamePrefix, themeName, debug, enableDebugClassNames } = {
4040
...defaultOptions,
4141
...options,
4242
};
@@ -56,7 +56,7 @@ export default function styleXDefineVars<Vars: VarsConfig>(
5656
// Created hashed variable names with fileName//themeName//key
5757
const nameHash = key.startsWith('--')
5858
? key.slice(2)
59-
: debug
59+
: debug && enableDebugClassNames
6060
? key + '-' + classNamePrefix + createHash(`${themeName}.${key}`)
6161
: classNamePrefix + createHash(`${themeName}.${key}`);
6262

0 commit comments

Comments
 (0)