Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: all flattenToken need hashed #204

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const flattenTokenCache = new WeakMap<any, string>();
/**
* Flatten token to string, this will auto cache the result when token not change
*/
export function flattenToken(token: any, hashed: boolean = false) {
export function flattenToken(token: any) {
let str = flattenTokenCache.get(token) || '';

if (!str) {
Expand All @@ -45,17 +45,15 @@ export function flattenToken(token: any, hashed: boolean = false) {
if (value instanceof Theme) {
str += value.id;
} else if (value && typeof value === 'object') {
str += flattenToken(value, hashed);
str += flattenToken(value);
} else {
str += value;
}
});

// https://github.com/ant-design/ant-design/issues/48386
// Should hash the string to avoid style tag name too long
if (hashed) {
str = hash(str);
}
str = hash(str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面的 #48386 应该是跷跷板问题吧,hash 不缩短,拼接就会很长🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

额,48386 指的是什么?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该不是这个……


// Put in cache
flattenTokenCache.set(token, str);
Expand All @@ -67,7 +65,7 @@ export function flattenToken(token: any, hashed: boolean = false) {
* Convert derivative token to key string
*/
export function token2key(token: any, salt: string): string {
return hash(`${salt}_${flattenToken(token, true)}`);
return hash(`${salt}_${flattenToken(token)}`);
}

const randomSelectorKey = `random-${Date.now()}-${Math.random()}`.replace(
Expand Down
2 changes: 1 addition & 1 deletion tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe('csssinjs', () => {
const { container } = render(<TokenShower />);

// src/util.tsx - token2key func
expect(container.textContent).toEqual('1cpx0di');
expect(container.textContent).toEqual('1fs647j');
});

it('hash', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/util.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('util', () => {
// Repeat call flattenToken
for (let i = 0; i < 10000; i += 1) {
const tokenStr = flattenToken(token);
expect(tokenStr).toEqual('a1');
expect(tokenStr).toEqual('d9a1z5');
}

expect(checkTimes).toEqual(1);
Expand Down
Loading