Skip to content

Commit c38b152

Browse files
authoredSep 6, 2022
Use standard identifier generation logic for variable names (#810)
1 parent bfde2e5 commit c38b152

File tree

6 files changed

+102
-30
lines changed

6 files changed

+102
-30
lines changed
 

‎.changeset/gold-trainers-sing.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/css': patch
3+
---
4+
5+
Fix spaces in debug IDs for variable names.

‎.changeset/hungry-ties-judge.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@vanilla-extract/css': minor
3+
---
4+
5+
Support excluding file names from `generateIdentifier` output. This is available by passing a newly-added options object rather than a string.
6+
7+
**Example usage**
8+
9+
```ts
10+
import { generateIdentifier } from '@vanilla-extract/css';
11+
12+
const identifier = generateIdentifier({
13+
debugId,
14+
debugFileName: false,
15+
});
16+
```

‎.changeset/popular-seas-relate.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/css': patch
3+
---
4+
5+
Fix file name prefix in debug names when file extension is `.cjs` or `.mjs`.

‎packages/css/src/identifier.test.ts

+36-4
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,58 @@ import { generateIdentifier } from './identifier';
33

44
describe('identifier', () => {
55
beforeAll(() => {
6-
setFileScope('test');
6+
setFileScope('path/to/file.css.ts');
77
});
88

99
afterAll(() => {
1010
endFileScope();
1111
});
1212

13-
it(`should create a valid identifier`, () => {
13+
it(`should ignore file scopes without a file extension when creating a path prefix`, () => {
14+
setFileScope('test');
1415
expect(generateIdentifier(undefined)).toMatchInlineSnapshot(`"skkcyc0"`);
16+
endFileScope();
17+
});
18+
19+
it(`should create a valid identifier`, () => {
20+
expect(generateIdentifier(undefined)).toMatchInlineSnapshot(
21+
`"file__18bazsm0"`,
22+
);
1523
});
1624

1725
it('should create a valid identifier with a debug id', () => {
1826
expect(generateIdentifier('debug')).toMatchInlineSnapshot(
19-
`"debug__skkcyc1"`,
27+
`"file_debug__18bazsm1"`,
2028
);
2129
});
2230

2331
it('should create a valid identifier with a debug id with whitespace', () => {
2432
expect(generateIdentifier('debug and more')).toMatchInlineSnapshot(
25-
`"debug_and_more__skkcyc2"`,
33+
`"file_debug_and_more__18bazsm2"`,
2634
);
2735
});
36+
37+
describe('options object', () => {
38+
it(`should create a valid identifier`, () => {
39+
expect(generateIdentifier({})).toMatchInlineSnapshot(`"file__18bazsm3"`);
40+
});
41+
42+
it('should create a valid identifier with a debug id and with file name debugging explicitly enabled', () => {
43+
expect(
44+
generateIdentifier({ debugId: 'debug', debugFileName: true }),
45+
).toMatchInlineSnapshot(`"file_debug__18bazsm4"`);
46+
});
47+
48+
it('should create a valid identifier with a debug id and without file name debugging', () => {
49+
expect(
50+
generateIdentifier({ debugId: 'debug', debugFileName: false }),
51+
).toMatchInlineSnapshot(`"debug__18bazsm5"`);
52+
});
53+
54+
it('should create a valid identifier without a debug ID or file name', () => {
55+
expect(
56+
generateIdentifier({ debugFileName: false }),
57+
).toMatchInlineSnapshot(`"_18bazsm6"`);
58+
});
59+
});
2860
});

‎packages/css/src/identifier.ts

+33-10
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,46 @@ import hash from '@emotion/hash';
33
import { getIdentOption } from './adapter';
44
import { getAndIncrementRefCounter, getFileScope } from './fileScope';
55

6-
function getDevPrefix(debugId: string | undefined) {
6+
function getDevPrefix({
7+
debugId,
8+
debugFileName,
9+
}: {
10+
debugId?: string;
11+
debugFileName: boolean;
12+
}) {
713
const parts = debugId ? [debugId.replace(/\s/g, '_')] : [];
8-
const { filePath } = getFileScope();
914

10-
const matches = filePath.match(
11-
/(?<dir>[^\/\\]*)?[\/\\]?(?<file>[^\/\\]*)\.css\.(ts|js|tsx|jsx)$/,
12-
);
15+
if (debugFileName) {
16+
const { filePath } = getFileScope();
17+
18+
const matches = filePath.match(
19+
/(?<dir>[^\/\\]*)?[\/\\]?(?<file>[^\/\\]*)\.css\.(ts|js|tsx|jsx|cjs|mjs)$/,
20+
);
1321

14-
if (matches && matches.groups) {
15-
const { dir, file } = matches.groups;
16-
parts.unshift(file && file !== 'index' ? file : dir);
22+
if (matches && matches.groups) {
23+
const { dir, file } = matches.groups;
24+
parts.unshift(file && file !== 'index' ? file : dir);
25+
}
1726
}
1827

1928
return parts.join('_');
2029
}
2130

22-
export function generateIdentifier(debugId: string | undefined) {
31+
interface GenerateIdentifierOptions {
32+
debugId?: string;
33+
debugFileName?: boolean;
34+
}
35+
36+
export function generateIdentifier(debugId?: string): string;
37+
export function generateIdentifier(options?: GenerateIdentifierOptions): string;
38+
export function generateIdentifier(
39+
arg?: string | GenerateIdentifierOptions,
40+
): string {
41+
const { debugId, debugFileName = true } = {
42+
...(typeof arg === 'string' ? { debugId: arg } : null),
43+
...(typeof arg === 'object' ? arg : null),
44+
};
45+
2346
// Convert ref count to base 36 for optimal hash lengths
2447
const refCount = getAndIncrementRefCounter().toString(36);
2548
const { filePath, packageName } = getFileScope();
@@ -31,7 +54,7 @@ export function generateIdentifier(debugId: string | undefined) {
3154
let identifier = `${fileScopeHash}${refCount}`;
3255

3356
if (getIdentOption() === 'debug') {
34-
const devPrefix = getDevPrefix(debugId);
57+
const devPrefix = getDevPrefix({ debugId, debugFileName });
3558

3659
if (devPrefix) {
3760
identifier = `${devPrefix}__${identifier}`;

‎packages/css/src/vars.ts

+7-16
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,20 @@ import {
55
MapLeafNodes,
66
CSSVarFunction,
77
} from '@vanilla-extract/private';
8-
import hash from '@emotion/hash';
98
import cssesc from 'cssesc';
109

1110
import { Tokens, NullableTokens, ThemeVars } from './types';
12-
import { getAndIncrementRefCounter, getFileScope } from './fileScope';
1311
import { validateContract } from './validateContract';
14-
import { getIdentOption } from './adapter';
12+
import { generateIdentifier } from './identifier';
1513

1614
export function createVar(debugId?: string): CSSVarFunction {
17-
// Convert ref count to base 36 for optimal hash lengths
18-
const refCount = getAndIncrementRefCounter().toString(36);
19-
const { filePath, packageName } = getFileScope();
20-
const fileScopeHash = hash(
21-
packageName ? `${packageName}${filePath}` : filePath,
15+
const cssVarName = cssesc(
16+
generateIdentifier({
17+
debugId,
18+
debugFileName: false,
19+
}),
20+
{ isIdentifier: true },
2221
);
23-
const varName =
24-
getIdentOption() === 'debug' && debugId
25-
? `${debugId}__${fileScopeHash}${refCount}`
26-
: `${fileScopeHash}${refCount}`;
27-
28-
const cssVarName = cssesc(varName.match(/^[0-9]/) ? `_${varName}` : varName, {
29-
isIdentifier: true,
30-
});
3122

3223
return `var(--${cssVarName})` as const;
3324
}

0 commit comments

Comments
 (0)